Why is reflection slow?
Asked Answered
K

5

29

Is it because we should load class (by string for example), create instance, then search for appropriate method, pack parameters, and then just invoke method? So most time is spent on these operations instead of explicit method invocation on an object, right?

Kirkkirkcaldy answered 17/8, 2010 at 13:6 Comment(1)
In modern jdk/jvm implementation it's not so slow as you think.Mcalpine
M
40

Every step you take needs to be validated every time you take it when you use reflection. For example, when you invoke a method, it needs to check whether the target is actually an instance of the declarer of the method, whether you've got the right number of arguments, whether each argument is of the right type, etc.

There's absolutely no possibility of inlining or other performance tricks.

If you're finding types or methods by name, that's at best going to involve a simple map lookup - which will be performed every time you execute it, rather than once at JIT time.

Basically there's a lot more to do. However, reflection has become a lot faster than it used to be... if you're finding it much too slow, you may well be overusing it.

Monatomic answered 17/8, 2010 at 13:10 Comment(4)
Another subtle issue is that when using reflection, obfuscators may need to be set to be less aggressive, resulting in fewer benefits. (Many obfuscator makers claim that obfuscation offers performance benefits as well as IP protection and footprint reduction. See, e.g., this article.)Whosoever
Is it just invocations that is especially slow? What about other reflection uses such as enumerating properties, getting declaring type, getting getter/setter methods, etc? Are these slow as well?Zettazeugma
@zespri: Well, "slow" is usually relative to something else - it's hard to say whether enumerating properties is "slow" because that's not something done outside reflection as well - whereas "invoke method" or "get field value" is much slower with reflection than directly. Whether the reflection-only operations are slower than your app needs them to be is a different matter...Monatomic
Thank you, that's fair enough.Zettazeugma
S
6

As an addendum to Jon Skeet's answer above (I need more reputation in order to be able to comment.):

Reflection is dependent on CPU resources being available; if you have problem with your application being slow, reflection won't solve anything, just make it slower.

Like Java itself, reflection isn't slow any more - it is more of an old rumor ;)

Shaina answered 17/8, 2010 at 13:31 Comment(2)
Reflection not being slow is not true. A toString() for three vairable class, is fifty percent slower for me compared to hand coded toString. (I am using OpenJDK 8)Jitterbug
@AjeetGanga Your problem there is that you're using OpenJDK 8 instead of a modern JDK/JVM. As the original post said, "Reflection isn't slow anymore." And this is true, modern JVM implementations are better at reflective operations.Extricate
H
3

When you call a method, you need to know if you're doing so with valid arguments, on a valid object, what the return type is, and what bytecode to execute. When the exact method is specified in code, java can quickly figure this stuff out and move on to actually executing the method.

When you do this with reflection, you know a lot less. Since the method to be invoked wasn't specified in the code, none of this can be done beforehand, and the VM has to do things at run time that are far more complex and processor intensive.

Polymorphic method calls can be though of as somewhere between these two extremes. You don't know what method to invoke until run time, but at least you can be sure of the method's name, arguments, and return type. The more you know about what method to execute, the more Java can avoid doing at run time.

This proves that reflection is slower, but not that it is actually "slow." If you need reflection or polymorphic methods, use them, and save the judgement of what is "slow" for later.

Hinz answered 20/1, 2011 at 23:44 Comment(0)
S
0

If you use it in an appropriate way, it is not that slow. For example, I used it for scanning all the property from model class, it was working perfectly.

In other cases, such as checking whether the target has the same type or signature, it is absolutely slow.

Event it is sometime slow, but it is important for out of the box implementation...:D.

Starry answered 10/1, 2014 at 19:10 Comment(0)
S
0

According to Oracle documentations

Reflection is slower Because it involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

Sharma answered 4/6, 2019 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.