As per my understanding, use of Java reflection API slows down code execution by orders. But then I see it being used in so many places in Java universe. To name a few :
根据我的理解,使用Java反射API会降低按顺序执行代码的速度。但是我看到它在Java世界的很多地方被使用。举几个例子:
- Annotations
- 注释
- Spring framework (AOP)
- Spring框架(AOP)
- Hibernate
- Hibernate
- MyBatis
- MyBatis
Which implies that there's some fact about java reflection (aka optimization technique) that I have missed out on. Any pointers ?
这意味着我已经错过了一些关于java反射(即优化技术)的事实。指针吗?
2 个解决方案
#1
15
Main point: because they have no other choice.
要点:因为他们别无选择。
Java is not a dynamic language, so the only way these frameworks can provide their services is by reflection.
Java不是一种动态语言,因此这些框架提供服务的唯一方式是反射。
Second, notice that most of the reflection work these framework do happens only once, during initialization, so the runtime performance is not affected.
其次,请注意,这些框架的大多数反射工作只在初始化期间发生一次,因此不会影响运行时性能。
About the performance of reflection
There is one distinction that I notice being mixed up all the time:
我注意到有一个区别一直被混淆:
- reflective lookup of members;
- 反射查找的成员;
- reflective member access (invocation/read/write).
- 反光的成员访问(调用/读/写)。
Number 1 is slow (this is the "orders" you mention); number 2 is the one that has received significant speed improvements and is now only a couple of times slower than native access.
数字1是慢的(这是你提到的“命令”);数字2已经得到了显著的速度改进,现在只比本机访问慢了几倍。
#2
3
As a general rule, performance issues should be addressed by profiling. Leaving aside major improvements in reflection performance, all of those frameworks emphasize one-time lookups at startup (or later, in cases of lazy initialization). In the sort of enterprise app that uses them, that's not really relevant. As long as invoke
is optimized, most of the penalty will go away.
一般来说,性能问题应该通过剖析来解决。除了反射性能方面的重大改进之外,所有这些框架都强调在启动时(或者在延迟初始化的情况下)进行一次性查找。在使用它们的企业应用程序中,这并不相关。只要优化了调用,大部分的代价就会消失。
#1
15
Main point: because they have no other choice.
要点:因为他们别无选择。
Java is not a dynamic language, so the only way these frameworks can provide their services is by reflection.
Java不是一种动态语言,因此这些框架提供服务的唯一方式是反射。
Second, notice that most of the reflection work these framework do happens only once, during initialization, so the runtime performance is not affected.
其次,请注意,这些框架的大多数反射工作只在初始化期间发生一次,因此不会影响运行时性能。
About the performance of reflection
There is one distinction that I notice being mixed up all the time:
我注意到有一个区别一直被混淆:
- reflective lookup of members;
- 反射查找的成员;
- reflective member access (invocation/read/write).
- 反光的成员访问(调用/读/写)。
Number 1 is slow (this is the "orders" you mention); number 2 is the one that has received significant speed improvements and is now only a couple of times slower than native access.
数字1是慢的(这是你提到的“命令”);数字2已经得到了显著的速度改进,现在只比本机访问慢了几倍。
#2
3
As a general rule, performance issues should be addressed by profiling. Leaving aside major improvements in reflection performance, all of those frameworks emphasize one-time lookups at startup (or later, in cases of lazy initialization). In the sort of enterprise app that uses them, that's not really relevant. As long as invoke
is optimized, most of the penalty will go away.
一般来说,性能问题应该通过剖析来解决。除了反射性能方面的重大改进之外,所有这些框架都强调在启动时(或者在延迟初始化的情况下)进行一次性查找。在使用它们的企业应用程序中,这并不相关。只要优化了调用,大部分的代价就会消失。