Skip to content

Commit 52bbeeb

Browse files
committed
Preserve already computed hash code in TruffleString to Java String conversion.
1 parent 8ea3472 commit 52bbeeb

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

truffle/src/com.oracle.truffle.api.strings/src/com/oracle/truffle/api/strings/AbstractTruffleString.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 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
@@ -131,6 +131,8 @@ public abstract sealed class AbstractTruffleString permits TruffleString, Mutabl
131131
*/
132132
int hashCode;
133133

134+
static final int MASKED_ZERO_HASH_CODE = -1;
135+
134136
AbstractTruffleString(Object data, int offset, int length, int stride, Encoding encoding, int flags, int codePointLength, int codeRange) {
135137
validateData(data, offset, length, stride);
136138
assert isByte(stride);

truffle/src/com.oracle.truffle.api.strings/src/com/oracle/truffle/api/strings/TStringInternalNodes.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 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
@@ -2039,7 +2039,12 @@ static String createJavaString(Node node, AbstractTruffleString a, byte[] arrayA
20392039
arrayA, offsetA, a.stride(), 0,
20402040
bytes, byteArrayBaseOffset(), stride, 0, a.length());
20412041
}
2042-
return TStringUnsafe.createJavaString(bytes, stride);
2042+
int javaStringHash = a.hashCode;
2043+
if (javaStringHash == AbstractTruffleString.MASKED_ZERO_HASH_CODE) {
2044+
// hash code could be 0 or -1, treat the hash code as uninitialized
2045+
javaStringHash = 0;
2046+
}
2047+
return TStringUnsafe.createJavaString(bytes, stride, javaStringHash);
20432048
}
20442049
}
20452050

truffle/src/com.oracle.truffle.api.strings/src/com/oracle/truffle/api/strings/TStringUnsafe.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 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
@@ -154,12 +154,12 @@ private static String allocateJavaString() {
154154
}
155155

156156
@TruffleBoundary
157-
static String createJavaString(byte[] bytes, int stride) {
157+
static String createJavaString(byte[] bytes, int stride, int hash) {
158158
if (stride < (COMPACT_STRINGS_ENABLED ? 0 : 1) || stride > 1) {
159159
throw new IllegalArgumentException("illegal stride!");
160160
}
161161
String ret = allocateJavaString();
162-
UNSAFE.putInt(ret, javaStringHashFieldOffset, 0);
162+
UNSAFE.putInt(ret, javaStringHashFieldOffset, hash);
163163
UNSAFE.putByte(ret, javaStringCoderFieldOffset, (byte) stride);
164164
UNSAFE.putObjectVolatile(ret, javaStringValueFieldOffset, bytes);
165165
assert checkUnsafeStringResult(bytes, stride, ret);

truffle/src/com.oracle.truffle.api.strings/src/com/oracle/truffle/api/strings/TruffleString.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 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
@@ -3087,6 +3087,7 @@ private static int maskZero(int rawHashCode) {
30873087
int h = rawHashCode;
30883088
if (h == 0) {
30893089
h--;
3090+
assert h == MASKED_ZERO_HASH_CODE;
30903091
}
30913092
return h;
30923093
}

0 commit comments

Comments
 (0)