Skip to content

Commit 9af3956

Browse files
eregonandrykonchin
authored andcommitted
No need to subtract keys if no keys for pattern matching
1 parent 7b70fa8 commit 9af3956

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public abstract class HashSubtractKeysNode extends RubyContextSourceNode impleme
2929
@Child private HashStoreLibrary hashes = HashStoreLibrary.createDispatched();
3030

3131
public HashSubtractKeysNode(RubySymbol[] excludedKeys) {
32+
assert excludedKeys.length > 0 : "unnecessary";
3233
this.excludedKeys = excludedKeys;
3334
}
3435

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,13 @@ public RubyNode visitHashPatternNode(Nodes.HashPatternNode node) {
249249

250250
var rest = node.rest;
251251
if (rest != null) {
252+
RubyNode withoutMatchedKeys = keys.length == 0
253+
? readTemp
254+
: HashSubtractKeysNodeGen.create(keys, readTemp);
252255
if (rest instanceof Nodes.AssocSplatNode assocSplatNode) {
253256
if (assocSplatNode.value != null) {
254257
RubyNode prev = currentValueToMatch;
255-
currentValueToMatch = HashSubtractKeysNodeGen.create(keys, readTemp);
258+
currentValueToMatch = withoutMatchedKeys;
256259
try {
257260
condition = AndNodeGen.create(condition, assocSplatNode.value.accept(this));
258261
} finally {
@@ -262,8 +265,7 @@ public RubyNode visitHashPatternNode(Nodes.HashPatternNode node) {
262265
// nothing
263266
}
264267
} else if (rest instanceof Nodes.NoKeywordsParameterNode) {
265-
condition = AndNodeGen.create(condition,
266-
new HashIsEmptyNode(HashSubtractKeysNodeGen.create(keys, readTemp)));
268+
condition = AndNodeGen.create(condition, new HashIsEmptyNode(withoutMatchedKeys));
267269
} else {
268270
throw fail(rest);
269271
}

0 commit comments

Comments
 (0)