开发者

Cloning an int[] -- anyone got a faster suggestion?

开发者 https://www.devze.com 2023-03-09 02:50 出处:网络
I\'m staring at a profile where one of the CPU hot-spots is a function that consists of cloning a final static int[]. You might ask, \'Why?\' the callers use the results as the starting points of a ha

I'm staring at a profile where one of the CPU hot-spots is a function that consists of cloning a final static int[]. You might ask, 'Why?' the callers use the results as the starting points of a hashing process.

In other words, the code needs to do (logically):

  1. make a new array
  2. get the hash seeds (as many as the size of the array)
  3. put values in the new array calculated from the hash seeds. That's an iterative algorithm, so it's advantageous to have the seeds start out in the array -- and thus the idea of starting with a clone of the array of seeds.

Before开发者_运维百科 I either give up or start wrting microbenchmarks, I'm posting this question in case anyone has any special knowledge of the what's under the hood with clone() versus Arrays.copyOf versus just new and an arraycopy.


Arrays.copyOf takes advantage of memcopy like instructions under the hood. Moreover, there are less index-out-of-bound checks. If you assign values one-by-one, there is a 'bound' check each time.

I have not done the benchmarking for each suggested options, but I am sure that Arrays.copyOf() is going to be faster than new int[] + for(...).

On the other side, I am not sure that Arrays.copyOf() is going to be more efficient than int[] myCopy = myOrig.clone(), since the cloning happens on a primitive type array. There is a high probability that the compiler will generate optimized bytecode. Only tests will provide a definitive answer.


Arrays.copyOf uses System.arraycopy but adds some boundchecks first

public static int[] copyOf(int[] original,
    int newLength) 
{
        if (0 < = newLength) {
            return copyOfRange(original, 0, newLength);
        }
        throw new NegativeArraySizeException();
}


public static int[] copyOfRange(int[] original,
    int start,
    int end) 
{
        if (start < = end) {
            if (original.length  >= start && 0 < = start) {
                int length = end - start;
                int copyLength = Math.min(length, original.length - start);
                int[] copy = new int[length];
                System.arraycopy(original, start, copy, 0, copyLength);
                return copy;
            }
            throw new ArrayIndexOutOfBoundsException();
        }
        throw new IllegalArgumentException();
}

I got this from http://www.docjar.com/docs/api/java/util/Arrays.html so it might be marginally better to use this than the Arrays.copyOf

int[] newone = new int[orig.length];
System.arraycopy(orig, 0, newone , 0, orig.length);

though really it might be best to keep the arrays and reuse them after they are done with them


If the code belongs to you, how about the following:

public final class ReadOnlyIntArray {
  private final int[] _contents;

  public ReadOnlyIntArray(int[] data) {
    _contents = (int[]) data.clone();
  }

  public int get(int index) {
    return _contents[index];
  }
}

Keep the final static content in one of these, and return this object instead of of the int[]. Force the consumer to clone if they do anything destructive.

I've often felt that this construct should have been one of the core constructs in java.lang, which would have allowed code that returns Class[], Method[] etc to not have to clone every time.


Using clone() on arrays of primitive types is about 50% slower than a new [] and System.arraycopy combination with the Java 1.6/1.7 x86 client VM but is about the same speed with the x64 server VM.

For array sizes of a few 1000 elements or less it is not the copying that is the most expensive part of the operation but the creation of the array itself (i.e. the creation of a 2048 elements byte array takes 3 times longer than to copy 2048 elements into is with System.arraycopy). So you could gain some improvement if you you recycle the temporary arrays.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号