Skip to content

Commit 396f3df

Browse files
committed
Merge specialization in ReplacePairNode to improve code size
1 parent 5a28177 commit 396f3df

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.oracle.truffle.api.dsl.Specialization;
1717
import com.oracle.truffle.api.nodes.Node;
1818
import com.oracle.truffle.api.object.Shape;
19+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
1920
import org.truffleruby.annotations.CoreMethod;
2021
import org.truffleruby.annotations.CoreModule;
2122
import org.truffleruby.annotations.Primitive;
@@ -29,7 +30,6 @@
2930
import org.truffleruby.core.proc.RubyProc;
3031
import org.truffleruby.extra.AtomicReferenceNodes.CompareAndSetReferenceNode;
3132
import org.truffleruby.extra.RubyConcurrentMap.Key;
32-
import org.truffleruby.language.RubyGuards;
3333
import org.truffleruby.annotations.Visibility;
3434
import org.truffleruby.language.objects.AllocationTracing;
3535
import org.truffleruby.language.yield.CallBlockNode;
@@ -39,6 +39,8 @@
3939
import java.util.concurrent.ConcurrentHashMap;
4040
import java.util.function.BiFunction;
4141

42+
import static org.truffleruby.language.RubyGuards.isPrimitive;
43+
4244
@CoreModule(value = "TruffleRuby::ConcurrentMap", isClass = true)
4345
public class ConcurrentMapNodes {
4446

@@ -192,19 +194,29 @@ private static Object merge(ConcurrentHashMap<Key, Object> map, Key key, Object
192194

193195
@CoreMethod(names = "replace_pair", required = 3)
194196
public abstract static class ReplacePairNode extends CoreMethodArrayArgumentsNode {
195-
/** See {@link CompareAndSetReferenceNode} */
196-
@Specialization(guards = "isPrimitive(expectedValue)")
197-
protected boolean replacePairPrimitive(
198-
RubyConcurrentMap self, Object key, Object expectedValue, Object newValue,
199-
@Exclusive @Cached ToHashByHashCode hashNode,
200-
@Cached ReferenceEqualNode equalNode) {
197+
198+
@Specialization
199+
protected boolean replacePair(RubyConcurrentMap self, Object key, Object expectedValue, Object newValue,
200+
@Cached ToHashByHashCode hashNode,
201+
@Cached ReferenceEqualNode equalNode,
202+
@Cached InlinedConditionProfile isPrimitiveProfile) {
201203
final int hashCode = hashNode.execute(this, key);
204+
205+
if (isPrimitiveProfile.profile(this, isPrimitive(expectedValue))) {
206+
return replacePairPrimitive(self, key, expectedValue, newValue, hashCode, equalNode);
207+
} else {
208+
return replace(self.getMap(), new Key(key, hashCode), expectedValue, newValue);
209+
}
210+
}
211+
212+
private boolean replacePairPrimitive(RubyConcurrentMap self, Object key, Object expectedValue, Object newValue,
213+
int hashCode, ReferenceEqualNode equalNode) {
202214
final Key keyWrapper = new Key(key, hashCode);
203215

204216
while (true) {
205217
final Object currentValue = get(self.getMap(), keyWrapper);
206218

207-
if (RubyGuards.isPrimitive(currentValue) &&
219+
if (isPrimitive(currentValue) &&
208220
equalNode.execute(expectedValue, currentValue)) {
209221
if (replace(self.getMap(), keyWrapper, currentValue, newValue)) {
210222
return true;
@@ -215,14 +227,6 @@ protected boolean replacePairPrimitive(
215227
}
216228
}
217229

218-
@Specialization(guards = "!isPrimitive(expectedValue)")
219-
protected static boolean replacePair(RubyConcurrentMap self, Object key, Object expectedValue, Object newValue,
220-
@Exclusive @Cached ToHashByHashCode hashNode,
221-
@Bind("this") Node node) {
222-
final int hashCode = hashNode.execute(node, key);
223-
return replace(self.getMap(), new Key(key, hashCode), expectedValue, newValue);
224-
}
225-
226230
@TruffleBoundary
227231
private static boolean replace(ConcurrentHashMap<Key, Object> map, Key key, Object oldValue, Object newValue) {
228232
return map.replace(key, oldValue, newValue);

0 commit comments

Comments
 (0)