Skip to content

Commit dbb0091

Browse files
committed
Convert ToHashByHashCode to DSL inlinable
1 parent 3163bbd commit dbb0091

File tree

9 files changed

+57
-50
lines changed

9 files changed

+57
-50
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ public TruffleLanguage.Env getEnv() {
558558
}
559559

560560
/** Hashing for a RubyNode, the seed should only be used for a Ruby-level #hash method */
561-
public Hashing getHashing(RubyBaseNode node) {
561+
public Hashing getHashing(Node node) {
562562
return hashing;
563563
}
564564

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ public abstract static class RbHashNode extends CoreMethodArrayArgumentsNode {
13331333
@Specialization
13341334
protected int rbHash(Object object,
13351335
@Cached HashingNodes.ToHashByHashCode toHashByHashCode) {
1336-
return toHashByHashCode.execute(object);
1336+
return toHashByHashCode.execute(this, object);
13371337
}
13381338
}
13391339

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import com.oracle.truffle.api.TruffleSafepoint;
1919
import com.oracle.truffle.api.dsl.NeverDefault;
20+
import com.oracle.truffle.api.nodes.Node;
2021
import com.oracle.truffle.api.object.Shape;
22+
import com.oracle.truffle.api.profiles.InlinedIntValueProfile;
23+
import com.oracle.truffle.api.profiles.InlinedLoopConditionProfile;
2124
import com.oracle.truffle.api.profiles.LoopConditionProfile;
2225
import com.oracle.truffle.api.source.SourceSection;
2326
import com.oracle.truffle.api.strings.TruffleString;
@@ -1058,25 +1061,26 @@ public abstract static class HashNode extends PrimitiveArrayArgumentsNode {
10581061
private static final int CLASS_SALT = 42753062; // random number, stops hashes for similar values but different classes being the same, static because we want deterministic hashes
10591062

10601063
@Specialization(limit = "storageStrategyLimit()")
1061-
protected long hash(VirtualFrame frame, RubyArray array,
1064+
protected static long hash(VirtualFrame frame, RubyArray array,
10621065
@Bind("array.getStore()") Object store,
10631066
@CachedLibrary("store") ArrayStoreLibrary stores,
10641067
@Cached HashingNodes.ToHashByHashCode toHashByHashCode,
1065-
@Cached @Shared IntValueProfile arraySizeProfile,
1066-
@Cached @Shared LoopConditionProfile loopProfile) {
1067-
final int size = arraySizeProfile.profile(array.size);
1068-
long h = getContext().getHashing(this).start(size);
1068+
@Cached @Shared InlinedIntValueProfile arraySizeProfile,
1069+
@Cached @Shared InlinedLoopConditionProfile loopProfile,
1070+
@Bind("this") Node node) {
1071+
final int size = arraySizeProfile.profile(node, array.size);
1072+
long h = getContext(node).getHashing(node).start(size);
10691073
h = Hashing.update(h, CLASS_SALT);
10701074

10711075
int n = 0;
10721076
try {
1073-
for (; loopProfile.inject(n < size); n++) {
1077+
for (; loopProfile.inject(node, n < size); n++) {
10741078
final Object value = stores.read(store, n);
1075-
h = Hashing.update(h, toHashByHashCode.execute(value));
1076-
TruffleSafepoint.poll(this);
1079+
h = Hashing.update(h, toHashByHashCode.execute(node, value));
1080+
TruffleSafepoint.poll(node);
10771081
}
10781082
} finally {
1079-
profileAndReportLoopCount(loopProfile, n);
1083+
profileAndReportLoopCount(node, loopProfile, n);
10801084
}
10811085

10821086
return Hashing.end(h);

src/main/java/org/truffleruby/core/hash/HashNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected Object construct(RubyClass hashClass, Object[] args,
109109
final Object key = pairObjectStore[0];
110110
final Object value = pairObjectStore[1];
111111

112-
final int hashed = hashNode.execute(key);
112+
final int hashed = hashNode.execute(this, key);
113113

114114
PackedHashStoreLibrary.setHashedKeyValue(newStore, n, hashed, key, value);
115115
}

src/main/java/org/truffleruby/core/hash/HashOperations.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
package org.truffleruby.core.hash;
1111

1212
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
13+
import com.oracle.truffle.api.nodes.Node;
1314
import org.truffleruby.RubyContext;
1415
import org.truffleruby.RubyLanguage;
1516
import org.truffleruby.core.hash.library.EmptyHashStore;
1617
import org.truffleruby.core.hash.library.HashStoreLibrary;
1718
import org.truffleruby.core.numeric.BigIntegerOps;
1819
import org.truffleruby.core.numeric.RubyBignum;
19-
import org.truffleruby.language.RubyBaseNode;
2020

2121
import java.util.Map;
2222
import java.util.Optional;
@@ -55,19 +55,19 @@ public static <K, V> RubyHash toRubyHash(RubyContext context, RubyLanguage langu
5555
public static final int INTEGER_CLASS_SALT = 1028093337;
5656
public static final int DOUBLE_CLASS_SALT = -1611229937;
5757

58-
public static long hashBoolean(boolean value, RubyContext context, RubyBaseNode node) {
58+
public static long hashBoolean(boolean value, RubyContext context, Node node) {
5959
return context.getHashing(node).hash(BOOLEAN_CLASS_SALT, Boolean.hashCode(value));
6060
}
6161

62-
public static long hashLong(long value, RubyContext context, RubyBaseNode node) {
62+
public static long hashLong(long value, RubyContext context, Node node) {
6363
return context.getHashing(node).hash(INTEGER_CLASS_SALT, value);
6464
}
6565

66-
public static long hashDouble(double value, RubyContext context, RubyBaseNode node) {
66+
public static long hashDouble(double value, RubyContext context, Node node) {
6767
return context.getHashing(node).hash(DOUBLE_CLASS_SALT, Double.doubleToRawLongBits(value));
6868
}
6969

70-
public static long hashBignum(RubyBignum value, RubyContext context, RubyBaseNode node) {
70+
public static long hashBignum(RubyBignum value, RubyContext context, Node node) {
7171
return context.getHashing(node).hash(INTEGER_CLASS_SALT, BigIntegerOps.hashCode(value));
7272
}
7373
}

src/main/java/org/truffleruby/core/hash/HashingNodes.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
import com.oracle.truffle.api.dsl.Cached;
1313
import com.oracle.truffle.api.dsl.Cached.Shared;
1414
import com.oracle.truffle.api.dsl.Fallback;
15+
import com.oracle.truffle.api.dsl.GenerateInline;
1516
import com.oracle.truffle.api.dsl.GenerateUncached;
1617
import com.oracle.truffle.api.dsl.NeverDefault;
1718
import com.oracle.truffle.api.dsl.ReportPolymorphism;
1819
import com.oracle.truffle.api.dsl.Specialization;
20+
import com.oracle.truffle.api.nodes.Node;
1921
import org.truffleruby.core.basicobject.BasicObjectNodes.ObjectIDNode;
2022
import org.truffleruby.core.cast.ToRubyIntegerNode;
2123
import org.truffleruby.core.numeric.BigIntegerOps;
@@ -42,7 +44,7 @@ public static ToHash getUncached() {
4244
@Specialization(guards = "!compareByIdentity")
4345
protected int hash(Object key, boolean compareByIdentity,
4446
@Cached ToHashByHashCode toHashByHashCode) {
45-
return toHashByHashCode.execute(key);
47+
return toHashByHashCode.execute(this, key);
4648
}
4749

4850

@@ -56,6 +58,7 @@ protected int hashCompareByIdentity(Object key, boolean compareByIdentity,
5658
// MRI: any_hash
5759
/** Keep consistent with {@link org.truffleruby.core.kernel.KernelNodes.HashNode} */
5860
@GenerateUncached
61+
@GenerateInline(inlineByDefault = true)
5962
@ReportPolymorphism
6063
public abstract static class ToHashByHashCode extends RubyBaseNode {
6164

@@ -64,53 +67,53 @@ public static ToHashByHashCode create() {
6467
return HashingNodesFactory.ToHashByHashCodeNodeGen.create();
6568
}
6669

67-
public abstract int execute(Object key);
70+
public abstract int execute(Node node, Object key);
6871

6972
@Specialization
70-
protected int hashBoolean(boolean value) {
71-
return (int) HashOperations.hashBoolean(value, getContext(), this);
73+
protected static int hashBoolean(Node node, boolean value) {
74+
return (int) HashOperations.hashBoolean(value, getContext(node), node);
7275
}
7376

7477
@Specialization
75-
protected int hashInt(int value) {
76-
return (int) HashOperations.hashLong(value, getContext(), this);
78+
protected static int hashInt(Node node, int value) {
79+
return (int) HashOperations.hashLong(value, getContext(node), node);
7780
}
7881

7982
@Specialization
80-
protected int hashLong(long value) {
81-
return (int) HashOperations.hashLong(value, getContext(), this);
83+
protected static int hashLong(Node node, long value) {
84+
return (int) HashOperations.hashLong(value, getContext(node), node);
8285
}
8386

8487
@Specialization
85-
protected int hashDouble(double value) {
86-
return (int) HashOperations.hashDouble(value, getContext(), this);
88+
protected static int hashDouble(Node node, double value) {
89+
return (int) HashOperations.hashDouble(value, getContext(node), node);
8790
}
8891

8992
@Specialization
90-
protected int hashBignum(RubyBignum value) {
91-
return (int) HashOperations.hashBignum(value, getContext(), this);
93+
protected static int hashBignum(Node node, RubyBignum value) {
94+
return (int) HashOperations.hashBignum(value, getContext(node), node);
9295
}
9396

9497
@Specialization
95-
protected int hashString(RubyString value,
98+
protected static int hashString(RubyString value,
9699
@Shared @Cached StringHelperNodes.HashStringNode stringHashNode) {
97100
return (int) stringHashNode.execute(value);
98101
}
99102

100103
@Specialization
101-
protected int hashImmutableString(ImmutableRubyString value,
104+
protected static int hashImmutableString(ImmutableRubyString value,
102105
@Shared @Cached StringHelperNodes.HashStringNode stringHashNode) {
103106
return (int) stringHashNode.execute(value);
104107
}
105108

106109
@Specialization
107-
protected int hashSymbol(RubySymbol value,
110+
protected static int hashSymbol(RubySymbol value,
108111
@Cached SymbolNodes.HashSymbolNode symbolHashNode) {
109112
return (int) symbolHashNode.execute(value);
110113
}
111114

112115
@Fallback
113-
protected int hashOther(Object value,
116+
protected static int hashOther(Object value,
114117
@Cached DispatchNode callHash,
115118
@Cached HashCastResultNode cast) {
116119
return cast.execute(callHash.call(value, "hash"));

src/main/java/org/truffleruby/core/hash/library/PackedHashStoreLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ private int hash(Object key) {
538538
CompilerDirectives.transferToInterpreterAndInvalidate();
539539
hashNode = insert(HashingNodes.ToHashByHashCode.create());
540540
}
541-
return hashNode.execute(key);
541+
return hashNode.execute(this, key);
542542
}
543543

544544
private boolean callEqual(Object receiver, Object key) {

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ private RubyHash createFreezeBooleanHash(boolean freeze, HashingNodes.ToHashByHa
605605
final RubySymbol key = coreSymbols().FREEZE;
606606

607607
final Object[] newStore = PackedHashStoreLibrary.createStore();
608-
final int hashed = hashNode.execute(key);
608+
final int hashed = hashNode.execute(this, key);
609609
PackedHashStoreLibrary.setHashedKeyValue(newStore, 0, hashed, key, freeze);
610610

611611
return new RubyHash(coreLibrary().hashClass, getLanguage().hashShape, getContext(), newStore, 1, false);

src/main/java/org/truffleruby/extra/ConcurrentMapNodes.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public abstract static class GetIndexNode extends CoreMethodArrayArgumentsNode {
9191
@Specialization
9292
protected Object getIndex(RubyConcurrentMap self, Object key,
9393
@Cached ToHashByHashCode hashNode) {
94-
final int hashCode = hashNode.execute(key);
94+
final int hashCode = hashNode.execute(this, key);
9595
return nullToNil(get(self.getMap(), new Key(key, hashCode)));
9696
}
9797
}
@@ -101,7 +101,7 @@ public abstract static class SetIndexNode extends CoreMethodArrayArgumentsNode {
101101
@Specialization
102102
protected Object setIndex(RubyConcurrentMap self, Object key, Object value,
103103
@Cached ToHashByHashCode hashNode) {
104-
final int hashCode = hashNode.execute(key);
104+
final int hashCode = hashNode.execute(this, key);
105105
put(self.getMap(), new Key(key, hashCode), value);
106106
return value;
107107
}
@@ -118,7 +118,7 @@ public abstract static class ComputeIfAbsentNode extends CoreMethodArrayArgument
118118
protected Object computeIfAbsent(RubyConcurrentMap self, Object key, RubyProc block,
119119
@Cached ToHashByHashCode hashNode,
120120
@Cached CallBlockNode yieldNode) {
121-
final int hashCode = hashNode.execute(key);
121+
final int hashCode = hashNode.execute(this, key);
122122
final Object returnValue = ConcurrentOperations
123123
.getOrCompute(self.getMap(), new Key(key, hashCode), (k) -> yieldNode.yield(block));
124124
assert returnValue != null;
@@ -133,7 +133,7 @@ public abstract static class ComputeIfPresentNode extends CoreMethodArrayArgumen
133133
protected Object computeIfPresent(RubyConcurrentMap self, Object key, RubyProc block,
134134
@Cached ToHashByHashCode hashNode,
135135
@Cached CallBlockNode yieldNode) {
136-
final int hashCode = hashNode.execute(key);
136+
final int hashCode = hashNode.execute(this, key);
137137
return nullToNil(
138138
computeIfPresent(self.getMap(), new Key(key, hashCode), (k, v) ->
139139
// TODO (Chris, 6 May 2021): It's unfortunate we're calling this behind a boundary! Can we do better?
@@ -153,7 +153,7 @@ public abstract static class ComputeNode extends CoreMethodArrayArgumentsNode {
153153
protected Object compute(RubyConcurrentMap self, Object key, RubyProc block,
154154
@Cached ToHashByHashCode hashNode,
155155
@Cached CallBlockNode yieldNode) {
156-
final int hashCode = hashNode.execute(key);
156+
final int hashCode = hashNode.execute(this, key);
157157
return nullToNil(compute(
158158
self.getMap(),
159159
new Key(key, hashCode),
@@ -173,7 +173,7 @@ public abstract static class MergePairNode extends CoreMethodArrayArgumentsNode
173173
protected Object mergePair(RubyConcurrentMap self, Object key, Object value, RubyProc block,
174174
@Cached ToHashByHashCode hashNode,
175175
@Cached CallBlockNode yieldNode) {
176-
final int hashCode = hashNode.execute(key);
176+
final int hashCode = hashNode.execute(this, key);
177177
return nullToNil(merge(
178178
self.getMap(),
179179
new Key(key, hashCode),
@@ -196,7 +196,7 @@ protected boolean replacePairPrimitive(
196196
RubyConcurrentMap self, Object key, Object expectedValue, Object newValue,
197197
@Exclusive @Cached ToHashByHashCode hashNode,
198198
@Cached ReferenceEqualNode equalNode) {
199-
final int hashCode = hashNode.execute(key);
199+
final int hashCode = hashNode.execute(this, key);
200200
final Key keyWrapper = new Key(key, hashCode);
201201

202202
while (true) {
@@ -216,7 +216,7 @@ protected boolean replacePairPrimitive(
216216
@Specialization(guards = "!isPrimitive(expectedValue)")
217217
protected boolean replacePair(RubyConcurrentMap self, Object key, Object expectedValue, Object newValue,
218218
@Exclusive @Cached ToHashByHashCode hashNode) {
219-
final int hashCode = hashNode.execute(key);
219+
final int hashCode = hashNode.execute(this, key);
220220
return replace(self.getMap(), new Key(key, hashCode), expectedValue, newValue);
221221
}
222222

@@ -233,7 +233,7 @@ public abstract static class DeletePairNode extends CoreMethodArrayArgumentsNode
233233
protected boolean deletePairPrimitive(RubyConcurrentMap self, Object key, Object expectedValue,
234234
@Exclusive @Cached ToHashByHashCode hashNode,
235235
@Cached ReferenceEqualNode equalNode) {
236-
final int hashCode = hashNode.execute(key);
236+
final int hashCode = hashNode.execute(this, key);
237237
final Key keyWrapper = new Key(key, hashCode);
238238

239239
while (true) {
@@ -253,7 +253,7 @@ protected boolean deletePairPrimitive(RubyConcurrentMap self, Object key, Object
253253
@Specialization(guards = "!isPrimitive(expectedValue)")
254254
protected boolean deletePair(RubyConcurrentMap self, Object key, Object expectedValue,
255255
@Exclusive @Cached ToHashByHashCode hashNode) {
256-
final int hashCode = hashNode.execute(key);
256+
final int hashCode = hashNode.execute(this, key);
257257
return remove(self.getMap(), new Key(key, hashCode), expectedValue);
258258
}
259259

@@ -268,7 +268,7 @@ public abstract static class ReplaceIfExistsNode extends CoreMethodArrayArgument
268268
@Specialization
269269
protected Object replaceIfExists(RubyConcurrentMap self, Object key, Object newValue,
270270
@Cached ToHashByHashCode hashNode) {
271-
final int hashCode = hashNode.execute(key);
271+
final int hashCode = hashNode.execute(this, key);
272272
return nullToNil(replace(self.getMap(), new Key(key, hashCode), newValue));
273273
}
274274

@@ -283,7 +283,7 @@ public abstract static class GetAndSetNode extends CoreMethodArrayArgumentsNode
283283
@Specialization
284284
protected Object getAndSet(RubyConcurrentMap self, Object key, Object value,
285285
@Cached ToHashByHashCode hashNode) {
286-
final int hashCode = hashNode.execute(key);
286+
final int hashCode = hashNode.execute(this, key);
287287
return nullToNil(put(self.getMap(), new Key(key, hashCode), value));
288288
}
289289

@@ -298,7 +298,7 @@ public abstract static class KeyNode extends CoreMethodArrayArgumentsNode {
298298
@Specialization
299299
protected boolean key(RubyConcurrentMap self, Object key,
300300
@Cached ToHashByHashCode hashNode) {
301-
final int hashCode = hashNode.execute(key);
301+
final int hashCode = hashNode.execute(this, key);
302302
return containsKey(self.getMap(), new Key(key, hashCode));
303303
}
304304

@@ -313,7 +313,7 @@ public abstract static class DeleteNode extends CoreMethodArrayArgumentsNode {
313313
@Specialization
314314
protected Object delete(RubyConcurrentMap self, Object key,
315315
@Cached ToHashByHashCode hashNode) {
316-
final int hashCode = hashNode.execute(key);
316+
final int hashCode = hashNode.execute(this, key);
317317
return nullToNil(remove(self.getMap(), new Key(key, hashCode)));
318318
}
319319

@@ -347,7 +347,7 @@ public abstract static class GetOrDefaultNode extends CoreMethodArrayArgumentsNo
347347
@Specialization
348348
protected Object getOrDefault(RubyConcurrentMap self, Object key, Object defaultValue,
349349
@Cached ToHashByHashCode hashNode) {
350-
final int hashCode = hashNode.execute(key);
350+
final int hashCode = hashNode.execute(this, key);
351351
return getOrDefault(self.getMap(), new Key(key, hashCode), defaultValue);
352352
}
353353

0 commit comments

Comments
 (0)