内存复用场景下数组右移:System.arraycopy与自定义循环性能对比咨询
Great question—let’s break down the two approaches and address your concerns about System.arraycopy head-on, since this is a common optimization point in high-throughput systems.
Your custom shiftRight method iterates from the end of the target range backward, copying bytes[index - length] to bytes[index]—this avoids overwriting elements that haven’t been processed yet. The System.arraycopy call you found (System.arraycopy(bytes, 0, bytes, length, size)) achieves the exact same result: it copies the size elements starting at index 0 to the position starting at length, aligning perfectly with your manual shift logic.
System.arraycopy is mostly unfounded While some generic documentation mentions temporary arrays for overlapping copies, modern JVMs (like HotSpot, the backbone of most production Java systems) have heavily optimized this scenario. When copying within the same array where the source start position is less than the destination start position (your right-shift case), the JVM automatically uses a backward copy order—exactly the same logic as your custom loop! No temporary array is created at all.
This optimization is baked into the native implementation of System.arraycopy: it checks the relative positions of srcPos and destPos, then chooses the safest, fastest copy direction to avoid overwrites without extra memory allocations.
System.arraycopy blows the custom loop out of the water Let’s break down the overhead differences:
Custom loop: This runs in Java bytecode, with per-iteration overhead including:
- Loop boundary checks (
index >= length) - Array access boundary checks (JVM may optimize these away, but not consistently)
- Two separate array lookups (
bytes[index - length]andbytes[index]) - Explicit assignment operations
For largesizevalues, these small costs add up to significant latency.
- Loop boundary checks (
System.arraycopy: This is a native method that leverages hardware-level memory copy instructions (like x86’s
movsb/movsdor ARM’s batch load/store commands). These instructions copy blocks of memory directly, bypassing most Java-level overhead. For arrays with hundreds or thousands of elements,System.arraycopycan be 5-20x faster than a manual loop.
If you want to verify this, run a quick JMH benchmark with varying size values—you’ll see the performance gap grow as the array gets larger.
Your custom loop requires readers to parse why you’re iterating backward (to avoid overwrites), which adds cognitive load. System.arraycopy is a standard JDK method; any experienced Java developer will immediately recognize its purpose from the parameters. Plus, System.arraycopy has been battle-tested for decades—there’s almost no risk of bugs, whereas a manual loop could break if someone accidentally changes the iteration direction.
Ditch the custom shiftRight method and use System.arraycopy confidently:
- It won’t create temporary arrays in modern JVMs
- It’s drastically faster for large arrays
- It’s cleaner and less error-prone
内容的提问来源于stack exchange,提问作者Hossein Mobasher

