开发者

What parts of reflection are unperformant? [duplicate]

开发者 https://www.devze.com 2023-02-06 01:49 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Why reflection is slow?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Why reflection is slow?

It is said, that some parts of reflection does not perform very well. Is that true and which part开发者_如何学JAVAs of it are considered to perform slow?

Background: I am currently experimenting with a self written persistance layer using reflection. I have done no measurement yet. I just need to know where possible points of optimization are located, so that I can design the program well.


Most frameworks that use reflection cache the results of the reflective operations so that they do not have to perform them again. So in fact everything happens once, at startup.

Apart from that - all parts are "unperformant". The whole reflection API should be avoided for regular code. For a framework - see the 1st paragraph.


Typically methods invocation works well. Performance problems may be caused by Class.getMethod(), Class.getField(), Class.getDeclaredMethod() etc. Class.forName() is also slow (although it is rather dynamic class loading than reflection).

So, I'd recommend you to cache results of method locating methods, e.g.

Method m = Class.getMethod("foo"); // do it once

// now invoke it as many times as you want

m.invoke(obj);


don't worry. it's very fast. especially in comparison to database operations.

optimizations in a persistence framework like using runtime bytecode generation are ... very odd. we are talking about database here... just use reflection.

PS: a very good and relavant blog article, published today

http://buzdin.blogspot.com/2011/01/is-java-reflection-really-slow.html


The whole point of reflection is to circumvent the type system to do things the language was never intended to do in the first place. So it will be slow no matter what you do.

Reflection allows you to trade-off performance to get a simpler solutions that can be written faster and maintained easier. As soon as you find yourself complicating your code with reflection, you're doing something wrong.

Update

By "simpler", I mean less lines of code. A shorter solution basically. Reflection allows you to abandon type safety to write a shorter, more concise program. This is the rational behind dynamic typing. Java generics gives you a more complex program, but you have type safety. Reflection gives you dynamic typing in a statically typed language. My point was that, if the "reflection" version of your program is more complex than an equivalent "non-reflection" version, then the non-reflection version is superior because it will run faster and be less complicated.

There are other uses of reflection, but metaprogramming isn't really one of them. Metaprogramming is where you write a program that writes another program. For example, lets say you wanted to reformat the names of a large group of files; maybe a collection of wedding photos. A program to do this my look something like:

mv "1bill and fred(kfjw0f3).jpg" "1 - bill and fred.jpg"
mv "2sam and kim(g02fsgsg).jpg" "2 - sam and kim.jpg"
.
.
.

A Ruby program that generates that list of mv commands would be a metaprogram.


Speed mostly comes down to how many instructions need to executed on the processor

(gross oversimplification, but let's run with it for second).

When you call a method, the code can be JIT compiled, giving minimal overhead. You already know what method is going to be called, therefore you know if you're calling it in a valid way, what it returns, what code it needs, etc.

When you do this with reflection, everything goes out the window. You have to try to find the method with a string, then see if you're calling it right, then get all of the code and run it. Usually, none of this can be done beforehand.


This comes back to the difference between static and dynamic:

Static: fast, everything known early, minimal effort necessary at execution time.

Dynamic: Slow, things are figured out later, so things have to be done at run time.

this does not mean reflection is 'bad'!!!!!!!! Speed is NOT the be all and the end all!!!!!

If it saves time and makes things simpler: Do it! If you find out later that you need better performance and the reflection is in the ~20% that is run ~80% of the time, then consider some other solutions. If it was, java would be a very different language and you probably wouldn't be using objects nearly at all. Obviously you should have speed in mind and have some knowledge about reflection, which is exactly why you're asking this question. :D

0

精彩评论

暂无评论...
验证码 换一张
取 消