Skip to content

Commit 561c8f8

Browse files
committed
only create and write the annotations dict if there are any
1 parent 51f63d8 commit 561c8f8

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@
2525
*/
2626
package com.oracle.graal.python.nodes.function;
2727

28+
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__ANNOTATIONS__;
29+
30+
import java.util.Map;
31+
2832
import com.oracle.graal.python.PythonLanguage;
2933
import com.oracle.graal.python.builtins.objects.cell.PCell;
34+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
3035
import com.oracle.graal.python.builtins.objects.dict.PDict;
3136
import com.oracle.graal.python.builtins.objects.function.PArguments;
3237
import com.oracle.graal.python.builtins.objects.function.PFunction;
3338
import com.oracle.graal.python.builtins.objects.function.PKeyword;
3439
import com.oracle.graal.python.nodes.SpecialAttributeNames;
35-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__ANNOTATIONS__;
3640
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
3741
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
3842
import com.oracle.graal.python.nodes.expression.ExpressionNode;
@@ -49,7 +53,6 @@
4953
import com.oracle.truffle.api.frame.VirtualFrame;
5054
import com.oracle.truffle.api.nodes.ExplodeLoop;
5155
import com.oracle.truffle.api.nodes.RootNode;
52-
import java.util.Map;
5356

5457
public class FunctionDefinitionNode extends ExpressionDefinitionNode {
5558
@CompilationFinal private ContextReference<PythonContext> contextRef;
@@ -63,6 +66,7 @@ public class FunctionDefinitionNode extends ExpressionDefinitionNode {
6366
@Child private WriteAttributeToObjectNode writeAttrNode = WriteAttributeToObjectNode.create();
6467
@Child private WriteAttributeToDynamicObjectNode writeNameNode = WriteAttributeToDynamicObjectNode.create();
6568
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
69+
@Child private HashingCollectionNodes.SetItemNode setItemNode;
6670

6771
@CompilerDirectives.CompilationFinal(dimensions = 1) private final String[] annotationNames;
6872
@Children private ExpressionNode[] annotationTypes;
@@ -134,10 +138,8 @@ public Object execute(VirtualFrame frame) {
134138

135139
// Processing annotated arguments.
136140
// The __annotations__ dictionary is created even there are is not any annotated arg.
137-
PDict annotations = factory().createDict();
138-
writeAttrNode.execute(func, __ANNOTATIONS__, annotations);
139141
if (annotationNames != null) {
140-
writeAnnotations(frame, annotations);
142+
writeAnnotations(frame, func);
141143
}
142144
return func;
143145
}
@@ -175,12 +177,18 @@ private Object[] computeDefaultValues(VirtualFrame frame) {
175177
}
176178

177179
@ExplodeLoop
178-
private void writeAnnotations(VirtualFrame frame, PDict annotations) {
180+
private void writeAnnotations(VirtualFrame frame, PFunction func) {
181+
PDict annotations = factory().createDict();
182+
writeAttrNode.execute(func, __ANNOTATIONS__, annotations);
183+
if (setItemNode == null) {
184+
CompilerDirectives.transferToInterpreterAndInvalidate();
185+
setItemNode = insert(HashingCollectionNodes.SetItemNode.create());
186+
}
179187
for (int i = 0; i < annotationNames.length; i++) {
180188
// compute the types of the arg
181189
Object type = annotationTypes[i].execute(frame);
182190
// set the annotations
183-
annotations.setItem(annotationNames[i], type);
191+
setItemNode.execute(frame, annotations, annotationNames[i], type);
184192
}
185193
}
186194

0 commit comments

Comments
 (0)