8332826: Make hashCode methods in ArraysSupport friendlier

Reviewed-by: redestad, liach
This commit is contained in:
Pavel Rappo 2024-05-30 09:33:30 +00:00
parent 2b4a4b7bd8
commit 3cff588a31
13 changed files with 234 additions and 80 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -113,7 +113,7 @@ class CharacterName {
}
private static int hashN(byte[] a, int off, int len) {
return ArraysSupport.vectorizedHashCode(a, off, len, 1, ArraysSupport.T_BYTE);
return ArraysSupport.hashCode(a, off, len, 1);
}
private int addCp(int idx, int hash, int next, int cp) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -303,11 +303,7 @@ final class StringLatin1 {
}
public static int hashCode(byte[] value) {
return switch (value.length) {
case 0 -> 0;
case 1 -> value[0] & 0xff;
default -> ArraysSupport.vectorizedHashCode(value, 0, value.length, 0, ArraysSupport.T_BOOLEAN);
};
return ArraysSupport.hashCodeOfUnsigned(value, 0, value.length, 0);
}
// Caller must ensure that from- and toIndex are within bounds

View file

@ -585,11 +585,7 @@ final class StringUTF16 {
}
public static int hashCode(byte[] value) {
return switch (value.length) {
case 0 -> 0;
case 2 -> getChar(value, 0);
default -> ArraysSupport.vectorizedHashCode(value, 0, value.length >> 1, 0, ArraysSupport.T_CHAR);
};
return ArraysSupport.hashCodeOfUTF16(value, 0, value.length >> 1, 0);
}
// Caller must ensure that from- and toIndex are within bounds

View file

@ -4098,8 +4098,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
*/
@Override
public int hashCode() {
return ArraysSupport.vectorizedHashCode(mag, 0, mag.length, 0,
ArraysSupport.T_INT) * signum;
return ArraysSupport.hashCode(mag, 0, mag.length, 0) * signum;
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -707,7 +707,7 @@ class Heap$Type$Buffer$RW$
}
public int hashCode() {
return ArraysSupport.vectorizedHashCode(hb, ix(position()), remaining(), 1, ArraysSupport.T_BYTE);
return ArraysSupport.hashCode(hb, ix(position()), remaining(), 1);
}
#end[byte]
@ -738,7 +738,7 @@ class Heap$Type$Buffer$RW$
}
public int hashCode() {
return ArraysSupport.vectorizedHashCode(hb, ix(position()), remaining(), 1, ArraysSupport.T_CHAR);
return ArraysSupport.hashCode(hb, ix(position()), remaining(), 1);
}
#end[char]

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -4358,11 +4358,7 @@ public final class Arrays {
if (a == null) {
return 0;
}
return switch (a.length) {
case 0 -> 1;
case 1 -> 31 + a[0];
default -> ArraysSupport.vectorizedHashCode(a, 0, a.length, 1, ArraysSupport.T_INT);
};
return ArraysSupport.hashCode(a, 0, a.length, 1);
}
/**
@ -4385,11 +4381,7 @@ public final class Arrays {
if (a == null) {
return 0;
}
return switch (a.length) {
case 0 -> 1;
case 1 -> 31 + (int)a[0];
default -> ArraysSupport.vectorizedHashCode(a, 0, a.length, 1, ArraysSupport.T_SHORT);
};
return ArraysSupport.hashCode(a, 0, a.length, 1);
}
/**
@ -4412,11 +4404,7 @@ public final class Arrays {
if (a == null) {
return 0;
}
return switch (a.length) {
case 0 -> 1;
case 1 -> 31 + (int)a[0];
default -> ArraysSupport.vectorizedHashCode(a, 0, a.length, 1, ArraysSupport.T_CHAR);
};
return ArraysSupport.hashCode(a, 0, a.length, 1);
}
/**
@ -4439,11 +4427,7 @@ public final class Arrays {
if (a == null) {
return 0;
}
return switch (a.length) {
case 0 -> 1;
case 1 -> 31 + (int)a[0];
default -> ArraysSupport.vectorizedHashCode(a, 0, a.length, 1, ArraysSupport.T_BYTE);
};
return ArraysSupport.hashCode(a, 0, a.length, 1);
}
/**
@ -4549,15 +4533,10 @@ public final class Arrays {
* @since 1.5
*/
public static int hashCode(Object[] a) {
if (a == null)
if (a == null) {
return 0;
int result = 1;
for (Object element : a)
result = 31 * result + Objects.hashCode(element);
return result;
}
return ArraysSupport.hashCode(a, 0, a.length, 1);
}
/**

View file

@ -290,8 +290,7 @@ class ZipCoder {
// exceptions eagerly when opening ZipFiles
return hash(JLA.newStringUTF8NoRepl(a, off, len));
}
// T_BOOLEAN to treat the array as unsigned bytes, in line with StringLatin1.hashCode
int h = ArraysSupport.vectorizedHashCode(a, off, len, 0, ArraysSupport.T_BOOLEAN);
int h = ArraysSupport.hashCodeOfUnsigned(a, off, len, 0);
if (a[end - 1] != '/') {
h = 31 * h + '/';
}