Skip to content

Commit 58314bd

Browse files
committed
Ensure all ImmutableRubyString have a Rope from the RopeCache
1 parent 883221d commit 58314bd

File tree

8 files changed

+41
-34
lines changed

8 files changed

+41
-34
lines changed

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.oracle.truffle.api.instrumentation.AllocationReporter;
1919
import com.oracle.truffle.api.object.Shape;
2020
import org.graalvm.options.OptionDescriptors;
21+
import org.jcodings.Encoding;
2122
import org.truffleruby.builtins.PrimitiveManager;
2223
import org.truffleruby.core.RubyHandle;
2324
import org.truffleruby.core.array.RubyArray;
@@ -53,7 +54,7 @@
5354
import org.truffleruby.core.range.RubyObjectRange;
5455
import org.truffleruby.core.regexp.RubyMatchData;
5556
import org.truffleruby.core.regexp.RubyRegexp;
56-
import org.truffleruby.core.rope.LeafRope;
57+
import org.truffleruby.core.rope.CodeRange;
5758
import org.truffleruby.core.rope.Rope;
5859
import org.truffleruby.core.rope.RopeCache;
5960
import org.truffleruby.core.string.CoreStrings;
@@ -153,9 +154,9 @@ public final class RubyLanguage extends TruffleLanguage<RubyContext> {
153154
public final CoreStrings coreStrings;
154155
public final CoreSymbols coreSymbols;
155156
public final PrimitiveManager primitiveManager;
156-
public final FrozenStringLiterals frozenStringLiterals;
157157
public final RopeCache ropeCache;
158158
public final SymbolTable symbolTable;
159+
public final FrozenStringLiterals frozenStringLiterals;
159160
@CompilationFinal public LanguageOptions options;
160161

161162
@CompilationFinal private AllocationReporter allocationReporter;
@@ -212,9 +213,9 @@ public RubyLanguage() {
212213
coreStrings = new CoreStrings(this);
213214
coreSymbols = new CoreSymbols();
214215
primitiveManager = new PrimitiveManager();
215-
frozenStringLiterals = new FrozenStringLiterals();
216216
ropeCache = new RopeCache(coreSymbols);
217217
symbolTable = new SymbolTable(ropeCache, coreSymbols);
218+
frozenStringLiterals = new FrozenStringLiterals(ropeCache);
218219
}
219220

220221
@TruffleBoundary
@@ -435,7 +436,11 @@ public AllocationReporter getAllocationReporter() {
435436
return allocationReporter;
436437
}
437438

438-
public ImmutableRubyString getFrozenStringLiteral(LeafRope rope) {
439+
public ImmutableRubyString getFrozenStringLiteral(byte[] bytes, Encoding encoding, CodeRange codeRange) {
440+
return frozenStringLiterals.getFrozenStringLiteral(bytes, encoding, codeRange);
441+
}
442+
443+
public ImmutableRubyString getFrozenStringLiteral(Rope rope) {
439444
return frozenStringLiterals.getFrozenStringLiteral(rope);
440445
}
441446

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.truffleruby.core.numeric.BigIntegerOps;
4545
import org.truffleruby.core.numeric.RubyBignum;
4646
import org.truffleruby.core.rope.CodeRange;
47-
import org.truffleruby.core.rope.LeafRope;
4847
import org.truffleruby.core.string.RubyString;
4948
import org.truffleruby.core.string.StringOperations;
5049
import org.truffleruby.debug.GlobalVariablesObject;
@@ -737,10 +736,8 @@ private void setConstant(RubyModule module, String name, Object value) {
737736
}
738737

739738
private ImmutableRubyString frozenUSASCIIString(String string) {
740-
// NOTE(norswap, Nov. 2nd 2020): Okay for language access to be slow, currently only used during initialization.
741-
final LeafRope rope = language.ropeCache.getRope(
739+
return language.getFrozenStringLiteral(
742740
StringOperations.encodeRope(string, USASCIIEncoding.INSTANCE, CodeRange.CR_7BIT));
743-
return language.getFrozenStringLiteral(rope);
744741
}
745742

746743
private RubyClass defineClass(String name) {

src/main/java/org/truffleruby/core/encoding/EncodingManager.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.truffleruby.RubyLanguage;
3333
import org.truffleruby.core.klass.RubyClass;
3434
import org.truffleruby.core.rope.CodeRange;
35-
import org.truffleruby.core.rope.LeafRope;
3635
import org.truffleruby.core.rope.Rope;
3736
import org.truffleruby.core.rope.RopeOperations;
3837
import org.truffleruby.core.string.EncodingUtils;
@@ -182,11 +181,7 @@ private RubyEncoding newRubyEncoding(Encoding encoding, byte[] name, int p, int
182181
"; name.length = " + name.length + ")";
183182

184183
final Rope rope = RopeOperations.create(name, USASCIIEncoding.INSTANCE, CodeRange.CR_7BIT);
185-
final LeafRope cachedRope = language.ropeCache.getRope(
186-
rope.getBytes(),
187-
rope.getEncoding(),
188-
rope.getCodeRange());
189-
final ImmutableRubyString string = language.getFrozenStringLiteral(cachedRope);
184+
final ImmutableRubyString string = language.getFrozenStringLiteral(rope);
190185

191186
final RubyEncoding instance = new RubyEncoding(
192187
context.getCoreLibrary().encodingClass,

src/main/java/org/truffleruby/core/rope/RopeCache.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ public LeafRope getRope(Rope string) {
6262
return getRope(string.getBytes(), string.getEncoding(), string.getCodeRange());
6363
}
6464

65-
public LeafRope getRope(Rope string, CodeRange codeRange) {
66-
return getRope(string.getBytes(), string.getEncoding(), codeRange);
67-
}
68-
6965
@TruffleBoundary
7066
public LeafRope getRope(byte[] bytes, Encoding encoding, CodeRange codeRange) {
7167
assert encoding != null;

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,35 @@
99
*/
1010
package org.truffleruby.core.string;
1111

12+
import org.jcodings.Encoding;
1213
import org.truffleruby.collections.WeakValueCache;
14+
import org.truffleruby.core.rope.CodeRange;
1315
import org.truffleruby.core.rope.LeafRope;
1416
import org.truffleruby.core.rope.Rope;
17+
import org.truffleruby.core.rope.RopeCache;
1518

1619
public class FrozenStringLiterals {
1720

18-
private final WeakValueCache<Rope, ImmutableRubyString> values = new WeakValueCache<>();
21+
private final RopeCache ropeCache;
22+
private final WeakValueCache<LeafRope, ImmutableRubyString> values = new WeakValueCache<>();
1923

20-
public ImmutableRubyString getFrozenStringLiteral(LeafRope rope) {
21-
final ImmutableRubyString string = values.get(rope);
24+
public FrozenStringLiterals(RopeCache ropeCache) {
25+
this.ropeCache = ropeCache;
26+
}
27+
28+
public ImmutableRubyString getFrozenStringLiteral(Rope rope) {
29+
return getFrozenStringLiteral(rope.getBytes(), rope.getEncoding(), rope.getCodeRange());
30+
}
31+
32+
public ImmutableRubyString getFrozenStringLiteral(byte[] bytes, Encoding encoding, CodeRange codeRange) {
33+
// Ensure all ImmutableRubyString have a Rope from the RopeCache
34+
final LeafRope cachedRope = ropeCache.getRope(bytes, encoding, codeRange);
35+
36+
final ImmutableRubyString string = values.get(cachedRope);
2237
if (string != null) {
2338
return string;
2439
} else {
25-
return values.addInCacheIfAbsent(rope, new ImmutableRubyString(rope));
40+
return values.addInCacheIfAbsent(cachedRope, new ImmutableRubyString(cachedRope));
2641
}
2742
}
2843

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.truffleruby.language.library.RubyStringLibrary;
2929

3030
/** All ImmutableRubyString are interned and must be created through
31-
* {@link FrozenStringLiterals#getFrozenStringLiteral(LeafRope)}. */
31+
* {@link FrozenStringLiterals#getFrozenStringLiteral(Rope)}. */
3232
@ExportLibrary(InteropLibrary.class)
3333
@ExportLibrary(RubyStringLibrary.class)
3434
public class ImmutableRubyString extends ImmutableRubyObject implements TruffleObject {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5370,7 +5370,7 @@ public abstract static class InternNode extends PrimitiveArrayArgumentsNode {
53705370
@Specialization
53715371
protected ImmutableRubyString internString(RubyString string,
53725372
@Cached RopeNodes.FlattenNode flattenNode) {
5373-
final LeafRope flattened = flattenNode.executeFlatten(string.rope);
5373+
final Rope flattened = flattenNode.executeFlatten(string.rope);
53745374
return getLanguage().getFrozenStringLiteral(flattened);
53755375
}
53765376

src/main/java/org/truffleruby/parser/BodyTranslator.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import org.truffleruby.core.regexp.RegexpOptions;
6060
import org.truffleruby.core.regexp.RubyRegexp;
6161
import org.truffleruby.core.regexp.TruffleRegexpNodes;
62-
import org.truffleruby.core.rope.CodeRange;
6362
import org.truffleruby.core.rope.LeafRope;
6463
import org.truffleruby.core.rope.Rope;
6564
import org.truffleruby.core.rope.RopeConstants;
@@ -511,10 +510,9 @@ public RubyNode visitCallNode(CallParseNode node) {
511510
if (receiver instanceof StrParseNode && methodName.equals("freeze")) {
512511
final StrParseNode strNode = (StrParseNode) receiver;
513512
final Rope nodeRope = strNode.getValue();
514-
final CodeRange codeRange = strNode.getCodeRange();
515513

516-
final LeafRope rope = language.ropeCache.getRope(nodeRope, codeRange);
517-
final ImmutableRubyString frozenString = language.getFrozenStringLiteral(rope);
514+
final ImmutableRubyString frozenString = language
515+
.getFrozenStringLiteral(nodeRope.getBytes(), nodeRope.getEncoding(), strNode.getCodeRange());
518516

519517
return addNewlineIfNeeded(node, withSourceSection(
520518
sourceSection,
@@ -2936,17 +2934,18 @@ public RubyNode visitSplatNode(SplatParseNode node) {
29362934

29372935
@Override
29382936
public RubyNode visitStrNode(StrParseNode node) {
2939-
final LeafRope rope = language.ropeCache.getRope(node.getValue(), node.getCodeRange());
2937+
final Rope nodeRope = node.getValue();
29402938
final RubyNode ret;
29412939

29422940
if (node.isFrozen()) {
2943-
final ImmutableRubyString frozenString = language.getFrozenStringLiteral(rope);
2941+
final ImmutableRubyString frozenString = language
2942+
.getFrozenStringLiteral(nodeRope.getBytes(), nodeRope.getEncoding(), node.getCodeRange());
29442943

2945-
ret = new DefinedWrapperNode(
2946-
language.coreStrings.EXPRESSION,
2947-
new ObjectLiteralNode(frozenString));
2944+
ret = new DefinedWrapperNode(language.coreStrings.EXPRESSION, new ObjectLiteralNode(frozenString));
29482945
} else {
2949-
ret = new StringLiteralNode(rope);
2946+
final LeafRope cachedRope = language.ropeCache
2947+
.getRope(nodeRope.getBytes(), nodeRope.getEncoding(), node.getCodeRange());
2948+
ret = new StringLiteralNode(cachedRope);
29502949
}
29512950
ret.unsafeSetSourceSection(node.getPosition());
29522951
return addNewlineIfNeeded(node, ret);

0 commit comments

Comments
 (0)