Skip to content

Commit 1a1ab37

Browse files
committed
Move to a shared cache for interned strings and frozen literals.
1 parent aa408c8 commit 1a1ab37

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public class RubyContext {
120120
private final PreInitializationManager preInitializationManager;
121121
private final NativeConfiguration nativeConfiguration;
122122
private final ValueWrapperManager valueWrapperManager = new ValueWrapperManager(this);
123-
private final WeakValueCache<RopeKey, DynamicObject> internedStringCache = new WeakValueCache<>();
124123

125124
private final CompilerOptions compilerOptions = Truffle.getRuntime().createCompilerOptions();
126125

@@ -639,6 +638,10 @@ public DynamicObject getFrozenStringLiteral(Rope rope) {
639638
return frozenStringLiterals.getFrozenStringLiteral(rope);
640639
}
641640

641+
public DynamicObject getInternedString(DynamicObject string) {
642+
return frozenStringLiterals.getFrozenStringLiteral(string);
643+
}
644+
642645
public Object getClassVariableDefinitionLock() {
643646
return classVariableDefinitionLock;
644647
}
@@ -784,10 +787,6 @@ public ValueWrapperManager getValueWrapperManager() {
784787
return valueWrapperManager;
785788
}
786789

787-
public WeakValueCache<RopeKey, DynamicObject> getInternedStringCache() {
788-
return internedStringCache;
789-
}
790-
791790
private static SecureRandom createRandomInstance() {
792791
try {
793792
/*

src/main/java/org/truffleruby/collections/WeakValueCache.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.Map.Entry;
3939
import java.util.concurrent.ConcurrentHashMap;
4040

41+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
42+
4143
import org.truffleruby.core.hash.ReHashable;
4244

4345
/**
@@ -65,6 +67,7 @@ public Value get(Key key) {
6567
* Returns the value in the cache (existing or added).
6668
* Similar to a putIfAbsent() but always return the value in the cache.
6769
*/
70+
@TruffleBoundary
6871
public Value addInCacheIfAbsent(Key key, Value newValue) {
6972
removeStaleEntries();
7073

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,33 @@
1212
import java.util.Map;
1313
import java.util.concurrent.ConcurrentHashMap;
1414

15+
import org.truffleruby.Layouts;
1516
import org.truffleruby.RubyContext;
1617
import org.truffleruby.collections.ConcurrentOperations;
18+
import org.truffleruby.collections.WeakValueCache;
1719
import org.truffleruby.core.rope.Rope;
20+
import org.truffleruby.core.rope.RopeKey;
1821

1922
import com.oracle.truffle.api.object.DynamicObject;
2023

2124
public class FrozenStringLiterals {
2225

2326
private final RubyContext context;
24-
private final Map<Rope, DynamicObject> strings = new ConcurrentHashMap<>();
27+
private final WeakValueCache<RopeKey, DynamicObject> values = new WeakValueCache<>();
2528

2629
public FrozenStringLiterals(RubyContext context) {
2730
this.context = context;
2831
}
2932

3033
public DynamicObject getFrozenStringLiteral(Rope rope) {
31-
return ConcurrentOperations.getOrCompute(strings, rope, r -> StringOperations.createFrozenString(context, rope));
34+
final RopeKey key = new RopeKey(rope, context.getHashing(values));
35+
return values.addInCacheIfAbsent(key, StringOperations.createFrozenString(context, rope));
36+
}
37+
38+
public DynamicObject getFrozenStringLiteral(DynamicObject string) {
39+
final Rope rope = Layouts.STRING.getRope(string);
40+
final RopeKey key = new RopeKey(rope, context.getHashing(values));
41+
return values.addInCacheIfAbsent(key, string);
3242
}
3343

3444
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
import org.truffleruby.core.rope.RopeKey;
129129
import org.truffleruby.core.rope.RopeNodes;
130130
import org.truffleruby.core.rope.RopeNodes.RepeatNode;
131+
import org.truffleruby.core.rope.TruffleRopesNodes.FlattenRopeNode;
131132
import org.truffleruby.core.rope.RopeOperations;
132133
import org.truffleruby.core.rope.SubstringRope;
133134
import org.truffleruby.core.string.StringNodesFactory.ByteIndexFromCharIndexNodeGen;
@@ -4717,8 +4718,8 @@ protected Object emptyString(DynamicObject string) {
47174718
public abstract static class InternNode extends PrimitiveArrayArgumentsNode {
47184719

47194720
@Specialization
4720-
public DynamicObject internString(DynamicObject string) {
4721-
return getContext().getInternedStringCache().addInCacheIfAbsent(new RopeKey(Layouts.STRING.getRope(string), getContext().getHashing(this)), string);
4721+
public DynamicObject internString(VirtualFrame frame, DynamicObject string) {
4722+
return getContext().getInternedString(string);
47224723
}
47234724
}
47244725

0 commit comments

Comments
 (0)