Skip to content

Commit 19ec2c2

Browse files
committed
Cleanup
1 parent 2effcae commit 19ec2c2

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import java.util.Objects;
1515

16-
public class RopeWithEncoding {
16+
public final class RopeWithEncoding {
1717

1818
private final Rope rope;
1919
private final RubyEncoding encoding;
@@ -37,11 +37,11 @@ public boolean equals(Object o) {
3737
if (this == o) {
3838
return true;
3939
}
40-
if (o == null || getClass() != o.getClass()) {
40+
if (!(o instanceof RopeWithEncoding)) {
4141
return false;
4242
}
4343
RopeWithEncoding that = (RopeWithEncoding) o;
44-
return rope.equals(that.rope) && encoding.equals(that.encoding);
44+
return rope.equals(that.rope) && encoding == that.encoding;
4545
}
4646

4747
@Override

src/main/java/org/truffleruby/core/symbol/SymbolTable.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SymbolTable {
3838
// However, this doesn't matter as the cache entries will be re-created when used.
3939
private final WeakValueCache<String, RubySymbol> stringToSymbolCache = new WeakValueCache<>();
4040

41-
// Weak map of RopeKey to Symbol to keep Symbols unique.
41+
// Weak map of RopeWithEncoding to Symbol to keep Symbols unique.
4242
// As long as the Symbol is referenced, the entry will stay in the symbolMap.
4343
private final WeakValueCache<RopeWithEncoding, RubySymbol> symbolMap = new WeakValueCache<>();
4444

@@ -50,17 +50,19 @@ public SymbolTable(RopeCache ropeCache, CoreSymbols coreSymbols) {
5050
private void addCoreSymbols(CoreSymbols coreSymbols) {
5151
for (RubySymbol symbol : coreSymbols.CORE_SYMBOLS) {
5252
final Rope rope = symbol.getRope();
53-
assert rope == normalizeRopeForLookup(rope, symbol.encoding).getRope();
53+
final RopeWithEncoding ropeWithEncoding = normalizeRopeForLookup(rope, symbol.encoding);
54+
assert rope == ropeWithEncoding.getRope();
5455
assert rope == ropeCache.getRope(rope);
5556

56-
final RopeWithEncoding ropeWithEncoding = new RopeWithEncoding(rope, symbol.encoding);
5757
final RubySymbol existing = symbolMap.put(ropeWithEncoding, symbol);
5858
if (existing != null) {
5959
throw new AssertionError("Duplicate Symbol in SymbolTable: " + existing);
6060
}
6161

6262
final RubySymbol old = stringToSymbolCache.put(symbol.getString(), symbol);
63-
assert old == null || new RopeWithEncoding(old.getRope(), old.encoding).equals(ropeWithEncoding);
63+
if (old != null) {
64+
throw new AssertionError("Duplicate Symbol in SymbolTable: " + old);
65+
}
6466
}
6567
}
6668

@@ -82,26 +84,26 @@ public RubySymbol getSymbol(String string) {
8284
}
8385
symbol = getSymbol(rope, encoding);
8486

85-
// Add it to the direct j.l.String to Symbol cache
86-
87+
// Add it to the direct java.lang.String to Symbol cache
8788
stringToSymbolCache.addInCacheIfAbsent(string, symbol);
8889

8990
return symbol;
9091
}
9192

9293
@TruffleBoundary
9394
public RubySymbol getSymbol(Rope rope, RubyEncoding encoding) {
94-
final RopeWithEncoding normalizedRope = normalizeRopeForLookup(rope, encoding);
95-
final RubySymbol symbol = symbolMap.get(normalizedRope);
95+
final RopeWithEncoding ropeEncodingForLookup = normalizeRopeForLookup(rope, encoding);
96+
final RubySymbol symbol = symbolMap.get(ropeEncodingForLookup);
9697
if (symbol != null) {
9798
return symbol;
9899
}
99100

100-
final LeafRope cachedRope = ropeCache.getRope(normalizedRope.getRope());
101-
final RubySymbol newSymbol = createSymbol(cachedRope, normalizedRope.getEncoding());
102-
// Use a RopeKey with the cached Rope in symbolMap, since the Symbol refers to it and so we
101+
final LeafRope cachedRope = ropeCache.getRope(ropeEncodingForLookup.getRope());
102+
final RubyEncoding symbolEncoding = ropeEncodingForLookup.getEncoding();
103+
final RubySymbol newSymbol = createSymbol(cachedRope, symbolEncoding);
104+
// Use a RopeWithEncoding with the cached Rope in symbolMap, since the Symbol refers to it and so we
103105
// do not keep rope alive unnecessarily.
104-
return symbolMap.addInCacheIfAbsent(new RopeWithEncoding(cachedRope, normalizedRope.getEncoding()), newSymbol);
106+
return symbolMap.addInCacheIfAbsent(new RopeWithEncoding(cachedRope, symbolEncoding), newSymbol);
105107
}
106108

107109
@TruffleBoundary

0 commit comments

Comments
 (0)