Tip:
Highlight text to annotate it
X
[How to implement optimizations] Okay, well that sounds great, so far.
We have an idea of when we can possibly remove things
based on mathematical truths.
But how do we actually implement them
in our JavaScript interpretor--in our web browser?
Well, step 1 is to think of a big library of optimizations.
For example, X times 1 is always equal to X,
and X+0 is always equal to X.
So if I see one of these expressions,
I could just replace it with the simpler one on the right.
And I'm going to do that by transforming the parse tree directly.
But before we talk about transforming the parse tree,
we're going to need to be certain about what sort of optimizations we want.
Remember, we can only do optimizations that don't change the meaning of the program.
We have to get the same answer, just in less time.
So here I'm going to have you take a look and evaluate
or tell me about some candidate optimizations.
I want you to fill in each of these 4 blanks. [X1==] [X0==] [X2==] [X/X==]
Write the simplest expression you can to make the equality true.
So can I replace X times 1 with something simple that fits in this box?
Write the simplest expression you can
that retains the meaning in all cases.
This is a little trickier than it looks, so try it out.