Skip to content

Commit

Permalink
Revert "Standardize on Array copyOf" (#9400)
Browse files Browse the repository at this point in the history
This reverts commit 935d099 from PR #9162.

While the original commit was a nice simplification, I learned from
another Googler that there is unfortunately a performance cost to this
(or at least there was last time this change was attempted). Even if it
turns out to be fast on modern Java runtimes, we still care about the
performance on old Android devices.
  • Loading branch information
acozzette committed Jan 11, 2022
1 parent 3d995aa commit 49b184b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/BooleanArrayList.java
Expand Up @@ -197,7 +197,10 @@ public void addBoolean(boolean element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
boolean[] newArray = new boolean[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -216,10 +219,14 @@ private void addBoolean(int index, boolean element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
boolean[] newArray = new boolean[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/DoubleArrayList.java
Expand Up @@ -197,7 +197,10 @@ public void addDouble(double element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
double[] newArray = new double[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -216,10 +219,14 @@ private void addDouble(int index, double element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
double[] newArray = new double[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/FloatArrayList.java
Expand Up @@ -196,7 +196,10 @@ public void addFloat(float element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
float[] newArray = new float[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -215,10 +218,14 @@ private void addFloat(int index, float element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
float[] newArray = new float[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/IntArrayList.java
Expand Up @@ -196,7 +196,10 @@ public void addInt(int element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
int[] newArray = new int[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -215,10 +218,14 @@ private void addInt(int index, int element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
int[] newArray = new int[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/LongArrayList.java
Expand Up @@ -196,7 +196,10 @@ public void addLong(long element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
long[] newArray = new long[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -215,10 +218,14 @@ private void addLong(int index, long element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
long[] newArray = new long[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
Expand Up @@ -80,7 +80,9 @@ public boolean add(E element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
E[] newArray = Arrays.copyOf(array, length);

array = newArray;
}

array[size++] = element;
Expand Down

0 comments on commit 49b184b

Please sign in to comment.