From: Charles Oliver Nutter Date: 2008-12-05T11:30:48+09:00 Subject: [ruby-core:20352] Re: Behavior: autoload calls rb_require() directly Michael Selig wrote: > On Fri, 05 Dec 2008 11:56:19 +1100, Charles Oliver Nutter > wrote: > >> All these optimizations are predicated on arriving at a static view of >> the world at some point during execution. The ability to redefine >> Fixnum#+, for example, means that we'll always need (at minimum) a >> guard against + being overridden. > > I know nothing about JRuby, so pardon my ignorance if I have got it all > wrong, but I figure that in order to optimize an expression like "a + b" > in a loop to an integer addition, at some point before the loop you need > to establish that both "a" & "b" are Fixnums, and are going to stay > Fixnums for the duration of the loop. Only then you know that the "+" is > actually Fixnum#+, and that the method can be optimized to an integer > addition (unless "+" has been overridden). Assuming that is possible, > then at that time can't you also establish that Fixnum#+ isn't > overridden, and won't be during the loop? In simple cases, you can. However a general solution doesn't necessarily work, since the purpose of that loop could be to define new methods. Of course if you can analyze everything infinitely deep, you can learn all you need to know. In JRuby currently we only do simple inline caching, like 1.9 does. However we compile code lazily, so it is possible for us to know at compile time that a given method has only ever been called on Fixnum, and add a guarded *static* call along with the dynamic path. I've prototyped this and it helps performance quite a bit; the JVM is able to optimize those calls directly. We can look at this in terms of two extremes. On the one hand, everything is static and can be optimized completely via static analysis. On the other hand, everything is dynamic and static analysis gets you get little. In that case, we need dynamic optimization to get the best performance. So we watch, we profile, we record everything happening at runtime and as things settle down we can make static analysis against that new information. The cost of having the core classes totally mutable is that in addition to generating optimized logic, we have to also generate bail-out logic. We're still experimenting with the next steps to take for JRuby, but I think we can get dynamic calls inlining very soon. - Charlie