Here's a comparison between a reflected Object#hash and a direct version.
o = Object.new; t = Time.now; 10_000_000.times { o.hash }; p Time.now - t
Direct:
20.226
20.07
20.531
Reflected:
21.423000000000002
21.452
22.881
A long time ago there was also a method/callable that used code generation to do the call; that might also be an option. We ripped it out back then for whatever reason (used little, library dependency, whatever) but it could be a way to do direct calls without adding all those classes.
On 7/9/06, Thomas E Enebo <[EMAIL PROTECTED]> wrote:
From what I remember of this the last time it was brought up was
not the number of classes, but the sheer size of our jar file once
every single method was converted to a class. Then again perhaps
adding 5 MB for 10x method invocation is more than worth it. Of
course, we could always selectively do it too :|
I also thought we did not find this particular speed up last time.
Or we thought we did then we realized something else. I am going to
hit the archives and see if I can refresh my memory.
On Sun, 09 Jul 2006, Charles O Nutter defenestrated me:
>
> It has long been assumed that reflection is fast, and that much is
> true. The JVM has done some amazing things to make reflected calls
> really f'n fast these days, and for most scenarios they're as fast as
> you'd ever want them to be. I certainly don't know the details, but
> the rumors are that there's code generation going on, reflection calls
> are actually doing direct calls, the devil and souls are involved, and
> so on. Many stories, but not a lot of concrete evidence.
> A while back, I started playing around with a "direct invocation
> method" in JRuby. Basically, it's an interface that provides an
> "invoke" method. The idea is that for every Ruby method we provide in
> Java code you would create an implementation of this interface; then
> when the time comes to invoke those methods, we are doing an
> INVOKEINTERFACE bytecode rather than a call through reflection code.
> The down side is that this would create a class for every Java method,
> which amounts to probably several hundred classes. That's certainly
> not ideal, but perhaps manageable considering you'd have JRuby loaded
> once in a whole JVM for all uses of it. Still, it's a fairly big deal.
> The up side, or so I hoped, would be that a straight INVOKEINTERFACE
> would be faster than a reflected call, regardless of any optimization
> going on, and we wouldn't have to do any wacked-out code generation.
> Initial results seemed to agree with the upside, but in the long term
> nothing seemed to speed up all that much. There's actually a number of
> these "direct invocation methods" still in the codebase, specifically
> for a few heavily-hit String methods like hash, [], and so on.
> So I figured I'd resolve this question once and for all in my mind. Is
> a reflected call as fast as this "direct invocation"?
> A test case is attached. I ran the loops for a ten million
> invocations...then ran them again timed, so that hotspot could do its
> thing. The results are below for both pure interpreter and hotspotted
> runs (time are in ms).
> Hotspotted:
> first time reflected: 293
> second time reflected: 211
> total invocations: 20000000
> first time direct: 16
> second time direct: 8
> total invocations: 20000000
> Interpreted:
> first time reflected: 9247
> second time reflected: 9237
> total invocations: 20000000
> first time direct: 899
> second time direct: 893
> total invocations: 20000000
> I would really love for someone to prove me wrong, but according to
> this simple benchmark, direct invocation is faster--way, way
> faster--in all cases. It's obviously way faster when we're purely
> interpreting or before hotspot kicks in, but it's even faster after
> hotspot. I made both invocations increment a static variable, which
> I'm hoping prevented hotspot from optimizing code into oblivion.
> However even if hotspot IS optimizing something away, it's apparent
> that it does a better job on direct invocations. I know hotspot does
> some inlining of code when it's appropriate to do so...perhaps
> reflected code is impossible to inline?
> Anyone care to comment? I wouldn't mind speeding up Java-native method
> invocations by a factor of ten, even if it did mean a bunch of extra
> classes. We could even selectively "directify" methods, like do
> everything in Kernel and Object and specific methods elsewhere.
> --
> Charles Oliver Nutter @ [1]headius.blogspot.com
> JRuby Developer @ [2]www.jruby.org
> Application Architect @ [3]www.ventera.com
>
> References
>
> 1. http://headius.blogspot.com/
> 2. http://www.jruby.org/
> 3. http://www.ventera.com/
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Jruby-devel mailing list
> Jruby-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jruby-devel
--
+ http://www.tc.umn.edu/~enebo +---- mailto:[EMAIL PROTECTED] ----+
| Thomas E Enebo, Protagonist | "Luck favors the prepared |
| | mind." -Louis Pasteur |
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jruby-devel mailing list
Jruby-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jruby-devel
--
Charles Oliver Nutter @ headius.blogspot.com
JRuby Developer @ www.jruby.org
Application Architect @ www.ventera.com
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ Jruby-devel mailing list Jruby-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jruby-devel