Skip to content

Commit 1c65a11

Browse files
committed
Inline PyIterNextNode for interp performance.
1 parent e38a854 commit 1c65a11

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
import com.oracle.graal.python.builtins.objects.ints.IntBuiltins;
5050
import com.oracle.graal.python.builtins.objects.ints.PInt;
5151
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
52+
import com.oracle.graal.python.builtins.objects.type.TpSlots;
53+
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot;
54+
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotIterNext;
5255
import com.oracle.graal.python.lib.IteratorExhausted;
5356
import com.oracle.graal.python.lib.PyBoolCheckNode;
5457
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
@@ -869,24 +872,38 @@ protected ArgumentClinicProvider getArgumentClinic() {
869872
@Builtin(name = "fsum", minNumOfPositionalArgs = 1)
870873
@GenerateNodeFactory
871874
public abstract static class FsumNode extends PythonUnaryBuiltinNode {
875+
876+
/**
877+
* Note: this specialization uses an inlined version of {@link PyIterNextNode} with the
878+
* tp_iternext slot moved out of the loop.
879+
*/
872880
@Specialization
873881
static double doIt(VirtualFrame frame, Object iterable,
874882
@Bind Node inliningTarget,
875883
@Cached PyObjectGetIter getIter,
876-
@Cached PyIterNextNode nextNode,
884+
@Cached GetClassNode nextNodeGetClassNode,
885+
@Cached TpSlots.GetCachedTpSlotsNode nextNodeGetSlots,
886+
@Cached TpSlotIterNext.CallSlotTpIterNextNode nextNodeCallNext,
887+
@Cached IsBuiltinObjectProfile nextNodeStopIterationProfile,
877888
@Cached PyFloatAsDoubleNode asDoubleNode,
878889
@Cached PRaiseNode raiseNode) {
879890
Object iterator = getIter.execute(frame, inliningTarget, iterable);
880891

892+
TpSlot tpIternext = nextNodeGetSlots.execute(inliningTarget, nextNodeGetClassNode.execute(inliningTarget, iterator)).tp_iternext();
893+
assert tpIternext != null;
894+
881895
var acc = new XSum.SmallAccumulator();
882896
int nbrIter = 0;
883897
while (true) {
884898
try {
885-
Object next = nextNode.execute(frame, inliningTarget, iterator);
899+
Object next = nextNodeCallNext.execute(frame, inliningTarget, tpIternext, iterator);
886900
nbrIter++;
887901
acc.add(asDoubleNode.execute(frame, inliningTarget, next));
888902
} catch (IteratorExhausted e) {
889903
break;
904+
} catch (PException e) {
905+
e.expectStopIteration(inliningTarget, nextNodeStopIterationProfile);
906+
break;
890907
} finally {
891908
LoopNode.reportLoopCount(inliningTarget, nbrIter);
892909
}

0 commit comments

Comments
 (0)