Skip to content

Commit ca9a1a1

Browse files
committed
Switch to a WeakValueCache for interned strings.
1 parent 9a11c98 commit ca9a1a1

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.truffleruby.core.rope.PathToRopeCache;
4747
import org.truffleruby.core.rope.Rope;
4848
import org.truffleruby.core.rope.RopeCache;
49+
import org.truffleruby.core.rope.RopeKey;
4950
import org.truffleruby.core.string.CoreStrings;
5051
import org.truffleruby.core.string.FrozenStringLiterals;
5152
import org.truffleruby.core.symbol.SymbolTable;
@@ -119,6 +120,7 @@ public class RubyContext {
119120
private final PreInitializationManager preInitializationManager;
120121
private final NativeConfiguration nativeConfiguration;
121122
private final ValueWrapperManager valueWrapperManager = new ValueWrapperManager(this);
123+
private final WeakValueCache<RopeKey, DynamicObject> internedStringCache = new WeakValueCache<>();
122124

123125
private final CompilerOptions compilerOptions = Truffle.getRuntime().createCompilerOptions();
124126

@@ -782,6 +784,10 @@ public ValueWrapperManager getValueWrapperManager() {
782784
return valueWrapperManager;
783785
}
784786

787+
public WeakValueCache<RopeKey, DynamicObject> getInternedStringCache() {
788+
return internedStringCache;
789+
}
790+
785791
private static SecureRandom createRandomInstance() {
786792
try {
787793
/*

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
import com.oracle.truffle.api.dsl.Cached;
1414
import com.oracle.truffle.api.dsl.Specialization;
1515
import com.oracle.truffle.api.object.DynamicObject;
16+
import org.truffleruby.Layouts;
1617
import org.truffleruby.builtins.CoreClass;
1718
import org.truffleruby.builtins.CoreMethod;
1819
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1920
import org.truffleruby.core.rope.Rope;
21+
import org.truffleruby.core.rope.RopeKey;
2022
import org.truffleruby.core.rope.RopeNodes;
2123
import org.truffleruby.language.RubyGuards;
2224
import org.truffleruby.language.control.RaiseException;
@@ -70,4 +72,12 @@ private String formatTooLongError(int count, final Rope rope) {
7072

7173
}
7274

75+
@CoreMethod(names = "intern_string", onSingleton = true, required = 1)
76+
public abstract static class InternNode extends CoreMethodArrayArgumentsNode {
77+
78+
@Specialization
79+
public DynamicObject internString(DynamicObject string) {
80+
return getContext().getInternedStringCache().addInCacheIfAbsent(new RopeKey(Layouts.STRING.getRope(string), getContext().getHashing(this)), string);
81+
}
82+
}
7383
}

src/main/ruby/core/truffle/string_operations.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
module Truffle
1212
module StringOperations
1313

14-
FROZEN_STRING_HASH = {}
15-
16-
def self.intern_string(str)
17-
FROZEN_STRING_HASH[str] ||= str
18-
end
19-
2014
# Similar to MRI's Warning::buffer class
2115
class SimpleStringIO
2216
attr_reader :string

0 commit comments

Comments
 (0)