java中的动态调用特性

java中的动态调用特性,java,Java,如果我们使用动态调用,应用程序中是否有任何性能改进?如果不是,它的优点是什么?我想你指的是动态调用的反射。反射的成本比普通的方法调用稍微高一点 反射有很多用途,例如我广泛地将其用于ui数据绑定 我猜OP的意思是InvokeDynamic,正如这里提到的方法调用。我从未使用过自己,但它引用了 “性能差异对于库执行一些反射调用可能并不重要,特别是如果这些调用主要是在内存中动态设置一个静态结构,它可以根据该结构进行正常调用。但是在动态语言中,每个调用都必须使用这些机制,这会严重影响性能。”所以最后的答

如果我们使用
动态调用
,应用程序中是否有任何性能改进?如果不是,它的优点是什么?

我想你指的是动态调用的反射。反射的成本比普通的方法调用稍微高一点


反射有很多用途,例如我广泛地将其用于ui数据绑定

我猜OP的意思是InvokeDynamic,正如这里提到的方法调用。我从未使用过自己,但它引用了


“性能差异对于库执行一些反射调用可能并不重要,特别是如果这些调用主要是在内存中动态设置一个静态结构,它可以根据该结构进行正常调用。但是在动态语言中,每个调用都必须使用这些机制,这会严重影响性能。”所以最后的答案是:

使用callsites调用dynamic比传统方法调用更快

字段访问的唯一更快的方法是直接访问字段,而不需要任何方法调用和使用不安全的方法

答案是不,调用动态并不慢。它很快。它非常快

更新:

另一个更新(周日晚些时候) 我将代码更改为使用callsite进行动态调用,这大大改变了时间

(所有使用JDK 1.8 build 94运行)

1000次跑步

description                                 duration in nanoseconds
regular method call time                    2095
invoke dynamic method call time             1098
reflection method call time                 3104

field method invoke dynamic call time       1165
field method invoke reflection call time    689
unsafe field access time                    94
direct field access (baseline)              92
description                               duration in nanoseconds
regular method call time                  68
invoke dynamic method call time           43
reflection method call time               202

field method invoke dynamic call time     42
field method invoke reflection call time  45
unsafe field access time                  87
direct                                    86
description                                 duration in nanoseconds
regular method call time                    70
invoke dynamic method call time             44
reflection method call time                 249

field method invoke dynamic call time       45
field method invoke reflection call time    47
unsafe field access time                    88
direct                                      36
description                                 duration in nanoseconds
regular method call time                    11
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 25

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
regular method call time        = 103
invoke dynamic method call time = 116
reflection method call time     = 252
regular method call time        = 23
invoke dynamic method call time = 35
reflection method call time     = 30
regular method call time        = 34
invoke dynamic method call time = 24
reflection method call time     = 43
10000次跑步

description                                 duration in nanoseconds
regular method call time                    2095
invoke dynamic method call time             1098
reflection method call time                 3104

field method invoke dynamic call time       1165
field method invoke reflection call time    689
unsafe field access time                    94
direct field access (baseline)              92
description                               duration in nanoseconds
regular method call time                  68
invoke dynamic method call time           43
reflection method call time               202

field method invoke dynamic call time     42
field method invoke reflection call time  45
unsafe field access time                  87
direct                                    86
description                                 duration in nanoseconds
regular method call time                    70
invoke dynamic method call time             44
reflection method call time                 249

field method invoke dynamic call time       45
field method invoke reflection call time    47
unsafe field access time                    88
direct                                      36
description                                 duration in nanoseconds
regular method call time                    11
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 25

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
regular method call time        = 103
invoke dynamic method call time = 116
reflection method call time     = 252
regular method call time        = 23
invoke dynamic method call time = 35
reflection method call time     = 30
regular method call time        = 34
invoke dynamic method call time = 24
reflection method call time     = 43
100000次跑步

description                                 duration in nanoseconds
regular method call time                    2095
invoke dynamic method call time             1098
reflection method call time                 3104

field method invoke dynamic call time       1165
field method invoke reflection call time    689
unsafe field access time                    94
direct field access (baseline)              92
description                               duration in nanoseconds
regular method call time                  68
invoke dynamic method call time           43
reflection method call time               202

field method invoke dynamic call time     42
field method invoke reflection call time  45
unsafe field access time                  87
direct                                    86
description                                 duration in nanoseconds
regular method call time                    70
invoke dynamic method call time             44
reflection method call time                 249

field method invoke dynamic call time       45
field method invoke reflection call time    47
unsafe field access time                    88
direct                                      36
description                                 duration in nanoseconds
regular method call time                    11
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 25

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
regular method call time        = 103
invoke dynamic method call time = 116
reflection method call time     = 252
regular method call time        = 23
invoke dynamic method call time = 35
reflection method call time     = 30
regular method call time        = 34
invoke dynamic method call time = 24
reflection method call time     = 43
1000000次跑步

description                                 duration in nanoseconds
regular method call time                    2095
invoke dynamic method call time             1098
reflection method call time                 3104

field method invoke dynamic call time       1165
field method invoke reflection call time    689
unsafe field access time                    94
direct field access (baseline)              92
description                               duration in nanoseconds
regular method call time                  68
invoke dynamic method call time           43
reflection method call time               202

field method invoke dynamic call time     42
field method invoke reflection call time  45
unsafe field access time                  87
direct                                    86
description                                 duration in nanoseconds
regular method call time                    70
invoke dynamic method call time             44
reflection method call time                 249

field method invoke dynamic call time       45
field method invoke reflection call time    47
unsafe field access time                    88
direct                                      36
description                                 duration in nanoseconds
regular method call time                    11
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 25

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
regular method call time        = 103
invoke dynamic method call time = 116
reflection method call time     = 252
regular method call time        = 23
invoke dynamic method call time = 35
reflection method call time     = 30
regular method call time        = 34
invoke dynamic method call time = 24
reflection method call time     = 43
10000000次跑步

description                                 duration in nanoseconds
regular method call time                    2095
invoke dynamic method call time             1098
reflection method call time                 3104

field method invoke dynamic call time       1165
field method invoke reflection call time    689
unsafe field access time                    94
direct field access (baseline)              92
description                               duration in nanoseconds
regular method call time                  68
invoke dynamic method call time           43
reflection method call time               202

field method invoke dynamic call time     42
field method invoke reflection call time  45
unsafe field access time                  87
direct                                    86
description                                 duration in nanoseconds
regular method call time                    70
invoke dynamic method call time             44
reflection method call time                 249

field method invoke dynamic call time       45
field method invoke reflection call time    47
unsafe field access time                    88
direct                                      36
description                                 duration in nanoseconds
regular method call time                    11
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 25

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
regular method call time        = 103
invoke dynamic method call time = 116
reflection method call time     = 252
regular method call time        = 23
invoke dynamic method call time = 35
reflection method call time     = 30
regular method call time        = 34
invoke dynamic method call time = 24
reflection method call time     = 43
100000000次跑步

description                                 duration in nanoseconds
regular method call time                    2095
invoke dynamic method call time             1098
reflection method call time                 3104

field method invoke dynamic call time       1165
field method invoke reflection call time    689
unsafe field access time                    94
direct field access (baseline)              92
description                               duration in nanoseconds
regular method call time                  68
invoke dynamic method call time           43
reflection method call time               202

field method invoke dynamic call time     42
field method invoke reflection call time  45
unsafe field access time                  87
direct                                    86
description                                 duration in nanoseconds
regular method call time                    70
invoke dynamic method call time             44
reflection method call time                 249

field method invoke dynamic call time       45
field method invoke reflection call time    47
unsafe field access time                    88
direct                                      36
description                                 duration in nanoseconds
regular method call time                    11
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 25

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
regular method call time        = 103
invoke dynamic method call time = 116
reflection method call time     = 252
regular method call time        = 23
invoke dynamic method call time = 35
reflection method call time     = 30
regular method call time        = 34
invoke dynamic method call time = 24
reflection method call time     = 43
更新了使用调用站点和调用动态的代码

//fieldName is the reflection field (example below how to look it up and change its access)
MethodHandle methodHandleFieldDirect = lookup.unreflectGetter(fieldName);
CallSite callSiteField = new ConstantCallSite(methodHandleFieldDirect);
methodHandleFieldDirect = callSiteField.dynamicInvoker();
name = (String) methodHandleFieldDirect.invokeExact(new Employee());


//Lookup invoke dynamic
methodType = MethodType.methodType(String.class);
methodHandle = lookup.findVirtual(Employee.class, "getName", methodType);
CallSite callSiteMethod = new ConstantCallSite(methodHandleFieldDirect);
methodHandle = callSiteMethod.dynamicInvoker();
要查看其余内容,您必须查看博客条目。有使用不安全、反射、调用站点、调用动态等的示例。:)

调用站点对于加速动态调用非常重要

更新(较旧的更新):

我取出了hashCode和count代码,添加它们是因为反射速度太快,以至于我觉得这些循环不知何故都被抖动了:

已删除count/hashcode employee.getName()的1000万次运行
  • 常规方法调用时间=25纳秒
  • 调用动态方法调用时间=18纳秒**
  • 反射方法调用时间=36纳秒
  • field方法调用动态调用时间=8纳秒
  • 字段方法反射调用时间=6纳秒
已删除count/hashcode employee.getName()的10000次运行
  • 常规方法调用时间=70纳秒**
  • 调用动态方法调用时间=84纳秒
  • 反射方法调用时间=211纳秒
  • field方法调用动态调用时间=153纳秒
  • 字段方法反射调用时间=40纳秒
与反射相比,性能有所提高,但这很复杂。这取决于你打了多少次电话。您可能只关心代码是否处于紧密循环中

我最近看到的基准测试显示,与普通反射相比,性能提高了15倍,仅比以正常方式调用方法慢2.5倍。但是你知道一句老话,什么都不相信,只相信你读到的一半

我想我会试试看

我现在一直在玩弄反射和调用动态。 看见

下面是我使用JDK1.8Build94得到的结果

一百万次呼叫(结果以纳秒为单位):

10000次呼叫

description                                 duration in nanoseconds
regular method call time                    2095
invoke dynamic method call time             1098
reflection method call time                 3104

field method invoke dynamic call time       1165
field method invoke reflection call time    689
unsafe field access time                    94
direct field access (baseline)              92
description                               duration in nanoseconds
regular method call time                  68
invoke dynamic method call time           43
reflection method call time               202

field method invoke dynamic call time     42
field method invoke reflection call time  45
unsafe field access time                  87
direct                                    86
description                                 duration in nanoseconds
regular method call time                    70
invoke dynamic method call time             44
reflection method call time                 249

field method invoke dynamic call time       45
field method invoke reflection call time    47
unsafe field access time                    88
direct                                      36
description                                 duration in nanoseconds
regular method call time                    11
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 25

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
regular method call time        = 103
invoke dynamic method call time = 116
reflection method call time     = 252
regular method call time        = 23
invoke dynamic method call time = 35
reflection method call time     = 30
regular method call time        = 34
invoke dynamic method call time = 24
reflection method call time     = 43
100000次呼叫(热身后)。

regular method call time        = 46
invoke dynamic method call time = 112
reflection method call time     = 171
1000000次呼叫

description                                 duration in nanoseconds
regular method call time                    2095
invoke dynamic method call time             1098
reflection method call time                 3104

field method invoke dynamic call time       1165
field method invoke reflection call time    689
unsafe field access time                    94
direct field access (baseline)              92
description                               duration in nanoseconds
regular method call time                  68
invoke dynamic method call time           43
reflection method call time               202

field method invoke dynamic call time     42
field method invoke reflection call time  45
unsafe field access time                  87
direct                                    86
description                                 duration in nanoseconds
regular method call time                    70
invoke dynamic method call time             44
reflection method call time                 249

field method invoke dynamic call time       45
field method invoke reflection call time    47
unsafe field access time                    88
direct                                      36
description                                 duration in nanoseconds
regular method call time                    11
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 25

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
regular method call time        = 103
invoke dynamic method call time = 116
reflection method call time     = 252
regular method call time        = 23
invoke dynamic method call time = 35
reflection method call time     = 30
regular method call time        = 34
invoke dynamic method call time = 24
reflection method call time     = 43
在1M处反射比调用动态更快。嗯。。。奇怪

10000000次呼叫

description                                 duration in nanoseconds
regular method call time                    2095
invoke dynamic method call time             1098
reflection method call time                 3104

field method invoke dynamic call time       1165
field method invoke reflection call time    689
unsafe field access time                    94
direct field access (baseline)              92
description                               duration in nanoseconds
regular method call time                  68
invoke dynamic method call time           43
reflection method call time               202

field method invoke dynamic call time     42
field method invoke reflection call time  45
unsafe field access time                  87
direct                                    86
description                                 duration in nanoseconds
regular method call time                    70
invoke dynamic method call time             44
reflection method call time                 249

field method invoke dynamic call time       45
field method invoke reflection call time    47
unsafe field access time                    88
direct                                      36
description                                 duration in nanoseconds
regular method call time                    11
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 25

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
description                                 duration in nanoseconds
regular method call time                    9
invoke dynamic method call time             6
reflection method call time                 12

field method invoke dynamic call time       6
field method invoke reflection call time    4
unsafe field access time                    1
direct                                      0
regular method call time        = 103
invoke dynamic method call time = 116
reflection method call time     = 252
regular method call time        = 23
invoke dynamic method call time = 35
reflection method call time     = 30
regular method call time        = 34
invoke dynamic method call time = 24
reflection method call time     = 43
现在调用dynamic比常规方法调用更快

现在是100000000

regular method call time        = 22
invoke dynamic method call time = 24
reflection method call time     = 28
regular method call time                  = 109
invoke dynamic method call time           = 115
reflection method call time               = 236
field method invoke dynamic call time     = 178
field method reflection call time         = 709
regular method call time                  = 49
invoke dynamic method call time           = 118
reflection method call time               = 312
field method invoke dynamic call time     = 75
field method reflection call time         = 158
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 40
invoke dynamic method call time           = 25
reflection method call time               = 44
field method invoke dynamic call time     = 10
field method reflection call time         = 9
此时,JIT编译器消除了所有的痛苦。 如果你不能抽出额外的2到6纳秒,那么你需要一些应对技能

以下是重新创建测试的代码(也请遵循上面的链接):

创建一个employee对象

    Employee employee = new Employee();
使用invoke dynamic查找getName(更多示例请参见博客,上面的链接)

使用反射查找Employee.getName

    //Lookup reflection
    Method method = Employee.class.getMethod("getName", new Class<?>[]{});
    name = (String) method.invoke(new Employee());
    System.out.println("reflection " + name);
现在让我们预热JVM

    //warm up
    for (int index =0 ; index < times; index++) {
        employee.getName();
        name = (String) methodHandle.invokeExact(employee);
        name = (String) method.invoke(employee);
    }
100\u 000

regular method call time        = 22
invoke dynamic method call time = 24
reflection method call time     = 28
regular method call time                  = 109
invoke dynamic method call time           = 115
reflection method call time               = 236
field method invoke dynamic call time     = 178
field method reflection call time         = 709
regular method call time                  = 49
invoke dynamic method call time           = 118
reflection method call time               = 312
field method invoke dynamic call time     = 75
field method reflection call time         = 158
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 40
invoke dynamic method call time           = 25
reflection method call time               = 44
field method invoke dynamic call time     = 10
field method reflection call time         = 9
1\u 000\u 000

regular method call time        = 22
invoke dynamic method call time = 24
reflection method call time     = 28
regular method call time                  = 109
invoke dynamic method call time           = 115
reflection method call time               = 236
field method invoke dynamic call time     = 178
field method reflection call time         = 709
regular method call time                  = 49
invoke dynamic method call time           = 118
reflection method call time               = 312
field method invoke dynamic call time     = 75
field method reflection call time         = 158
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 40
invoke dynamic method call time           = 25
reflection method call time               = 44
field method invoke dynamic call time     = 10
field method reflection call time         = 9
10\u 000\u 000

regular method call time        = 22
invoke dynamic method call time = 24
reflection method call time     = 28
regular method call time                  = 109
invoke dynamic method call time           = 115
reflection method call time               = 236
field method invoke dynamic call time     = 178
field method reflection call time         = 709
regular method call time                  = 49
invoke dynamic method call time           = 118
reflection method call time               = 312
field method invoke dynamic call time     = 75
field method reflection call time         = 158
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 40
invoke dynamic method call time           = 25
reflection method call time               = 44
field method invoke dynamic call time     = 10
field method reflection call time         = 9
100\u 000\u 000

regular method call time        = 22
invoke dynamic method call time = 24
reflection method call time     = 28
regular method call time                  = 109
invoke dynamic method call time           = 115
reflection method call time               = 236
field method invoke dynamic call time     = 178
field method reflection call time         = 709
regular method call time                  = 49
invoke dynamic method call time           = 118
reflection method call time               = 312
field method invoke dynamic call time     = 75
field method reflection call time         = 158
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 28
invoke dynamic method call time           = 41
reflection method call time               = 30
field method invoke dynamic call time     = 11
field method reflection call time         = 18
regular method call time                  = 40
invoke dynamic method call time           = 25
reflection method call time               = 44
field method invoke dynamic call time     = 10
field method reflection call time         = 9
好的,下面是字段访问的代码,它比使用employee.getName()快4倍

long start=0;
长端=0;
长时间=10_000_000;
长期定期;
调用时间长;
反射时间长;
长调用的DynamicTimeUsingField;
长场直达;
长计数=0;
//热身
for(int index=0;index