Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenJDK implementation of System.arraycopy
    primarykey
    data
    text
    <p>Following a question related to the way the JVM implements creation of Strings based on char[], I have mentioned that no iteration takes place when the char[] gets copied to the interior of the new string, since System.arraycopy gets called eventually, which copies the desired memory using a function such as memcpy at a native, implementation-dependent level (<a href="https://stackoverflow.com/questions/11208444/java-public-stringchar-value">the original question</a>).</p> <p>I wanted to check that for myself, so I downloaded the Openjdk 7 source code and started browsing it. I found the implementation of System.arraycopy in the OpenJDK C++ source code, in <code>openjdx/hotspot/src/share/vm/oops/objArrayKlass.cpp</code>:</p> <pre><code>if (stype == bound || Klass::cast(stype)-&gt;is_subtype_of(bound)) { // elements are guaranteed to be subtypes, so no check necessary bs-&gt;write_ref_array_pre(dst, length); Copy::conjoint_oops_atomic(src, dst, length); } else { // slow case: need individual subtype checks </code></pre> <p>If the elements need no type checks (that's the case with, for instance, primitive data type arrays), Copy::conjoin_oops_atomic gets called.</p> <p>The <code>Copy::conjoint_oops_atomic</code> function resides in 'copy.hpp':</p> <pre><code>// overloaded for UseCompressedOops static void conjoint_oops_atomic(narrowOop* from, narrowOop* to, size_t count) { assert(sizeof(narrowOop) == sizeof(jint), "this cast is wrong"); assert_params_ok(from, to, LogBytesPerInt); pd_conjoint_jints_atomic((jint*)from, (jint*)to, count); } </code></pre> <p>Now we're platform dependent, as the copy operation has a different implementation, based on OS/architecture. I'll go with Windows as an example. <code>openjdk\hotspot\src\os_cpu\windows_x86\vm\copy_windows_x86.inline.hpp</code>:</p> <pre><code>static void pd_conjoint_oops_atomic(oop* from, oop* to, size_t count) { // Do better than this: inline memmove body NEEDS CLEANUP if (from &gt; to) { while (count-- &gt; 0) { // Copy forwards *to++ = *from++; } } else { from += count - 1; to += count - 1; while (count-- &gt; 0) { // Copy backwards *to-- = *from--; } } } </code></pre> <p>And... to my surprise, it iterates through the elements (the oop values), copying them one by one (seemingly). Can someone explain why the copy is done, even at the native level, by iterating through the elements in the array?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload