Skip to content

Commit 38e16b0

Browse files
raph-amiardHugoGGuerrier
authored andcommitted
Perf: Reoptimize method call targets
They were not optimized anymore due to the wrapper method object being different every time
1 parent bda6a81 commit 38e16b0

File tree

7 files changed

+59
-32
lines changed

7 files changed

+59
-32
lines changed

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/BuiltInFunctionValue.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ public BuiltInFunctionValue(
4141
stringDefaultVals = defaultValues;
4242
}
4343

44+
public BuiltInFunctionValue(
45+
String documentation,
46+
String[] names,
47+
String[] defaultValues,
48+
FunctionRootNode functionRootNode
49+
) {
50+
super(
51+
functionRootNode,
52+
Closure.EMPTY,
53+
functionRootNode.getName(),
54+
documentation,
55+
names,
56+
new Expr[names.length]
57+
);
58+
stringDefaultVals = defaultValues;
59+
}
60+
4461
@Override
4562
public Expr[] getParameterDefaultValues() {
4663
if (stringDefaultVals != null && !defaultValsEvald) {

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/BuiltInMethodFactory.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.adacore.lkql_jit.built_ins;
77

8+
import com.adacore.lkql_jit.nodes.root_nodes.FunctionRootNode;
89
import com.adacore.lkql_jit.utils.functions.ArrayUtils;
910

1011
/**
@@ -21,6 +22,8 @@ public final class BuiltInMethodFactory {
2122

2223
public final String[] paramNames;
2324

25+
private final FunctionRootNode functionRootNode;
26+
2427
public String[] defaultValues;
2528

2629
public final BuiltInBody methodBody;
@@ -48,20 +51,20 @@ public BuiltInMethodFactory(
4851
this.defaultValues = defaultValues;
4952
this.methodBody = methodBody;
5053
this.isProperty = isProperty;
54+
this.functionRootNode = new FunctionRootNode(null, null, false, methodBody, name);
5155
}
5256

5357
// ----- Instance methods -----
5458

5559
/** Instantiate the method with the given "thisValue" and return the LKQL value. */
5660
public BuiltInMethodValue instantiate(Object thisValue) {
5761
return this.isProperty
58-
? new BuiltInPropertyValue(this.name, this.documentation, this.methodBody, thisValue)
62+
? new BuiltInPropertyValue(documentation, functionRootNode, thisValue)
5963
: new BuiltInMethodValue(
60-
this.name,
61-
this.documentation,
62-
this.paramNames,
63-
this.defaultValues,
64-
this.methodBody,
64+
documentation,
65+
paramNames,
66+
defaultValues,
67+
functionRootNode,
6568
thisValue
6669
);
6770
}

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/BuiltInMethodValue.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package com.adacore.lkql_jit.built_ins;
77

8+
import com.adacore.lkql_jit.nodes.root_nodes.FunctionRootNode;
9+
810
/** This class represents the LKQL value of an instantiated built-in method. */
911
public class BuiltInMethodValue extends BuiltInFunctionValue {
1012

@@ -20,14 +22,13 @@ public class BuiltInMethodValue extends BuiltInFunctionValue {
2022
* "this" value.
2123
*/
2224
public BuiltInMethodValue(
23-
String name,
2425
String documentation,
2526
String[] names,
2627
String[] defaultValues,
27-
BuiltInBody body,
28+
FunctionRootNode functionRootNode,
2829
Object thisValue
2930
) {
30-
super(name, documentation, names, defaultValues, body);
31+
super(documentation, names, defaultValues, functionRootNode);
3132
this.thisValue = thisValue;
3233
}
3334
}

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/BuiltInPropertyValue.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package com.adacore.lkql_jit.built_ins;
77

8+
import com.adacore.lkql_jit.nodes.root_nodes.FunctionRootNode;
9+
810
/**
911
* This class represents the LKQL value of an instantiated attribute. An attribute is a special
1012
* method with not other arguments than "this" and is called implicitly by the dotted-name notation.
@@ -13,11 +15,16 @@ public class BuiltInPropertyValue extends BuiltInMethodValue {
1315

1416
/** Create a new built-in attribute value. */
1517
public BuiltInPropertyValue(
16-
String name,
1718
String documentation,
18-
BuiltInBody body,
19+
FunctionRootNode functionRootNode,
1920
Object thisValue
2021
) {
21-
super(name, documentation, new String[] { "this" }, new String[] { null }, body, thisValue);
22+
super(
23+
documentation,
24+
new String[] { "this" },
25+
new String[] { null },
26+
functionRootNode,
27+
thisValue
28+
);
2229
}
2330
}

lkql_jit/language/src/main/java/com/adacore/lkql_jit/nodes/expressions/dot/DotAccess.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ protected Object onNamespace(
7979

8080
/**
8181
* Execute the dot access on a node with the cached strategy.
82-
*
83-
* @param property The cached property reference.
84-
* @param isField The cached value if the property is a field.
85-
* @return The property reference or the field value.
8682
*/
8783
@Specialization(
8884
guards = {
@@ -110,8 +106,6 @@ protected Object onNodeCached(
110106

111107
/**
112108
* Execute the dot access on a node with the un-cached strategy.
113-
*
114-
* @return The property call or the file value.
115109
*/
116110
@Specialization(replaces = "onNodeCached")
117111
protected Object onNodeUncached(LangkitSupport.NodeInterface receiver) {

lkql_jit/language/src/main/java/com/adacore/lkql_jit/nodes/expressions/dot/DotAccessWrapper.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
package com.adacore.lkql_jit.nodes.expressions.dot;
77

88
import com.adacore.lkql_jit.built_ins.BuiltInPropertyValue;
9-
import com.adacore.lkql_jit.exception.LKQLRuntimeException;
109
import com.adacore.lkql_jit.nodes.expressions.Expr;
1110
import com.adacore.lkql_jit.utils.Constants;
11+
import com.oracle.truffle.api.dsl.Cached;
1212
import com.oracle.truffle.api.dsl.Fallback;
1313
import com.oracle.truffle.api.dsl.NodeChild;
1414
import com.oracle.truffle.api.dsl.Specialization;
15-
import com.oracle.truffle.api.interop.ArityException;
16-
import com.oracle.truffle.api.interop.InteropLibrary;
17-
import com.oracle.truffle.api.interop.UnsupportedMessageException;
18-
import com.oracle.truffle.api.interop.UnsupportedTypeException;
19-
import com.oracle.truffle.api.library.CachedLibrary;
15+
import com.oracle.truffle.api.nodes.DirectCallNode;
16+
import com.oracle.truffle.api.nodes.IndirectCallNode;
2017
import com.oracle.truffle.api.source.SourceSection;
2118

2219
/**
@@ -40,16 +37,23 @@ public DotAccessWrapper(SourceSection location) {
4037
* When the child dot-access is returning an attribute, execute this attribute implicitly and
4138
* return the result of this execution.
4239
*/
43-
@Specialization(limit = Constants.SPECIALIZED_LIB_LIMIT)
44-
protected Object onAttribute(
40+
@Specialization(
41+
limit = Constants.SPECIALIZED_LIB_LIMIT,
42+
guards = "attribute.getCallTarget() == directCallNode.getCallTarget()"
43+
)
44+
protected Object doCached(
4545
BuiltInPropertyValue attribute,
46-
@CachedLibrary("attribute") InteropLibrary attributeLibrary
46+
@Cached("create(attribute.getCallTarget())") DirectCallNode directCallNode
4747
) {
48-
try {
49-
return attributeLibrary.execute(attribute, attribute.thisValue);
50-
} catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) {
51-
throw LKQLRuntimeException.fromJavaException(e, this);
52-
}
48+
return directCallNode.call(attribute.thisValue);
49+
}
50+
51+
@Specialization(replaces = "doCached")
52+
protected Object doUncached(
53+
BuiltInPropertyValue attribute,
54+
@Cached IndirectCallNode indirectCallNode
55+
) {
56+
return indirectCallNode.call(attribute.getCallTarget(), attribute.thisValue);
5357
}
5458

5559
/** For other cases, just return the dot access result value. */

lkql_jit/language/src/main/java/com/adacore/lkql_jit/nodes/root_nodes/SelectorRootNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public Object execute(VirtualFrame frame) {
8686
LKQLRecValue res;
8787

8888
var val = body.executeGeneric(frame);
89+
8990
if (LKQLTypeSystemGen.isLKQLRecValue(val)) {
9091
res = LKQLTypeSystemGen.asLKQLRecValue(val);
9192
res.depth = (int) depth + 1;

0 commit comments

Comments
 (0)