Why is System.arraycopy native in Java?

后端 未结 5 1570
囚心锁ツ
囚心锁ツ 2020-11-28 03:07

I was surprised to see in the Java source that System.arraycopy is a native method.

Of course the reason is because it\'s faster. But what native tricks is the code

相关标签:
5条回答
  • 2020-11-28 03:39

    There are a few reasons:

    1. The JIT is unlikely to generate as efficient low level code as a manually written C code. Using low level C can enable a lot of optimizations that are close to impossible to do for a generic JIT compiler.

      See this link for some tricks and speed comparisons of hand written C implementations (memcpy, but the principle is the same): Check this Optimizing Memcpy improves speed

    2. The C version is pretty much independant of the type and size of the array members. It is not possible to do the same in java since there is no way to get the array contents as a raw block of memory (eg. pointer).

    0 讨论(0)
  • 2020-11-28 03:44

    It can't be written in Java. Native code is able to ignore or elide the difference between arrays of Object and arrays of primitives. Java can't do that, at least not efficiently.

    And it can't be written with a single memcpy(), because of the semantics required by overlapping arrays.

    0 讨论(0)
  • 2020-11-28 03:50

    In my own tests System.arraycopy() for copying multiple dimension arrays is 10 to 20 times faster than interleaving for loops:

    float[][] foo = mLoadMillionsOfPoints(); // result is a float[1200000][9]
    float[][] fooCpy = new float[foo.length][foo[0].length];
    long lTime = System.currentTimeMillis();
    System.arraycopy(foo, 0, fooCpy, 0, foo.length);
    System.out.println("native duration: " + (System.currentTimeMillis() - lTime) + " ms");
    lTime = System.currentTimeMillis();
    
    for (int i = 0; i < foo.length; i++)
    {
        for (int j = 0; j < foo[0].length; j++)
        {
            fooCpy[i][j] = foo[i][j];
        }
    }
    System.out.println("System.arraycopy() duration: " + (System.currentTimeMillis() - lTime) + " ms");
    for (int i = 0; i < foo.length; i++)
    {
        for (int j = 0; j < foo[0].length; j++)
        {
            if (fooCpy[i][j] != foo[i][j])
            {
                System.err.println("ERROR at " + i + ", " + j);
            }
        }
    }
    

    This prints:

    System.arraycopy() duration: 1 ms
    loop duration: 16 ms
    
    0 讨论(0)
  • 2020-11-28 03:55

    In native code, it can be done with a single memcpy / memmove, as opposed to n distinct copy operations. The difference in performance is substantial.

    0 讨论(0)
  • 2020-11-28 03:55

    It is, of course, implementation dependent.

    HotSpot will treat it as an "intrinsic" and insert code at the call site. That is machine code, not slow old C code. This also means the problems with the signature of the method largely go away.

    A simple copy loop is simple enough that obvious optimisations can be applied to it. For instance loop unrolling. Exactly what happens is again implementation dependent.

    0 讨论(0)
提交回复
热议问题