Skip to content

Commit fdaa660

Browse files
committed
Simplify specializations in MathModuleBuiltins
1 parent 9f6291d commit fdaa660

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
*/
2626
package com.oracle.graal.python.builtins.modules;
2727

28-
import com.oracle.truffle.api.dsl.NeverDefault;
29-
3028
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
3129
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
3230
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
@@ -59,6 +57,7 @@
5957
import com.oracle.graal.python.lib.PyNumberIndexNode;
6058
import com.oracle.graal.python.lib.PyObjectGetIter;
6159
import com.oracle.graal.python.nodes.ErrorMessages;
60+
import com.oracle.graal.python.nodes.PGuards;
6261
import com.oracle.graal.python.nodes.PNodeWithRaise;
6362
import com.oracle.graal.python.nodes.builtins.TupleNodes;
6463
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
@@ -75,8 +74,8 @@
7574
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
7675
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
7776
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
77+
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinObjectProfile;
7878
import com.oracle.graal.python.nodes.object.GetClassNode;
79-
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
8079
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
8180
import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode;
8281
import com.oracle.graal.python.nodes.util.NarrowBigIntegerNode;
@@ -86,16 +85,20 @@
8685
import com.oracle.truffle.api.CompilerDirectives;
8786
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
8887
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
88+
import com.oracle.truffle.api.dsl.Bind;
8989
import com.oracle.truffle.api.dsl.Cached;
9090
import com.oracle.truffle.api.dsl.Cached.Shared;
9191
import com.oracle.truffle.api.dsl.Fallback;
9292
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
9393
import com.oracle.truffle.api.dsl.ImportStatic;
94+
import com.oracle.truffle.api.dsl.NeverDefault;
9495
import com.oracle.truffle.api.dsl.NodeFactory;
9596
import com.oracle.truffle.api.dsl.Specialization;
9697
import com.oracle.truffle.api.dsl.TypeSystemReference;
9798
import com.oracle.truffle.api.frame.VirtualFrame;
99+
import com.oracle.truffle.api.nodes.Node;
98100
import com.oracle.truffle.api.profiles.ConditionProfile;
101+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
99102
import com.oracle.truffle.api.profiles.LoopConditionProfile;
100103

101104
@CoreFunctions(defineModule = "math")
@@ -838,12 +841,13 @@ public abstract static class FsumNode extends PythonUnaryBuiltinNode {
838841

839842
@Specialization
840843
double doIt(VirtualFrame frame, Object iterable,
844+
@Bind("this") Node inliningTarget,
841845
@Cached PyObjectGetIter getIter,
842846
@Cached("create(Next)") LookupAndCallUnaryNode callNextNode,
843847
@Cached PyFloatAsDoubleNode asDoubleNode,
844-
@Cached IsBuiltinClassProfile stopProfile) {
848+
@Cached IsBuiltinObjectProfile stopProfile) {
845849
Object iterator = getIter.execute(frame, iterable);
846-
return fsum(frame, iterator, callNextNode, asDoubleNode, stopProfile);
850+
return fsum(frame, iterator, callNextNode, asDoubleNode, inliningTarget, stopProfile);
847851
}
848852

849853
/*
@@ -856,7 +860,8 @@ public abstract static class FsumNode extends PythonUnaryBuiltinNode {
856860
* is little bit faster. The testFSum in test_math.py takes in different implementations:
857861
* CPython ~0.6s CurrentImpl: ~14.3s Using BigDecimal: ~15.1
858862
*/
859-
private double fsum(VirtualFrame frame, Object iterator, LookupAndCallUnaryNode next, PyFloatAsDoubleNode asDoubleNode, IsBuiltinClassProfile stopProfile) {
863+
private double fsum(VirtualFrame frame, Object iterator, LookupAndCallUnaryNode next,
864+
PyFloatAsDoubleNode asDoubleNode, Node inliningTarget, IsBuiltinObjectProfile stopProfile) {
860865
double x, y, t, hi, lo = 0, yr, inf_sum = 0, special_sum = 0, sum;
861866
double xsave;
862867
int i, j, n = 0, arayLength = 32;
@@ -865,7 +870,7 @@ private double fsum(VirtualFrame frame, Object iterator, LookupAndCallUnaryNode
865870
try {
866871
x = asDoubleNode.execute(frame, next.executeObject(frame, iterator));
867872
} catch (PException e) {
868-
e.expectStopIteration(stopProfile);
873+
e.expectStopIteration(inliningTarget, stopProfile);
869874
break;
870875
}
871876
xsave = x;
@@ -2313,26 +2318,22 @@ public abstract static class ProdNode extends PythonBuiltinNode {
23132318

23142319
@Child private LookupAndCallUnaryNode callNextNode = LookupAndCallUnaryNode.create(SpecialMethodSlot.Next);
23152320
@Child private BinaryOpNode mul = BinaryArithmetic.Mul.create();
2316-
@Child private IsBuiltinClassProfile errorProfile = IsBuiltinClassProfile.create();
23172321

23182322
@Specialization
2319-
@SuppressWarnings("unused")
2320-
public Object doGenericNoStart(VirtualFrame frame, Object iterable, PNone start,
2321-
@Shared("getIter") @Cached PyObjectGetIter getIter) {
2322-
return doGeneric(frame, iterable, 1, getIter);
2323-
}
2324-
2325-
@Specialization(guards = "!isNoValue(start)")
2326-
public Object doGeneric(VirtualFrame frame, Object iterable, Object start,
2327-
@Shared("getIter") @Cached PyObjectGetIter getIter) {
2323+
public Object doGeneric(VirtualFrame frame, Object iterable, Object startIn,
2324+
@Bind("this") Node inliningTarget,
2325+
@Cached IsBuiltinObjectProfile errorProfile,
2326+
@Cached InlinedConditionProfile startIsNoValueProfile,
2327+
@Cached PyObjectGetIter getIter) {
2328+
Object start = startIsNoValueProfile.profile(inliningTarget, PGuards.isNoValue(startIn)) ? 1 : startIn;
23282329
Object iterator = getIter.execute(frame, iterable);
23292330
Object value = start;
23302331
while (true) {
23312332
Object nextValue;
23322333
try {
23332334
nextValue = callNextNode.executeObject(frame, iterator);
23342335
} catch (PException e) {
2335-
e.expectStopIteration(errorProfile);
2336+
e.expectStopIteration(inliningTarget, errorProfile);
23362337
return value;
23372338
}
23382339
value = mul.executeObject(frame, value, nextValue);

0 commit comments

Comments
 (0)