Hibernate的实现。我们是否支付反映罚款? [英] Hibernate implementation. Are we paying the reflection penalty?

查看:102
本文介绍了Hibernate的实现。我们是否支付反映罚款?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很久以前,我使用反射来创建一个迷你ORM。



在阅读关于反射时,我得到了类似的答案: $ b Java反射性能



这完全有意义,我放弃了我的迷你ORM并锐化我的CTRL + C,CTRL + V键(这个lib的目的是为了避免在我正在处理的web应用程序中为不同的表格重复写入不同的表格)



<由于某些原因,我现在不记得(也不想记住)我正在阅读(或试图......)Hibernate源代码,因为我想知道他们是否使用AOP来即时生成代码并避免反映惩罚,但令我惊讶的是,我所看到的只是纯粹的反思。



这是否意味着最为接受的ORM框架?是否确实在几年前阻止我继续我的天真努力? :)



我的问题是:有人可以证实我对Hibernate实现的理解吗?他们是否随时生成字节码以提高性能?还是我们(当我们使用它)总是支付反映罚款(顺便说一下,如果差异是在几毫秒内,我们都没有注意到,也没有抱怨过)

我们是否支付反思如果我们是,我认为这是值得的!



问候。



使用Reflection有不同的成本级别。不断寻找如果用户想到反射API必须完成以调用该方法的任务,那么每个部分都是有意义的慢,并消耗CPU周期。



定位一种方法od




  • 访问特定类的每个方法
  • found 方法生成字节码。



在一个典型的类中的方法数量的因素,并且这些操作中的一些并不重要,显然这可能是昂贵的。



调用方法。



每个反射方法相当于一小部分字节码,用一些样板调用目标方法来匹配反射界面。在它可以这样做之前,它必须执行一些完整性检查,以便它可以用好的消息来投诉,而不是让运行时抛出ClassCastException和类似的异常。




  • 如果实例方法检查在isnt null中传递的实例并且是正确的类型。
  • 检查参数参数包括正确的参数量和类型。
  • 在try catch中执行该方法。在投掷ITE等。



所有这些附加内容都会增加一些成本 - 并不是很多,但它确实让事情变得更慢。 >

运行时间成本



一般来说,缓存方法和调用并不是成本,但会稍慢。反射api本身确实试图缓存方法和类,但找到正确的方法等仍然是一个缓慢的操作。


Long time ago, I was creating a mini ORM using reflection.

While reading about reflection I got a similar answer like this:

Java Reflection Performance

Which makes completely sense and I quit my mini orm and sharpen my CTRL+C, CTRL+V keys ( the lib was intended to avoid having to rewrite again and again the same snippets for different tables in a web app I was working on )

Years later for some reason I don't remember now ( nor want to remeber ) I was reading ( or trying to... ) the Hibernate source code, for I wanted to know if they use AOP to generate code on the fly and avoid the reflection penalty, but for my surprise, all of what I saw was pure reflection.

Does it mean the most accepted ORM framework out there, did exactly what years before discourage me from continuing my naive efforts ?? :")

My question is: Can someone confirm my understanding of the Hibernate implementation? Do they generate bytecode on the fly to improve performance? Or are we ( when we use it ) always paying the reflection penalty ( which by the way, if the diff is in some ms, none of us have noticed nor complained about )

Are we paying the reflection penalty? If we are, I think it is worth it!!!

Regards.

解决方案

Hibernate instruments your models to be hibernate aware.

There are varying levels of cost for using Reflection. Constantly looking up a method for a particular class is particularly expensive. Executing a method via reflection using a cached copy is not that much slower. If one thinks of the tasks that the reflection api must complete to invoke the method it all makes sense which each part is slow and consumes cpu cycles.

Locating a method

  • Visit each and every method of a particular class
  • Test each methods visibility, method signature etc.
  • Generate bytecode for found method.

One factors in the numbers of methods in a typical class and that some of these operations arent trivial it becomes obvious that this can be costly.

Invoking the method.

Each reflected method amounts to a bit of byte code that invokes the target method with a bit of boilerplate to match the reflection interface. Before it can do that it must perform some sanity checks so it can complain with nice messages rather than letting the runtime throw ClassCastException and similar exceptions.

  • If an instance method check the instance passed in isnt null and is the right type.
  • Check the arguments parameter includes the right amount and type of parameters.
  • Execute the method within a try catch. In the catch throw ITE etc.

All these extras add some cost - not a lot but it does make things slower.

Runtime costs

In general caching methods and invoking that isnt cost but is a bit slower. The reflection api itself does attempt to cache methods and classes but finding the right method and so on is still a slow operation.

这篇关于Hibernate的实现。我们是否支付反映罚款?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆