views:

541

answers:

5

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 able to employ that make it faster?

Why not just loop over the original array and copy each pointer to the new array - surely this isn't that slow and cumbersome?

+9  A: 

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.

Péter Török
@Peter, so within native code you can fettle with the Java memory model? (I've never had any cause to do any native malarkey)
James B
@James B, I am not an expert on this, but in native code you surely are not constrained with the Java memory model or anything - you can fiddle with the raw bits any way you like (at your own risk, of course).
Péter Török
Actually, only some subcases of `arraycopy` could be implemented using `memcpy` / `memmove`. Others require a runtime type check for each element copied.
Stephen C
@Stephen C, interesting - why is that?
Péter Török
@Péter Török - consider copying from an `Object[]` populated with `String` objects to a `String[]`. See last paragraph of http://java.sun.com/javase/6/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int)
Stephen C
@Stephen C, thanks. Should've thought before asking :-)
Péter Török
+5  A: 

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.

EJP
Fine, so `memmove` then. Although I don't think it makes much difference in context of this question.
Péter Török
+1  A: 

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): http://www.embedded.com/columns/technicalinsights/19205567?_requestid=457721

  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).

Hrvoje Prgeša
Java code can get optimised. In fact what actually happens is machine code is generated which is more efficient than the C.
Tom Hawtin - tackline
I agree that sometimes JITed code will be better localy optimised since it knows which processor it is runnimg on. However, since it is "just in time" it will never be able to use all those non-local optimisations that take longer to execute. Also, it will never be able to match the hand crafted C code (which could also take the processor in account and partially negate the JIT advantages, either by compilng for a specific processor or by some kind of runtime check).
Hrvoje Prgeša
@Hrvoje - I think that the Sun JIT compiler team would dispute many of those points. For instance, I believe that HotSpot does global optimization to remove unnecessary method dispatching, and there's no reason why a JIT cannot generate processor specific code. Then there is the point that a JIT compiler can do branch optimization based on the execution behavior of the current application run.
Stephen C
@Stephen C - excelent point about the branch optimisations, aldough you could also perform static performance profiling with C/C++ compilers to achieve the similar effect.I also think that the hotspot has 2 modes of operation - desktop applications will not use all of the available optimizations to achieve a reasonable startup time, while the server applications will be optimized more aggressively. All in all, you get some advantages, but you also loose some.
Hrvoje Prgeša
+2  A: 

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.

Tom Hawtin - tackline
+2  A: 

My take is that reason that System.arraycopy is native at least partly historical.

In the early days when Java was only interpreted, a native code implementation of arraycopy would have been substantially faster than an equivalent loop written in Java. The arraycopy method would have been essential in making (for example) I/O and graphics performance bearable.

Of course, things have changed. Java is now JIT compiled, and the native code compiler can do all sorts of clever things to make simple loops go fast. So it is not obvious that a native implementation of arraycopy makes a significant difference, "intrinsic" treatment not-withstanding. However, the name, signature and semantics of arraycopy are so entrenched that they are likely to remain unchanged for as long as Java (as we know it) is used.

Stephen C