Skip to content

Commit 7e12dc2

Browse files
committed
Move the immutability tracking for interned string lookups out of the cache key to save memory.
1 parent 3cee1ac commit 7e12dc2

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/main/java/org/truffleruby/core/string/TBytesKey.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,31 @@ public final class TBytesKey {
2323
private final byte[] bytes;
2424
private final int offset;
2525
private final int length;
26-
private final boolean isImmutable;
2726
private RubyEncoding encoding;
2827
private final int bytesHashCode;
2928

3029
public TBytesKey(
3130
byte[] bytes,
3231
int offset,
3332
int length,
34-
boolean isImmutable,
3533
int bytesHashCode,
3634
RubyEncoding encoding) {
3735
this.bytes = bytes;
3836
this.offset = offset;
3937
this.length = length;
40-
this.isImmutable = isImmutable;
4138
this.bytesHashCode = bytesHashCode;
4239
this.encoding = encoding;
4340
}
4441

4542
public TBytesKey(byte[] bytes, RubyEncoding encoding) {
46-
this(bytes, 0, bytes.length, true, Arrays.hashCode(bytes), encoding);
43+
this(bytes, 0, bytes.length, Arrays.hashCode(bytes), encoding);
4744
}
4845

49-
public TBytesKey(InternalByteArray byteArray, boolean isImmutable, RubyEncoding encoding) {
46+
public TBytesKey(InternalByteArray byteArray, RubyEncoding encoding) {
5047
this(
5148
byteArray.getArray(),
5249
byteArray.getOffset(),
5350
byteArray.getLength(),
54-
isImmutable,
5551
hashCode(byteArray),
5652
encoding);
5753
}
@@ -117,7 +113,7 @@ private boolean isPerfectFit() {
117113
return offset == 0 && length == bytes.length;
118114
}
119115

120-
public TBytesKey makeCacheable() {
116+
public TBytesKey makeCacheable(boolean isImmutable) {
121117
if (isImmutable && isPerfectFit()) {
122118
return new TBytesKey(bytes, encoding);
123119
}
@@ -127,7 +123,7 @@ public TBytesKey makeCacheable() {
127123
}
128124

129125
public TBytesKey withNewEncoding(RubyEncoding encoding) {
130-
return new TBytesKey(bytes, offset, length, isImmutable, bytesHashCode, encoding);
126+
return new TBytesKey(bytes, offset, length, bytesHashCode, encoding);
131127
}
132128

133129
public TruffleString toTruffleString() {

src/main/java/org/truffleruby/core/string/TStringCache.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,27 @@ public TruffleString getTString(TruffleString string, RubyEncoding rubyEncoding)
7575
assert rubyEncoding != null;
7676

7777
var byteArray = string.getInternalByteArrayUncached(rubyEncoding.tencoding);
78-
final TBytesKey key = new TBytesKey(byteArray, TStringUtils.hasImmutableInternalByteArray(string),
79-
rubyEncoding);
78+
final TBytesKey key = new TBytesKey(byteArray, rubyEncoding);
8079

81-
return getTString(key);
80+
return getTString(key, TStringUtils.hasImmutableInternalByteArray(string));
8281
}
8382

8483
@TruffleBoundary
8584
public TruffleString getTString(InternalByteArray byteArray, boolean isImmutable, RubyEncoding rubyEncoding) {
8685
assert rubyEncoding != null;
8786

88-
return getTString(new TBytesKey(byteArray, isImmutable, rubyEncoding));
87+
return getTString(new TBytesKey(byteArray, rubyEncoding), isImmutable);
8988
}
9089

9190
@TruffleBoundary
9291
public TruffleString getTString(byte[] bytes, RubyEncoding rubyEncoding) {
9392
assert rubyEncoding != null;
9493

95-
return getTString(new TBytesKey(bytes, rubyEncoding));
94+
return getTString(new TBytesKey(bytes, rubyEncoding), true);
9695
}
9796

9897
@TruffleBoundary
99-
private TruffleString getTString(TBytesKey lookupKey) {
98+
private TruffleString getTString(TBytesKey lookupKey, boolean isLookupKeyImmutable) {
10099
final TruffleString tstring = bytesToTString.get(lookupKey);
101100
var rubyEncoding = lookupKey.getMatchedEncoding();
102101

@@ -128,7 +127,7 @@ private TruffleString getTString(TBytesKey lookupKey) {
128127
}
129128

130129
// Use the new TruffleString bytes in the cache, so we do not keep bytes alive unnecessarily.
131-
return bytesToTString.addInCacheIfAbsent(lookupKey.makeCacheable(), newTString);
130+
return bytesToTString.addInCacheIfAbsent(lookupKey.makeCacheable(isLookupKeyImmutable), newTString);
132131
}
133132

134133
public boolean contains(TruffleString string, RubyEncoding encoding) {

0 commit comments

Comments
 (0)