Skip to content

Commit e13b61f

Browse files
gkorlandmp911de
authored andcommitted
Improve ByteUtils.concatAll(…) array allocations.
Closes #2366
1 parent 6b1038c commit e13b61f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/main/java/org/springframework/data/redis/util/ByteUtils.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @author Christoph Strobl
3232
* @author Mark Paluch
33+
* @author Guy Korland
3334
* @since 1.7
3435
*/
3536
public final class ByteUtils {
@@ -71,11 +72,19 @@ public static byte[] concatAll(byte[]... arrays) {
7172
return arrays[0];
7273
}
7374

74-
byte[] cur = concat(arrays[0], arrays[1]);
75-
for (int i = 2; i < arrays.length; i++) {
76-
cur = concat(cur, arrays[i]);
75+
// Sum the total result length
76+
int sum = 0;
77+
for (int i = 0; i < arrays.length; ++i) {
78+
sum += arrays[i].length;
7779
}
78-
return cur;
80+
81+
byte[] result = Arrays.copyOf(arrays[0], sum);
82+
int copied = arrays[0].length;
83+
for (int i = 1; i < arrays.length; ++i) {
84+
System.arraycopy(arrays[i], 0, result, copied, arrays[i].length);
85+
copied += arrays[i].length;
86+
}
87+
return result;
7988
}
8089

8190
/**

0 commit comments

Comments
 (0)