Skip to content

Commit c75743d

Browse files
committed
[GR-62533] Use Arrays.equals in ByteArraySequence.equals.
PullRequest: graal/20150
2 parents f5c6ce9 + 865e063 commit c75743d

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/io/ByteArraySequence.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -83,8 +83,7 @@ public byte[] toByteArray() {
8383
public boolean equals(Object obj) {
8484
if (this == obj) {
8585
return true;
86-
} else if (obj instanceof ByteArraySequence) {
87-
ByteArraySequence other = ((ByteArraySequence) obj);
86+
} else if (obj instanceof ByteArraySequence other) {
8887
if (buffer == other.buffer) {
8988
return start == other.start && length == other.length;
9089
}
@@ -97,15 +96,10 @@ public boolean equals(Object obj) {
9796
// hash was already computed and hash is not equal
9897
return false;
9998
}
100-
int otherStart = other.start;
101-
for (int i = 0; i < length; i++) {
102-
if (buffer[start + i] != other.buffer[otherStart + i]) {
103-
return false;
104-
}
105-
}
106-
return true;
107-
} else if (obj instanceof ByteSequence) {
108-
ByteSequence other = ((ByteSequence) obj);
99+
return Arrays.equals(
100+
this.buffer, this.start, this.start + this.length,
101+
other.buffer, other.start, other.start + other.length);
102+
} else if (obj instanceof ByteSequence other) {
109103
if (length != other.length()) {
110104
return false;
111105
}

truffle/src/com.oracle.truffle.api.benchmark/src/com/oracle/truffle/api/benchmark/ByteSequenceBenchmark.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -84,4 +84,27 @@ public int testHashing(HashingState state) {
8484
return hash;
8585
}
8686

87+
@State(Scope.Thread)
88+
public static class EqualsState {
89+
final ByteSequence[] sequences = new ByteSequence[ITERATIONS];
90+
private final byte[] buffer = new byte[4096];
91+
{
92+
for (int i = 0; i < buffer.length; i++) {
93+
buffer[i] = (byte) i;
94+
}
95+
for (int i = 0; i < sequences.length; i++) {
96+
sequences[i] = ByteSequence.create(buffer.clone());
97+
}
98+
}
99+
}
100+
101+
@Benchmark
102+
@OperationsPerInvocation(ITERATIONS)
103+
public int testEquals(EqualsState state) {
104+
int check = 0;
105+
for (int i = 0; i < state.sequences.length - 1; i++) {
106+
check += state.sequences[i].equals(state.sequences[i + 1]) ? 1 : 0;
107+
}
108+
return check;
109+
}
87110
}

0 commit comments

Comments
 (0)