Thursday, January 25, 2007

Fast Reflection

I replaced some critical reflective method and constructor invocations in one of my frameworks with cglib's optimized counterparts and saw about a 40% overall performance boost on Java 1.5. I only replaced the invocations, not the lookups, etc. I suspect cglib elides the security checks among other things, but I haven't dug too deeply.


Blogger Eugene Kuleshov said...

Bob, java relflection supposed to generate class after something like 15 invocations. Haven you take that into your measurements?

6:08 PM  
Blogger Bob said...

Yeah, I iterated about a million times.

6:22 PM  
Blogger Eugene Kuleshov said...


7:25 PM  
Blogger Yuri Schimke said...

Try it with setAccessible(true), it skips the security checks and in some situations may get you close to compiled speed.

This is a tip from a friend, and we've had mixed results with it. But worth trying.

11:32 PM  
Blogger Bob said...

I did that, too.

11:51 PM  
Blogger Chris said...

It's true, there aren't any security checks. Also, don't go crazy using it across the board or you'll exhaust your permgen memory. Glad it's still useful though... I didn't think this stuff would find much use after 1.5.

12:14 AM  
Blogger Bob said...

Hmmmm... we're already pushing our permgen pretty far. I should make this switchable.

12:29 AM  
Blogger Konstantin.Solomatov said...

If you turn off a reflection check with call of setAccessible method, you will have almost the same performance as CGLib's.

It would be very strange if Sun's implementation is slower than one that is created by third party.

1:40 PM  
Blogger afsina said...

i wish Guice did not carry the cglib stuff within. it makes the jar size pretty big.

1:42 PM  
Blogger Bob said...

Chris Nockleberg is helping us trim that down.

3:57 PM  

Post a Comment

<< Home