Skip to content

Commit c737c1a

Browse files
committed
Refactor min()/max()
1 parent 97e8ae9 commit c737c1a

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,23 +1633,17 @@ public int len(VirtualFrame frame, Object obj,
16331633
}
16341634
}
16351635

1636-
public abstract static class MinMaxNode extends PythonBuiltinNode {
1637-
@NeverDefault
1638-
protected final BinaryComparisonNode createComparison() {
1639-
if (this instanceof MaxNode) {
1640-
return BinaryComparisonNode.GtNode.create();
1641-
} else {
1642-
return BinaryComparisonNode.LtNode.create();
1643-
}
1644-
}
1636+
@GenerateInline
1637+
@GenerateCached(false)
1638+
public abstract static class MinMaxNode extends Node {
1639+
1640+
abstract Object execute(VirtualFrame frame, Node inliningTarget, Object arg1, Object[] args, Object keywordArgIn, Object defaultVal, String name, BinaryComparisonNode comparisonNode);
16451641

16461642
@Specialization(guards = "args.length == 0")
1647-
@SuppressWarnings("truffle-static-method") // TODO: inh
1648-
Object minmaxSequenceWithKey(VirtualFrame frame, Object arg1, @SuppressWarnings("unused") Object[] args, Object keywordArgIn, Object defaultVal,
1649-
@Bind("this") Node inliningTarget,
1643+
static Object minmaxSequenceWithKey(VirtualFrame frame, Node inliningTarget, Object arg1, @SuppressWarnings("unused") Object[] args, Object keywordArgIn, Object defaultVal, String name,
1644+
BinaryComparisonNode compare,
16501645
@Exclusive @Cached PyObjectGetIter getIter,
1651-
@Cached GetNextNode nextNode,
1652-
@Shared @Cached("createComparison()") BinaryComparisonNode compare,
1646+
@Cached(inline = false) GetNextNode nextNode,
16531647
@Exclusive @Cached CoerceToBooleanNode.YesNode castToBooleanNode,
16541648
@Exclusive @Cached CallNode.Lazy keyCall,
16551649
@Exclusive @Cached InlinedBranchProfile seenNonBoolean,
@@ -1668,7 +1662,7 @@ Object minmaxSequenceWithKey(VirtualFrame frame, Object arg1, @SuppressWarnings(
16681662
} catch (PException e) {
16691663
e.expectStopIteration(inliningTarget, errorProfile1);
16701664
if (hasDefaultProfile.profile(inliningTarget, isNoValue(defaultVal))) {
1671-
throw raiseNode.get(inliningTarget).raise(PythonErrorType.ValueError, ErrorMessages.ARG_IS_EMPTY_SEQ, getName());
1665+
throw raiseNode.get(inliningTarget).raise(PythonErrorType.ValueError, ErrorMessages.ARG_IS_EMPTY_SEQ, name);
16721666
} else {
16731667
return defaultVal;
16741668
}
@@ -1708,15 +1702,8 @@ Object minmaxSequenceWithKey(VirtualFrame frame, Object arg1, @SuppressWarnings(
17081702
return currentValue;
17091703
}
17101704

1711-
private String getName() {
1712-
return this instanceof MaxNode ? "max" : "min";
1713-
}
1714-
17151705
@Specialization(guards = {"args.length != 0"})
1716-
@SuppressWarnings("truffle-static-method") // TODO: inh
1717-
Object minmaxBinaryWithKey(VirtualFrame frame, Object arg1, Object[] args, Object keywordArgIn, Object defaultVal,
1718-
@Bind("this") Node inliningTarget,
1719-
@Shared @Cached("createComparison()") BinaryComparisonNode compare,
1706+
static Object minmaxBinaryWithKey(VirtualFrame frame, Node inliningTarget, Object arg1, Object[] args, Object keywordArgIn, Object defaultVal, String name, BinaryComparisonNode compare,
17201707
@Exclusive @Cached CallNode.Lazy keyCall,
17211708
@Exclusive @Cached CoerceToBooleanNode.YesNode castToBooleanNode,
17221709
@Exclusive @Cached InlinedBranchProfile seenNonBoolean,
@@ -1730,7 +1717,7 @@ Object minmaxBinaryWithKey(VirtualFrame frame, Object arg1, Object[] args, Objec
17301717
Object keywordArg = kwArgsAreNone ? null : keywordArgIn;
17311718

17321719
if (!hasDefaultProfile.profile(inliningTarget, isNoValue(defaultVal))) {
1733-
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.CANNOT_SPECIFY_DEFAULT_FOR_S, getName());
1720+
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.CANNOT_SPECIFY_DEFAULT_FOR_S, name);
17341721
}
17351722
Object currentValue = arg1;
17361723
Object currentKey = applyKeyFunction(frame, inliningTarget, keywordArg, keyCall, currentValue);
@@ -1787,16 +1774,29 @@ private static Object applyKeyFunction(VirtualFrame frame, Node inliningTarget,
17871774
"max(arg1, arg2, *args, *[, key=func]) -> value\n\n" + "With a single iterable argument, return its biggest item. The\n" +
17881775
"default keyword-only argument specifies an object to return if\n" + "the provided iterable is empty.\n" + "With two or more arguments, return the largest argument.")
17891776
@GenerateNodeFactory
1790-
public abstract static class MaxNode extends MinMaxNode {
1777+
public abstract static class MaxNode extends PythonBuiltinNode {
17911778

1779+
@Specialization
1780+
static Object max(VirtualFrame frame, Object arg1, Object[] args, Object keywordArgIn, Object defaultVal,
1781+
@Bind("this") Node inliningTarget,
1782+
@Cached MinMaxNode minMaxNode,
1783+
@Cached BinaryComparisonNode.GtNode gtNode) {
1784+
return minMaxNode.execute(frame, inliningTarget, arg1, args, keywordArgIn, defaultVal, "max", gtNode);
1785+
}
17921786
}
17931787

17941788
// min(iterable, *[, key])
17951789
// min(arg1, arg2, *args[, key])
17961790
@Builtin(name = J_MIN, minNumOfPositionalArgs = 1, takesVarArgs = true, keywordOnlyNames = {"key", "default"})
17971791
@GenerateNodeFactory
1798-
public abstract static class MinNode extends MinMaxNode {
1799-
1792+
public abstract static class MinNode extends PythonBuiltinNode {
1793+
@Specialization
1794+
static Object min(VirtualFrame frame, Object arg1, Object[] args, Object keywordArgIn, Object defaultVal,
1795+
@Bind("this") Node inliningTarget,
1796+
@Cached MinMaxNode minMaxNode,
1797+
@Cached BinaryComparisonNode.LtNode ltNode) {
1798+
return minMaxNode.execute(frame, inliningTarget, arg1, args, keywordArgIn, defaultVal, "min", ltNode);
1799+
}
18001800
}
18011801

18021802
// next(iterator[, default])

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ private static double compareUsingBigDecimal(double v, BigInteger w) {
835835
@GenerateNodeFactory
836836
public abstract static class EqNode extends PythonBinaryBuiltinNode {
837837
@Specialization
838-
static Object doIt(double left, double right,
838+
static Object doIt(Object left, Object right,
839839
@Bind("this") Node inliningTarget,
840840
@Cached ComparisonHelperNode comparisonHelperNode) {
841841
return comparisonHelperNode.execute(inliningTarget, left, right, (a, b) -> a == b);
@@ -846,7 +846,7 @@ static Object doIt(double left, double right,
846846
@GenerateNodeFactory
847847
abstract static class NeNode extends PythonBinaryBuiltinNode {
848848
@Specialization
849-
static Object doIt(double left, double right,
849+
static Object doIt(Object left, Object right,
850850
@Bind("this") Node inliningTarget,
851851
@Cached ComparisonHelperNode comparisonHelperNode) {
852852
return comparisonHelperNode.execute(inliningTarget, left, right, (a, b) -> a != b);
@@ -857,7 +857,7 @@ static Object doIt(double left, double right,
857857
@GenerateNodeFactory
858858
public abstract static class LtNode extends PythonBinaryBuiltinNode {
859859
@Specialization
860-
static Object doIt(double left, double right,
860+
static Object doIt(Object left, Object right,
861861
@Bind("this") Node inliningTarget,
862862
@Cached ComparisonHelperNode comparisonHelperNode) {
863863
return comparisonHelperNode.execute(inliningTarget, left, right, (a, b) -> a < b);
@@ -868,7 +868,7 @@ static Object doIt(double left, double right,
868868
@GenerateNodeFactory
869869
public abstract static class LeNode extends PythonBinaryBuiltinNode {
870870
@Specialization
871-
static Object doIt(double left, double right,
871+
static Object doIt(Object left, Object right,
872872
@Bind("this") Node inliningTarget,
873873
@Cached ComparisonHelperNode comparisonHelperNode) {
874874
return comparisonHelperNode.execute(inliningTarget, left, right, (a, b) -> a <= b);
@@ -879,7 +879,7 @@ static Object doIt(double left, double right,
879879
@GenerateNodeFactory
880880
public abstract static class GtNode extends PythonBinaryBuiltinNode {
881881
@Specialization
882-
static Object doIt(double left, double right,
882+
static Object doIt(Object left, Object right,
883883
@Bind("this") Node inliningTarget,
884884
@Cached ComparisonHelperNode comparisonHelperNode) {
885885
return comparisonHelperNode.execute(inliningTarget, left, right, (a, b) -> a > b);
@@ -890,7 +890,7 @@ static Object doIt(double left, double right,
890890
@GenerateNodeFactory
891891
public abstract static class GeNode extends PythonBinaryBuiltinNode {
892892
@Specialization
893-
static Object doIt(double left, double right,
893+
static Object doIt(Object left, Object right,
894894
@Bind("this") Node inliningTarget,
895895
@Cached ComparisonHelperNode comparisonHelperNode) {
896896
return comparisonHelperNode.execute(inliningTarget, left, right, (a, b) -> a >= b);

0 commit comments

Comments
 (0)