为什么JNI调用本机方法比sun.misc.Unsafe中的类似慢 [英] Why JNI call to native method is slower than similar in sun.misc.Unsafe

查看:302
本文介绍了为什么JNI调用本机方法比sun.misc.Unsafe中的类似慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发类似于sun.misc.Unsafe的JNI实现,但具有扩展的内存管理。

I'm developing JNI implementation similar to sun.misc.Unsafe but with extended memory management.

如何从sun.misc.Unsafe和我开发的库中解释本机方法的调用时间是非常不同的?

How to explain that call time of native methods from sun.misc.Unsafe and from my developed library is extremely different?

一些数字:
sun.misc.Unsafe.getInt(地址)需要〜1ns
,当我的类似方法需要~10ns

Some numbers: sun.misc.Unsafe.getInt(address) takes ~1ns when my similar method takes ~10ns

两个实现完全相同,遵循openJDK的源代码,只是通过指针返回变量。在JNI模块中不安全,以与其他模式相同的方式注册。

Both implementations are quite the same, following source code of openJDK, just return variable by pointer. Unsafe in JNI module, registered in the same manner as other.

如何加速JNI呼叫?是什么让Unsafe对性能如此特别?

How to speed up JNI call? What makes Unsafe so special for performance?

谢谢,
Yury /

Thanks, Yury/

推荐答案

如果您在Unsafe类中查找本机方法的源代码,您会发现没有JNI实现。相反,不安全方法被内联为机器代码。 getInt(long)例如成为一个机器代码指令。

If you look for the source of the native methods in the Unsafe class you will find there is not JNI implementations. Instead, Unsafe methods are inlined as machine code. getInt(long) for example becomes one machine code instruction.

因此,你不能写一个JNI方法,与使用Unsafe一样快,不需要更改JVM,因此它可以内联机器代码。

For this reason, you can't write a JNI method which is as fast as using Unsafe, without change the JVM so it inlines the machine code.

这篇关于为什么JNI调用本机方法比sun.misc.Unsafe中的类似慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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