Skip to content

Commit 999a761

Browse files
committed
Refactor DeletePairNode to reduce amount of generated code
1 parent f8cf9cc commit 999a761

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected static int castBignum(RubyBignum hashed) {
169169
@Specialization(guards = "!isRubyInteger(hashed)")
170170
protected static int castOther(Node node, Object hashed,
171171
@Cached ToRubyIntegerNode toRubyInteger,
172-
//recursive inlining is not supported
172+
// recursive inlining is not supported
173173
@Cached(inline = false) HashCastResultNode hashCastResult) {
174174
return hashCastResult.executeCached(toRubyInteger.execute(node, hashed));
175175
}

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
package org.truffleruby.extra;
1111

1212
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
13-
import com.oracle.truffle.api.dsl.Bind;
1413
import com.oracle.truffle.api.dsl.Cached;
15-
import com.oracle.truffle.api.dsl.Cached.Exclusive;
1614
import com.oracle.truffle.api.dsl.Specialization;
17-
import com.oracle.truffle.api.nodes.Node;
1815
import com.oracle.truffle.api.object.Shape;
1916
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
2017
import org.truffleruby.annotations.CoreMethod;
@@ -31,6 +28,7 @@
3128
import org.truffleruby.extra.AtomicReferenceNodes.CompareAndSetReferenceNode;
3229
import org.truffleruby.extra.RubyConcurrentMap.Key;
3330
import org.truffleruby.annotations.Visibility;
31+
import org.truffleruby.language.RubyGuards;
3432
import org.truffleruby.language.objects.AllocationTracing;
3533
import org.truffleruby.language.yield.CallBlockNode;
3634

@@ -39,8 +37,6 @@
3937
import java.util.concurrent.ConcurrentHashMap;
4038
import java.util.function.BiFunction;
4139

42-
import static org.truffleruby.language.RubyGuards.isPrimitive;
43-
4440
@CoreModule(value = "TruffleRuby::ConcurrentMap", isClass = true)
4541
public class ConcurrentMapNodes {
4642

@@ -202,7 +198,7 @@ protected boolean replacePair(RubyConcurrentMap self, Object key, Object expecte
202198
@Cached InlinedConditionProfile isPrimitiveProfile) {
203199
final int hashCode = hashNode.execute(this, key);
204200

205-
if (isPrimitiveProfile.profile(this, isPrimitive(expectedValue))) {
201+
if (isPrimitiveProfile.profile(this, RubyGuards.isPrimitive(expectedValue))) {
206202
return replacePairPrimitive(self, key, expectedValue, newValue, hashCode, equalNode);
207203
} else {
208204
return replace(self.getMap(), new Key(key, hashCode), expectedValue, newValue);
@@ -216,7 +212,7 @@ private boolean replacePairPrimitive(RubyConcurrentMap self, Object key, Object
216212
while (true) {
217213
final Object currentValue = get(self.getMap(), keyWrapper);
218214

219-
if (isPrimitive(currentValue) &&
215+
if (RubyGuards.isPrimitive(currentValue) &&
220216
equalNode.execute(expectedValue, currentValue)) {
221217
if (replace(self.getMap(), keyWrapper, currentValue, newValue)) {
222218
return true;
@@ -236,11 +232,23 @@ private static boolean replace(ConcurrentHashMap<Key, Object> map, Key key, Obje
236232
@CoreMethod(names = "delete_pair", required = 2)
237233
public abstract static class DeletePairNode extends CoreMethodArrayArgumentsNode {
238234
/** See {@link CompareAndSetReferenceNode} */
239-
@Specialization(guards = "isPrimitive(expectedValue)")
240-
protected boolean deletePairPrimitive(RubyConcurrentMap self, Object key, Object expectedValue,
241-
@Exclusive @Cached ToHashByHashCode hashNode,
242-
@Cached ReferenceEqualNode equalNode) {
235+
@Specialization
236+
protected boolean deletePair(RubyConcurrentMap self, Object key, Object expectedValue,
237+
@Cached ToHashByHashCode hashNode,
238+
@Cached ReferenceEqualNode equalNode,
239+
@Cached InlinedConditionProfile isPrimitiveProfile) {
240+
243241
final int hashCode = hashNode.execute(this, key);
242+
243+
if (isPrimitiveProfile.profile(this, RubyGuards.isPrimitive(expectedValue))) {
244+
return deletePairPrimitive(self, key, expectedValue, hashCode, equalNode);
245+
} else {
246+
return remove(self.getMap(), new Key(key, hashCode), expectedValue);
247+
}
248+
}
249+
250+
private boolean deletePairPrimitive(RubyConcurrentMap self, Object key, Object expectedValue, int hashCode,
251+
ReferenceEqualNode equalNode) {
244252
final Key keyWrapper = new Key(key, hashCode);
245253

246254
while (true) {
@@ -257,14 +265,6 @@ protected boolean deletePairPrimitive(RubyConcurrentMap self, Object key, Object
257265
}
258266
}
259267

260-
@Specialization(guards = "!isPrimitive(expectedValue)")
261-
protected static boolean deletePair(RubyConcurrentMap self, Object key, Object expectedValue,
262-
@Exclusive @Cached ToHashByHashCode hashNode,
263-
@Bind("this") Node node) {
264-
final int hashCode = hashNode.execute(node, key);
265-
return remove(self.getMap(), new Key(key, hashCode), expectedValue);
266-
}
267-
268268
@TruffleBoundary
269269
private static boolean remove(ConcurrentHashMap<Key, Object> map, Key key, Object expectedValue) {
270270
return map.remove(key, expectedValue);

0 commit comments

Comments
 (0)