views:

758

answers:

5

Spring and Hibernate uses reflection for bean creation (in case of spring) and POJO mapping (in case of Hibernate). Does it negatively impacts on performance ? Because reflection is slower compare to direct object creation.

+5  A: 

Check out Hibernate Q&A section in FAQ, it is almost all about instantiation issues

In the very latest versions of Hibernate, "reflection" is optimised via the CGLIB runtime bytecode generation library. This means that "reflected" property get / set calls no longer carry the overhead of the Java reflection API and are actually just normal method calls. This results in a (very) small performance gain.

victor hugo
Just for the record, this FAQ is now out-of-date. CGLIB is no longer the default reflection provider for Hibernate: as of Hibernate 3.3, CGLIB has been replaced as default provider by Javassist.
Cowan
Really? Good to know :)
victor hugo
+8  A: 

Yes, it probably does. Hibernate is pretty heavily optimised in various cunning ways, but it will still be slower than low-level data access with exactly the right prepared statement, assuming the cache doesn't help you.

But that's not the question you need to ask anyway.

You need to ask whether it affects performance significantly - at which point I suspect you'll find the answer is "no". In terms of Hibernate, the underlying database access is likely to be a lot slower than the overhead due to Hibernate. In terms of Spring, the bean creation often happens only at the very start of the program, a single time.

As always, if you have concerns, benchmark and profile a realistic scenario.

Jon Skeet
+2  A: 

The chances of the overhead of reflection being significant is remote, in any reasonable application. (there are always unreasonable applications :)

For a comparison; run your app through a profile and have a look at just how much memory/cpu is consumed by the JDBC driver.

The important thing about hibernate and spring is that they are designed to optimise your performance and make you more productive and your code more reliable.

UPDATE: I think the point that myself and Mr Skeet are trying to make is that the performance hit of using reflection in these frameworks is minor. That combined with the fact that reflection is a vital part of the implementation of these frameworks means that there is little choice.

Gareth Davis
I am totally agreed with your comment that spring and hibernate is develope for making developer more productive. But just want to know reflection part in terms of performance hit in case of both the framework.
Silent Warrior
A: 

You can only find out if you're measuring for your specific workload.

Assume that for retrieving + hydrating 100 entities Spring+Hibernate will add an overhead of 3 milliseconds.

If your unit of work usually takes 120 milliseconds, then there is a very small overhead. If it take 5 milliseconds, then it's serious.

Robert Munteanu
+4  A: 

While there is a performance penalty, it is relatively low and can be considered negligible. The key issue here is that the benefit you gain from using reflection far outweighs the very small performance penalty you pay.

Remember that when looking at performance, design the system the way you want and worry about performance when it becomes a problem.

Michael Wiles