Skip to content

Commit 57cbf2b

Browse files
committed
[GR-14758] Improve function generation.
PullRequest: graalpython/460
2 parents 98bc613 + dd60409 commit 57cbf2b

File tree

5 files changed

+89
-9
lines changed

5 files changed

+89
-9
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
def count(num):
41+
def foo(n):
42+
def bar():
43+
return n+1
44+
return bar()
45+
46+
last_val = -1
47+
for i in range(num):
48+
last_val = foo(i)
49+
50+
return last_val
51+
52+
53+
def measure(num):
54+
result = count(num)
55+
print("last value: %s " % result)
56+
57+
58+
def __benchmark__(num=10000):
59+
measure(num)
60+

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PFunction.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.oracle.graal.python.builtins.objects.object.PythonObject;
3535
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
3636
import com.oracle.graal.python.nodes.SpecialMethodNames;
37+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
3738
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
3839
import com.oracle.truffle.api.Assumption;
3940
import com.oracle.truffle.api.CompilerAsserts;
@@ -49,8 +50,8 @@ public class PFunction extends PythonObject {
4950
private static final Object[] EMPTY_DEFAULTS = new Object[0];
5051
private final String name;
5152
private final String enclosingClassName;
52-
private final Assumption codeStableAssumption = Truffle.getRuntime().createAssumption("function code unchanged for " + getQualifiedName());
53-
private final Assumption defaultsStableAssumption = Truffle.getRuntime().createAssumption("function defaults unchanged " + getQualifiedName());
53+
private final Assumption codeStableAssumption = Truffle.getRuntime().createAssumption();
54+
private final Assumption defaultsStableAssumption = Truffle.getRuntime().createAssumption();
5455
private final PythonObject globals;
5556
private final PCell[] closure;
5657
private final boolean isStatic;
@@ -64,6 +65,11 @@ public PFunction(LazyPythonClass clazz, String name, String enclosingClassName,
6465

6566
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
6667
PCell[] closure) {
68+
this(clazz, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure, null);
69+
}
70+
71+
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
72+
PCell[] closure, WriteAttributeToDynamicObjectNode writeAttrNode) {
6773
super(clazz);
6874
this.name = name;
6975
this.code = new PCode(PythonBuiltinClassType.PCode, callTarget);
@@ -73,13 +79,17 @@ public PFunction(LazyPythonClass clazz, String name, String enclosingClassName,
7379
this.defaultValues = defaultValues == null ? EMPTY_DEFAULTS : defaultValues;
7480
this.kwDefaultValues = kwDefaultValues == null ? PKeyword.EMPTY_KEYWORDS : kwDefaultValues;
7581
this.closure = closure;
76-
addDefaultConstants(this.getStorage(), name, enclosingClassName);
82+
addDefaultConstants(writeAttrNode, getStorage(), name, enclosingClassName);
7783
}
7884

79-
@TruffleBoundary
80-
private static void addDefaultConstants(DynamicObject storage, String name, String enclosingClassName) {
81-
storage.define(__NAME__, name);
82-
storage.define(__QUALNAME__, enclosingClassName != null ? enclosingClassName + "." + name : name);
85+
private static void addDefaultConstants(WriteAttributeToDynamicObjectNode writeAttrNode, DynamicObject storage, String name, String enclosingClassName) {
86+
if (writeAttrNode != null) {
87+
writeAttrNode.execute(storage, __NAME__, name);
88+
writeAttrNode.execute(storage, __QUALNAME__, enclosingClassName != null ? enclosingClassName + "." + name : name);
89+
} else {
90+
storage.define(__NAME__, name);
91+
storage.define(__QUALNAME__, enclosingClassName != null ? enclosingClassName + "." + name : name);
92+
}
8393
}
8494

8595
public boolean isStatic() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/FunctionDefinitionNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.oracle.graal.python.builtins.objects.function.PFunction;
3232
import com.oracle.graal.python.builtins.objects.function.PKeyword;
3333
import com.oracle.graal.python.nodes.SpecialAttributeNames;
34+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
3435
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
3536
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3637
import com.oracle.graal.python.parser.DefinitionCellSlots;
@@ -53,6 +54,8 @@ public class FunctionDefinitionNode extends ExpressionDefinitionNode {
5354
@Children protected KwDefaultExpressionNode[] kwDefaults;
5455
@Child private ExpressionNode doc;
5556
@Child private WriteAttributeToObjectNode writeDocNode = WriteAttributeToObjectNode.create();
57+
@Child private WriteAttributeToDynamicObjectNode writeNameNode = WriteAttributeToDynamicObjectNode.create();
58+
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
5659

5760
public FunctionDefinitionNode(String functionName, String enclosingClassName, ExpressionNode doc, ExpressionNode[] defaults, KwDefaultExpressionNode[] kwDefaults,
5861
RootCallTarget callTarget,
@@ -74,7 +77,7 @@ protected PythonContext getContext() {
7477
}
7578

7679
protected PythonObjectFactory factory() {
77-
return getContext().getCore().factory();
80+
return factory;
7881
}
7982

8083
private static boolean noNullElements(Object[] array) {
@@ -91,7 +94,7 @@ public Object execute(VirtualFrame frame) {
9194
Object[] defaultValues = computeDefaultValues(frame);
9295
PKeyword[] kwDefaultValues = computeKwDefaultValues(frame);
9396
PCell[] closure = getClosureFromGeneratorOrFunctionLocals(frame);
94-
return withDocString(frame, factory().createFunction(functionName, enclosingClassName, callTarget, PArguments.getGlobals(frame), defaultValues, kwDefaultValues, closure));
97+
return withDocString(frame, factory().createFunction(functionName, enclosingClassName, callTarget, PArguments.getGlobals(frame), defaultValues, kwDefaultValues, closure, writeNameNode));
9598
}
9699

97100
@ExplodeLoop

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
114114
import com.oracle.graal.python.builtins.objects.type.PythonClass;
115115
import com.oracle.graal.python.builtins.objects.zipimporter.PZipImporter;
116+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
116117
import com.oracle.graal.python.parser.ExecutionCellSlots;
117118
import com.oracle.graal.python.runtime.PythonContext;
118119
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
@@ -363,6 +364,11 @@ public PFunction createFunction(String name, String enclosingClassName, RootCall
363364
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure));
364365
}
365366

367+
public PFunction createFunction(String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
368+
PCell[] closure, WriteAttributeToDynamicObjectNode writeAttrNode) {
369+
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure, writeAttrNode));
370+
}
371+
366372
public PBuiltinFunction createBuiltinFunction(String name, LazyPythonClass type, int numDefaults, RootCallTarget callTarget) {
367373
return trace(new PBuiltinFunction(PythonBuiltinClassType.PBuiltinFunction, name, type, numDefaults, callTarget));
368374
}

mx.graalpython/mx_graalpython_bench_param.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
'call-classmethod': ITER_15 + ['50000000'],
9999
'mmap-anonymous': ITER_15 + ['1000'],
100100
'mmap-file': ITER_15 + ['1000'],
101+
'generate-functions': ITER_15 + ['10000000'],
101102
}
102103

103104

0 commit comments

Comments
 (0)