Skip to content

Commit b68eb30

Browse files
committed
Improvements: use cached nodes for MakeDict, replace GetItem with BinarySubscript and try to BE result, make method local a const operand of KwargsMerge
1 parent 0f5266b commit b68eb30

File tree

2 files changed

+64
-89
lines changed

2 files changed

+64
-89
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -2144,11 +2144,10 @@ public Void visit(ExprTy.Starred node) {
21442144
@Override
21452145
public Void visit(ExprTy.Subscript node) {
21462146
boolean newStatement = beginSourceSection(node, b);
2147-
2148-
b.beginGetItem();
2147+
b.beginBinarySubscript();
21492148
node.value.accept(this);
21502149
node.slice.accept(this);
2151-
b.endGetItem();
2150+
b.endBinarySubscript();
21522151

21532152
endSourceSection(b, newStatement);
21542153
return null;
@@ -2781,10 +2780,10 @@ public Void visit(ExprTy.Subscript node) {
27812780
b.beginSetItem();
27822781
beginAugAssign();
27832782

2784-
b.beginGetItem();
2783+
b.beginBinarySubscript();
27852784
b.emitLoadLocal(target);
27862785
b.emitLoadLocal(slice);
2787-
b.endGetItem();
2786+
b.endBinarySubscript();
27882787

27892788
value.accept(StatementCompiler.this);
27902789

@@ -2924,10 +2923,9 @@ private void emitKeywordsRecursive(KeywordGroup[] groups, int i, BytecodeLocal f
29242923
if (i == 0) {
29252924
emitKeywordGroup(groups[i], true, function);
29262925
} else {
2927-
b.beginKwargsMerge();
2926+
b.beginKwargsMerge(function);
29282927
emitKeywordsRecursive(groups, i - 1, function);
29292928
emitKeywordGroup(groups[i], false, function);
2930-
b.emitLoadLocal(function);
29312929
b.endKwargsMerge();
29322930
}
29332931
}
@@ -2944,10 +2942,9 @@ private void emitKeywordGroup(KeywordGroup group, boolean copy, BytecodeLocal fu
29442942
SplatKeywords splatKeywords = (SplatKeywords) group;
29452943

29462944
if (copy) {
2947-
b.beginKwargsMerge();
2945+
b.beginKwargsMerge(function);
29482946
b.emitMakeEmptyDict();
29492947
splatKeywords.expr.accept(this);
2950-
b.emitLoadLocal(function);
29512948
b.endKwargsMerge();
29522949
} else {
29532950
splatKeywords.expr.accept(this);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java

+58-80
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
3737
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
3838
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ListGeneralizationNode;
39+
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins;
3940
import com.oracle.graal.python.builtins.objects.dict.DictNodes;
4041
import com.oracle.graal.python.builtins.objects.dict.PDict;
4142
import com.oracle.graal.python.builtins.objects.exception.ChainExceptionsNode;
@@ -100,6 +101,8 @@
100101
import com.oracle.graal.python.nodes.argument.keywords.SameDictKeyException;
101102
import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetFixedAttributeNode;
102103
import com.oracle.graal.python.nodes.builtins.ListNodes;
104+
import com.oracle.graal.python.nodes.bytecode.BinarySubscrSeq;
105+
import com.oracle.graal.python.nodes.bytecode.BinarySubscrSeqFactory;
103106
import com.oracle.graal.python.nodes.bytecode.GetSendValueNode;
104107
import com.oracle.graal.python.nodes.bytecode.GetTPFlagsNode;
105108
import com.oracle.graal.python.nodes.bytecode.GetYieldFromIterNode;
@@ -1119,16 +1122,6 @@ public static Object perform(VirtualFrame frame, Object receiver,
11191122
}
11201123
}
11211124

1122-
@Operation
1123-
public static final class GetItem {
1124-
@Specialization
1125-
public static Object perform(VirtualFrame frame, Object key, Object value,
1126-
@Bind Node inliningTarget,
1127-
@Cached PyObjectGetItem getItemNode) {
1128-
return getItemNode.execute(frame, inliningTarget, key, value);
1129-
}
1130-
}
1131-
11321125
@Operation
11331126
public static final class FormatStr {
11341127
@Specialization
@@ -2232,45 +2225,30 @@ public static PKeyword[] perform(Object sourceCollection,
22322225
@ConstantOperand(type = int.class)
22332226
public static final class MakeDict {
22342227
@Specialization
2228+
@ExplodeLoop
22352229
public static PDict perform(VirtualFrame frame,
22362230
int entries,
22372231
@Variadic Object[] keysAndValues,
22382232
@Bind PBytecodeDSLRootNode rootNode,
2233+
@Cached DictBuiltins.SetItemNode setItemNode,
22392234
@Cached DictNodes.UpdateNode updateNode) {
2240-
PDict dict = rootNode.factory.createDict();
2241-
if (entries <= EXPLODE_LOOP_THRESHOLD) {
2242-
doExploded(frame, keysAndValues, entries, updateNode, dict);
2243-
} else {
2244-
doRegular(frame, keysAndValues, entries, updateNode, dict);
2235+
if (keysAndValues.length != entries * 2) {
2236+
throw CompilerDirectives.shouldNotReachHere();
22452237
}
2246-
return dict;
2247-
}
2248-
2249-
@ExplodeLoop
2250-
private static void doExploded(VirtualFrame frame, Object[] keysAndValues, int entries, DictNodes.UpdateNode updateNode, PDict dict) {
2251-
CompilerAsserts.partialEvaluationConstant(entries);
2238+
PDict dict = rootNode.factory.createDict();
22522239
for (int i = 0; i < entries; i++) {
22532240
Object key = keysAndValues[i * 2];
22542241
Object value = keysAndValues[i * 2 + 1];
2242+
// Each entry represents either a k: v pair or a **splats. splats have no key.
22552243
if (key == PNone.NO_VALUE) {
22562244
updateNode.execute(frame, dict, value);
22572245
} else {
2258-
dict.setItem(key, value);
2246+
setItemNode.execute(frame, dict, key, value);
22592247
}
22602248
}
2249+
return dict;
22612250
}
22622251

2263-
private static void doRegular(VirtualFrame frame, Object[] keysAndValues, int entries, DictNodes.UpdateNode updateNode, PDict dict) {
2264-
for (int i = 0; i < entries; i++) {
2265-
Object key = keysAndValues[i * 2];
2266-
Object value = keysAndValues[i * 2 + 1];
2267-
if (key == PNone.NO_VALUE) {
2268-
updateNode.execute(frame, dict, value);
2269-
} else {
2270-
dict.setItem(key, value);
2271-
}
2272-
}
2273-
}
22742252
}
22752253

22762254
@Operation
@@ -3212,22 +3190,25 @@ private static Object[] doRegular(Object[] values, int length) {
32123190
}
32133191

32143192
@Operation
3193+
@ConstantOperand(type = LocalAccessor.class)
32153194
public static final class KwargsMerge {
32163195
@Specialization
32173196
public static PDict doMerge(VirtualFrame frame,
3197+
LocalAccessor callee,
32183198
PDict dict,
32193199
Object toMerge,
3220-
Object function,
32213200
@Bind PBytecodeDSLRootNode rootNode,
3201+
@Bind BytecodeNode bytecodeNode,
32223202
@Cached ConcatDictToStorageNode concatNode,
32233203
@Cached PRaiseNode raise) {
32243204
try {
32253205
HashingStorage resultStorage = concatNode.execute(frame, dict.getDictStorage(), toMerge);
32263206
dict.setDictStorage(resultStorage);
32273207
} catch (SameDictKeyException e) {
3228-
throw raise.raise(PythonBuiltinClassType.TypeError, ErrorMessages.S_GOT_MULTIPLE_VALUES_FOR_KEYWORD_ARG, PyObjectFunctionStr.execute(function), e.getKey());
3208+
throw raise.raise(PythonBuiltinClassType.TypeError, ErrorMessages.S_GOT_MULTIPLE_VALUES_FOR_KEYWORD_ARG, PyObjectFunctionStr.execute(callee.getObject(bytecodeNode, frame)),
3209+
e.getKey());
32293210
} catch (NonMappingException e) {
3230-
throw raise.raise(PythonBuiltinClassType.TypeError, ErrorMessages.ARG_AFTER_MUST_BE_MAPPING, PyObjectFunctionStr.execute(function), toMerge);
3211+
throw raise.raise(PythonBuiltinClassType.TypeError, ErrorMessages.ARG_AFTER_MUST_BE_MAPPING, PyObjectFunctionStr.execute(callee.getObject(bytecodeNode, frame)), toMerge);
32313212
}
32323213
return dict;
32333214
}
@@ -3814,50 +3795,47 @@ public static boolean doObject(Object value,
38143795
@Operation
38153796
@ImportStatic(PGuards.class)
38163797
public static final class BinarySubscript {
3817-
// TODO: support boxing elimination
3818-
// @Specialization(guards = "cannotBeOverriddenForImmutableType(sequence)")
3819-
// public static int doIntSequence(PList sequence, int index,
3820-
// @Shared("list") @Cached("createForList()") SequenceStorageNodes.GetItemNode getItemNode) throws
3821-
// UnexpectedResultException {
3822-
// return getItemNode.executeInt(sequence.getSequenceStorage(), index);
3823-
// }
3824-
//
3825-
// @Specialization(guards = "cannotBeOverriddenForImmutableType(sequence)")
3826-
// public static int doIntTuple(PTuple sequence, int index,
3827-
// @Shared("tuple") @Cached("createForTuple()") SequenceStorageNodes.GetItemNode getItemNode) throws
3828-
// UnexpectedResultException {
3829-
// return getItemNode.executeInt(sequence.getSequenceStorage(), index);
3830-
// }
3831-
//
3832-
// @Specialization(guards = "cannotBeOverriddenForImmutableType(sequence)")
3833-
// public static double doDoubleSequence(PList sequence, int index,
3834-
// @Shared("list") @Cached("createForList()") SequenceStorageNodes.GetItemNode getItemNode) throws
3835-
// UnexpectedResultException {
3836-
// return getItemNode.executeDouble(sequence.getSequenceStorage(), index);
3837-
// }
3838-
//
3839-
// @Specialization(guards = "cannotBeOverriddenForImmutableType(sequence)")
3840-
// public static double doDoubleTuple(PTuple sequence, int index,
3841-
// @Shared("tuple") @Cached("createForTuple()") SequenceStorageNodes.GetItemNode getItemNode) throws
3842-
// UnexpectedResultException {
3843-
// return getItemNode.executeDouble(sequence.getSequenceStorage(), index);
3844-
// }
3845-
// TODO: add @Shared to GetItemNodes
3846-
3847-
@Specialization(guards = "isBuiltinList(sequence)")
3848-
public static Object doObjectSequence(PList sequence, int index,
3849-
@Cached("createForList()") SequenceStorageNodes.GetItemNode getItemNode) {
3850-
return getItemNode.execute(sequence.getSequenceStorage(), index);
3851-
}
3852-
3853-
@Specialization(guards = "isBuiltinTuple(sequence)")
3854-
public static Object doObjectTuple(PTuple sequence, int index,
3855-
@Cached("createForTuple()") SequenceStorageNodes.GetItemNode getItemNode) {
3856-
return getItemNode.execute(sequence.getSequenceStorage(), index);
3857-
}
3858-
3859-
@Specialization
3860-
public static Object doObjectKey(VirtualFrame frame, Object receiver, Object key,
3798+
// TODO: the result is not BE'd because of the UnexpectedResultException. maybe we should
3799+
// explicitly check for an int storage type?
3800+
@Specialization(rewriteOn = UnexpectedResultException.class)
3801+
public static int doIntList(PList list, int index,
3802+
@Shared @Cached("createForList()") SequenceStorageNodes.GetItemNode getListItemNode) throws UnexpectedResultException {
3803+
return getListItemNode.executeInt(list.getSequenceStorage(), index);
3804+
}
3805+
3806+
@Specialization(rewriteOn = UnexpectedResultException.class)
3807+
public static double doDoubleList(PList list, int index,
3808+
@Shared @Cached("createForList()") SequenceStorageNodes.GetItemNode getListItemNode) throws UnexpectedResultException {
3809+
return getListItemNode.executeDouble(list.getSequenceStorage(), index);
3810+
}
3811+
3812+
@Specialization(replaces = {"doIntList", "doDoubleList"})
3813+
public static Object doObjectList(PList sequence, int index,
3814+
@Shared @Cached("createForList()") SequenceStorageNodes.GetItemNode getListItemNode) {
3815+
return getListItemNode.execute(sequence.getSequenceStorage(), index);
3816+
}
3817+
3818+
@Specialization(rewriteOn = UnexpectedResultException.class)
3819+
public static int doIntTuple(PTuple tuple, int index,
3820+
@Shared @Cached("createForTuple()") SequenceStorageNodes.GetItemNode getTupleItemNode) throws UnexpectedResultException {
3821+
return getTupleItemNode.executeInt(tuple.getSequenceStorage(), index);
3822+
3823+
}
3824+
3825+
@Specialization(rewriteOn = UnexpectedResultException.class)
3826+
public static double doDoubleTuple(PTuple tuple, int index,
3827+
@Shared @Cached("createForTuple()") SequenceStorageNodes.GetItemNode getTupleItemNode) throws UnexpectedResultException {
3828+
return getTupleItemNode.executeDouble(tuple.getSequenceStorage(), index);
3829+
}
3830+
3831+
@Specialization(replaces = {"doIntTuple", "doDoubleTuple"})
3832+
public static Object doObjectTuple(PTuple tuple, int index,
3833+
@Shared @Cached("createForTuple()") SequenceStorageNodes.GetItemNode getTupleItemNode) {
3834+
return getTupleItemNode.execute(tuple.getSequenceStorage(), index);
3835+
}
3836+
3837+
@Fallback
3838+
public static Object doOther(VirtualFrame frame, Object receiver, Object key,
38613839
@Cached(inline = false) PyObjectGetItem getItemNode) {
38623840
return getItemNode.executeCached(frame, receiver, key);
38633841
}

0 commit comments

Comments
 (0)