Skip to content

Commit 21b95e6

Browse files
committed
AST-inline Array#[]
1 parent 6105b1e commit 21b95e6

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/main/java/org/truffleruby/core/inlined/CoreMethods.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class CoreMethods {
8888
public final InternalMethod MODULE_CASE_EQUAL;
8989
public final InternalMethod STRING_EQUAL;
9090
public final InternalMethod SYMBOL_TO_PROC;
91+
public final InternalMethod ARRAY_INDEX_GET;
9192

9293
public CoreMethods(RubyContext context) {
9394
this.context = context;
@@ -100,6 +101,7 @@ public CoreMethods(RubyContext context) {
100101
final RubyClass nilClass = context.getCoreLibrary().nilClass;
101102
final RubyClass stringClass = context.getCoreLibrary().stringClass;
102103
final RubyClass symbolClass = context.getCoreLibrary().symbolClass;
104+
final RubyClass arrayClass = context.getCoreLibrary().arrayClass;
103105

104106
integerNegAssumption = registerAssumption(integerClass, "-@");
105107
floatNegAssumption = registerAssumption(floatClass, "-@");
@@ -150,6 +152,7 @@ public CoreMethods(RubyContext context) {
150152
MODULE_CASE_EQUAL = getMethod(moduleClass, "===");
151153
STRING_EQUAL = getMethod(stringClass, "==");
152154
SYMBOL_TO_PROC = getMethod(symbolClass, "to_proc");
155+
ARRAY_INDEX_GET = getMethod(arrayClass, "[]");
153156
}
154157

155158
private Assumption registerAssumption(RubyClass klass, String methodName) {
@@ -238,6 +241,8 @@ public RubyNode createCallNode(RubyCallNodeParameters callParameters, Translator
238241
return InlinedGreaterThanNodeGen.create(context, callParameters, self, args[0]);
239242
case ">=":
240243
return InlinedGreaterOrEqualNodeGen.create(context, callParameters, self, args[0]);
244+
case "[]":
245+
return InlinedIndexGetNodeGen.create(context, callParameters, self, args[0]);
241246
case "is_a?":
242247
return InlinedIsANodeGen.create(context, callParameters, self, args[0]);
243248
case "kind_of?":
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.core.inlined;
11+
12+
import com.oracle.truffle.api.profiles.ConditionProfile;
13+
import org.truffleruby.RubyContext;
14+
import org.truffleruby.core.array.ArrayIndexNodes;
15+
import org.truffleruby.core.array.RubyArray;
16+
import org.truffleruby.language.dispatch.RubyCallNodeParameters;
17+
import org.truffleruby.language.methods.LookupMethodOnSelfNode;
18+
19+
import com.oracle.truffle.api.dsl.Cached;
20+
import com.oracle.truffle.api.dsl.Specialization;
21+
import com.oracle.truffle.api.frame.VirtualFrame;
22+
23+
public abstract class InlinedIndexGetNode extends BinaryInlinedOperationNode {
24+
25+
protected static final String METHOD = "[]";
26+
27+
public InlinedIndexGetNode(RubyContext context, RubyCallNodeParameters callNodeParameters) {
28+
super(context, callNodeParameters);
29+
}
30+
31+
@Specialization(
32+
guards = "lookupNode.lookupProtected(frame, array, METHOD) == coreMethods().ARRAY_INDEX_GET",
33+
assumptions = "assumptions",
34+
limit = "1")
35+
protected Object arrayRead(VirtualFrame frame, RubyArray array, int index,
36+
@Cached LookupMethodOnSelfNode lookupNode,
37+
@Cached ArrayIndexNodes.ReadNormalizedNode readNormalizedNode,
38+
@Cached ConditionProfile denormalized) {
39+
if (denormalized.profile(index < 0)) {
40+
index += array.size;
41+
}
42+
return readNormalizedNode.executeRead(array, index);
43+
}
44+
45+
@Specialization
46+
protected Object fallback(VirtualFrame frame, Object a, Object b) {
47+
return rewriteAndCall(frame, a, b);
48+
}
49+
50+
}

src/main/java/org/truffleruby/language/methods/LookupMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected InternalMethod lookupMethodCached(
6767
@Cached("contextReference.get()") RubyContext cachedContext,
6868
@Cached("metaClass") RubyClass cachedMetaClass,
6969
@Cached("name") String cachedName,
70-
@Cached(value = "config", allowUncached = true) DispatchConfiguration cachedConfig,
70+
@Cached("config") DispatchConfiguration cachedConfig,
7171
@Cached("lookupCached(cachedContext, frame, cachedMetaClass, cachedName, config)") MethodLookupResult methodLookupResult) {
7272

7373
return methodLookupResult.getMethod();

0 commit comments

Comments
 (0)