为什么JNI调用本机方法的速度要比sun.misc.Unsafe中的类似方法慢? [英] Why are JNI calls to native methods slower than similar methods in sun.misc.Unsafe?

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

问题描述

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

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

为什么从sun.misc.Unsafe和我开发的库中调用本机方法的时间截然不同?

Why are the call times of native methods from sun.misc.Unsafe and from my developed library extremely different?

一些数字:
sun.misc.Unsafe.getInt(address)大约需要1ns
当我的类似方法大约需要10ns时

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

两个实现完全相同,遵循OpenJDK的源代码,只是通过指针返回变量.两者都以相同的方式注册.

Both implementations are quite the same, following the source code of OpenJDK, just returning the variable by pointer. Both are registered in the same manner.

如何加快JNI通话速度?是什么使不安全性能如此特别?

How can I speed up JNI calls? What makes Unsafe performance so special?

谢谢,
尤里/

Thanks,
Yury/

推荐答案

如果您在Unsafe类中查找本机方法的源,您会发现它不使用JNI实现.而是将不安全的方法作为机器代码内联.例如,getInt(long)成为一条机器代码指令.

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

由于这个原因,您不能编写不像使用Unsafe一样快的JNI方法,而无需更改JVM以便内联机器代码.

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

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

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