使用JIT编译器的Collections.emptyList和空ArrayList的性能

use*_*676 6 java performance jit jvm-hotspot

使用Collections.emptyList()或空是否存在性能差异ArrayList,尤其是在使用JIT编译器时?

我可以想象 - 例如 - JIT编译器不执行内联或静态方法调用,因为执行的方法取决于类型.

编辑 我知道Collections.emptyList()返回一个不可变列表,ArrayList而是可变对象.

我的意思是,如果我将一个或另一个作为参数传递给方法,并且该方法不修改列表,那么是否限制了JIT编译器优化方法的可能性?

一个简单的例子(只是为了澄清我的意思):

int sum(List<Integer> list)
{
    int sum = 0;

    for(int i=0;i<list.size();++i)
      sum += list.get(i);

    return sum;
}
Run Code Online (Sandbox Code Playgroud)

如果我只使用ArrayListJIT编译器调用此方法可以内联ArrayList.get().如果我也用Collections.empty()它打电话是不可能的.

那是对的吗?

Iva*_*tov 6

Disclamer

以下所述内容仅适用于HotSpot JVM.

简短的答案

JIT编译器不执行内联或静态方法调用,因为执行的方法取决于类型.

这与事实相反.看我的回答.

使用Collections.emptyList()或空ArrayList之间是否存在性能差异,尤其是在使用JIT编译器时?

在极少数情况下 - 是的.请参阅microbenchmark结果.

如果我只使用ArrayList调用此方法,则JIT编译器可以内联ArrayList.get().如果我也使用Collections.empty()进行调用,那将是不可能的.那是对的吗?

简短的回答 - 这取决于.JIT编译器足够智能,可识别单形,双态和多态调用模式,并提供适当的实现.

回答

为了得到详细的答案,我建议阅读以下关于方法调度的黑魔法的帖子.用几句话说

C2根据观察到的类型配置文件进行有趣的配置文件引导优化.如果只有一个接收器类型(即,呼叫站点是单态的),它可以简单地检查预测的类型,并直接内联目标.如果观察到两种接收器类型(即,呼叫站点是双态的),则可以并且将应用相同的优化,代价是两个分支.

让我们考虑以下JMH示例(如果您还没有了解JMH,那么我建议在这里阅读它).

@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(value = 5)
public class ExampleBench {

    @Param("10000")
    private int count;

    List<Integer>[] arrays;
    List<Integer>[] empty;
    List<Integer>[] bimorphic;
    List<Integer>[] polimorphic;

    @Setup
    public void setup(){
        Random r = new Random(0xBAD_BEEF);
        arrays = new List[count];
        empty = new List[count];
        bimorphic = new List[count];
        polimorphic = new List[count];
        for (int i = 0; i < arrays.length; i++) {
            bimorphic[i] = r.nextBoolean() ? new ArrayList<Integer>(0) : Collections.<Integer>emptyList();
            int i1 = r.nextInt(3);
            switch (i1) {
                case 0 : polimorphic[i] = new ArrayList<>(0);
                    break;
                case 1 : polimorphic[i] = new LinkedList<>();
                    break;
                case 2 : polimorphic[i] = Collections.emptyList();
                    break;
            }
            arrays[i] = new ArrayList<>(0);
            empty[i] = Collections.emptyList();
        }
    }

    @Benchmark
    public float arrayList() {
        List<Integer>[] l = arrays;
        int c = count;
        float result = 0;
        for (int i = 0; i < c; i++) {
            result += sum(l[i]);
        }
        return result;
    }

    @Benchmark
    public float emptyList() {
        List<Integer>[] l = empty;
        int c = count;
        float result = 0;
        for (int i = 0; i < c; i++) {
            result += sum(l[i]);
        }
        return result;
    }

    @Benchmark
    public float biList() {
        List<Integer>[] l = bimorphic;
        int c = count;
        float result = 0;
        for (int i = 0; i < c; i++) {
            result += sum(l[i]);
        }
        return result;
    }

    @Benchmark
    public float polyList() {
        List<Integer>[] l = polimorphic;
        int c = count;
        float result = 0;
        for (int i = 0; i < c; i++) {
            result += sum(l[i]);
        }
        return result;
    }

    int sum(List<Integer> list) {
        int sum = 0;
        for (int i = 0; i < list.size(); ++i) {
            sum += list.get(i);
        }
        return sum;
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是:

Benchmark               (count)  Mode  Cnt       Score       Error  Units
ExampleBench.arrayList    10000  avgt    5   22902.547 ± 27665.651  ns/op
ExampleBench.biList       10000  avgt    5   50459.552 ±   739.379  ns/op
ExampleBench.emptyList    10000  avgt    5    3745.469 ±   211.794  ns/op
ExampleBench.polyList     10000  avgt    5  164879.943 ±  5830.008  ns/op
Run Code Online (Sandbox Code Playgroud)

在单态和双态调用的情况下,JIT通过具体实现替换虚拟调用.例如,如果arrayList()我们有以下输出-XX:+PrintInlining:

@ 27   edu.jvm.runtime.ExampleBench::sum (38 bytes)   inline (hot)
   @ 6   java.util.ArrayList::size (5 bytes)   accessor
    \-> TypeProfile (15648/15648 counts) = java/util/ArrayList
Run Code Online (Sandbox Code Playgroud)

用于emptyList():

@ 27   edu.jvm.runtime.ExampleBench::sum (38 bytes)   inline (hot)
   @ 6   java.util.Collections$EmptyList::size (2 bytes)   inline (hot)
    \-> TypeProfile (9913/9913 counts) = java/util/Collections$EmptyList
Run Code Online (Sandbox Code Playgroud)

用于biList():

@ 27   edu.jvm.runtime.ExampleBench::sum (38 bytes)   inline (hot)
   @ 6   java.util.Collections$EmptyList::size (2 bytes)   inline (hot)
   @ 6   java.util.ArrayList::size (5 bytes)   accessor
    \-> TypeProfile (2513/5120 counts) = java/util/ArrayList
    \-> TypeProfile (2607/5120 counts) = java/util/Collections$EmptyList
Run Code Online (Sandbox Code Playgroud)

如果polyList()JIT没有内联任何实现并使用真正的虚拟调用.

在这些方法中使用内联函数有什么好处?让我们看看编译器生成的代码arrayList():

0x00007ff9e51bce50: cmp $0xf80036dc,%r10d     ;instance of 'java/util/ArrayList'
0x00007ff9e51bce57: jne L0000                 ;if false go to L0000 (invokeinterface size)
0x00007ff9e51bce59: mov 0x10(%rdx),%ebp       ;*getfield size optimization java.util.ArrayList::size@1 

.....

0x00007ff9e51bce6d: retq
             L0000: mov $0xffffffde,%esi      ; true virtual call starts here
0x00007ff9e51bce73: mov %rdx,(%rsp)
0x00007ff9e51bce77: callq 0x00007ff9e50051a0  ; OopMap{[0]=Oop off=92}
                                              ;*invokeinterface size
                                              ; - edu.jvm.runtime.ExampleBench::sum@6 (line 119)
                                              ;   {runtime_call}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,JIT取代了虚拟呼叫getfield.