8300647: Miscellaneous hashCode improvements in java.base

Reviewed-by: stsypanov, rriggs
This commit is contained in:
Claes Redestad 2023-01-19 16:50:07 +00:00
parent 453dbd12ee
commit d85243f02b
2 changed files with 6 additions and 36 deletions

View File

@ -25,6 +25,7 @@
package sun.security.util;
import jdk.internal.util.ArraysSupport;
import sun.nio.cs.UTF_32BE;
import sun.util.calendar.CalendarDate;
import sun.util.calendar.CalendarSystem;
@ -1257,11 +1258,7 @@ public class DerValue {
*/
@Override
public int hashCode() {
int result = tag;
for (int i = start; i < end; i++) {
result = 31 * result + buffer[i];
}
return result;
return ArraysSupport.vectorizedHashCode(buffer, start, end - start, tag, ArraysSupport.T_BYTE);
}
/**

View File

@ -145,11 +145,11 @@ final class ProcessEnvironment
public boolean equals(Object o) {
return o instanceof ExternalData
&& arrayEquals(getBytes(), ((ExternalData) o).getBytes());
&& Arrays.equals(getBytes(), ((ExternalData) o).getBytes());
}
public int hashCode() {
return arrayHash(getBytes());
return Arrays.hashCode(getBytes());
}
}
@ -178,7 +178,7 @@ final class ProcessEnvironment
}
public int compareTo(Variable variable) {
return arrayCompare(getBytes(), variable.getBytes());
return Arrays.compare(getBytes(), variable.getBytes());
}
public boolean equals(Object o) {
@ -211,7 +211,7 @@ final class ProcessEnvironment
}
public int compareTo(Value value) {
return arrayCompare(getBytes(), value.getBytes());
return Arrays.compare(getBytes(), value.getBytes());
}
public boolean equals(Object o) {
@ -412,31 +412,4 @@ final class ProcessEnvironment
}
}
// Replace with general purpose method someday
private static int arrayCompare(byte[]x, byte[] y) {
int min = x.length < y.length ? x.length : y.length;
for (int i = 0; i < min; i++)
if (x[i] != y[i])
return x[i] - y[i];
return x.length - y.length;
}
// Replace with general purpose method someday
private static boolean arrayEquals(byte[] x, byte[] y) {
if (x.length != y.length)
return false;
for (int i = 0; i < x.length; i++)
if (x[i] != y[i])
return false;
return true;
}
// Replace with general purpose method someday
private static int arrayHash(byte[] x) {
int hash = 0;
for (int i = 0; i < x.length; i++)
hash = 31 * hash + x[i];
return hash;
}
}