[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scripting

Katie Lauren Lucas wrote:

> >   - For an 'if' branch you can at runtime know how much chance there
> > is
> >     that the 'if' will be positive or negative. Based on that
> > information you can
> >     choose to optimize differently. At compile time this is often
> > nearly
> >     impossible to know.
> No you can't. Well, not unless you're willing to accept being wrong: you only
> know the count of routes through that if statement at program termination.
> Before that, you're going to get it wrong some of the time.
> If you're willing to have it wrong, I don't quite see how you're going to get a
> better hit than a general optimiser.

The point is not getting it right all the time. The point is getting it right most
of the time. If 90% of the cases the assumption is true then the optimization
is useful for 90% of the cases. The nice thing is that this can dynamically
adapt. If later conditions change so that the 'if' suddenly changes most
common branch then HotSpot can detect that and reoptimize the code
due to new conditions.

This is all statistics. The idea is to try to find the code that best matches
current run time environment. Of course it will get it wrong sometimes.
But that's not bad if it gets it right more than wrong.

> Note also that program execution behaviour evolves over a program run: what's
> true at 20s in won't be true at 100s in...

Yes, that's why it can dynamically adapt.

> >   - If you have a function that takes some 'int' parameter (just an
> > example)
> >     you can detect at runtime that the int parameter is usually between
> > 1 to 10.
> >     In general a compiler cannot know that. If there is a switch in that
> > routine
> >     you can then optimize/compile that switch based on the information
> > that
> >     the number is usually between 1 and 10.
> How can you know that? Knowing that before program termination is akin to
> solving the halting problem. Again.

This is all about statistics. HotSpot doesn't need to know that the number
if ALWAYS between 1 and 10. HotSpot only detects that it seems to be
OFTEN between 1 and 10. If that is what happens often then it makes
sense to optimize the code for that. HotSpot is not trying to prove anything
or make assumptions that are wrong. It is all about statistical analyses.
To detect this HotSpot builds statistical information about the run time
environment (like these variables). From that statistical information it
can detect that in the past X seconds that value appeared to be between
1 and 10 for about 90% of the time (for example). That's all it needs
to know.


Jorrit.Tyberghein@uz.kuleuven.ac.be, University Hospitals KU Leuven BELGIUM

The labyrinth of Ephebe is ancient and full of one hundred and one amazing
things you can do with hidden springs, razor-sharp knives, and falling
        -- (Terry Pratchett, Small Gods)