Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 440
bmargulies Avatar asked Feb 25 '23 01:02

bmargulies


1 Answers

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.

like image 59
Jérôme Verstrynge Avatar answered Mar 06 '23 23:03

Jérôme Verstrynge