Skip to content

Commit 1d6e0f2

Browse files
committed
DRY up the string slicing code
1 parent 5004257 commit 1d6e0f2

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@
9999
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
100100
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
101101
import com.oracle.graal.python.nodes.subscript.GetItemNode;
102+
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode.CastToSliceComponentNode;
102103
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
103-
import com.oracle.graal.python.nodes.util.CastToIndexNode;
104104
import com.oracle.graal.python.nodes.util.CastToJavaIntNode;
105105
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
106106
import com.oracle.graal.python.nodes.util.CastToJavaStringNodeGen;
@@ -444,8 +444,7 @@ static Object doAll(VirtualFrame frame, Object left, Object right,
444444

445445
abstract static class PrefixSuffixBaseNode extends PythonQuaternaryBuiltinNode {
446446

447-
@Child private CastToIndexNode startNode;
448-
@Child private CastToIndexNode endNode;
447+
@Child private CastToSliceComponentNode castSliceComponentNode;
449448
@Child private GetObjectArrayNode getObjectArrayNode;
450449
@Child private CastToJavaStringNode castToJavaStringNode;
451450

@@ -493,8 +492,8 @@ boolean doObjectPrefixGeneric(VirtualFrame frame, Object self, Object substr, Ob
493492
@Cached CastToJavaStringCheckedNode castPrefixNode) {
494493
String selfStr = castSelfNode.cast(self, INVALID_RECEIVER, "startswith", self);
495494
int len = selfStr.length();
496-
int istart = PGuards.isPNone(start) ? 0 : adjustStart(castStart(frame, start), len);
497-
int iend = PGuards.isPNone(end) ? len : adjustEnd(castEnd(frame, end), len);
495+
int istart = adjustStart(castSlicePart(frame, start), len);
496+
int iend = PGuards.isPNone(end) ? len : adjustEnd(castSlicePart(frame, end), len);
498497
String prefixStr = castPrefixNode.cast(substr, INVALID_FIRST_ARG, "startswith", substr);
499498
return doIt(selfStr, prefixStr, istart, iend);
500499
}
@@ -504,8 +503,8 @@ boolean doTuplePrefixGeneric(VirtualFrame frame, Object self, PTuple substrs, Ob
504503
@Cached CastToJavaStringCheckedNode castSelfNode) {
505504
String selfStr = castSelfNode.cast(self, INVALID_RECEIVER, "startswith", self);
506505
int len = selfStr.length();
507-
int istart = PGuards.isPNone(start) ? 0 : adjustStart(castStart(frame, start), len);
508-
int iend = PGuards.isPNone(end) ? len : adjustEnd(castEnd(frame, end), len);
506+
int istart = adjustStart(castSlicePart(frame, start), len);
507+
int iend = PGuards.isPNone(end) ? len : adjustEnd(castSlicePart(frame, end), len);
509508
return doIt(selfStr, substrs, istart, iend);
510509
}
511510

@@ -553,24 +552,13 @@ static int adjustEnd(int end, int length) {
553552
return adjustStart(end, length);
554553
}
555554

556-
private int castStart(VirtualFrame frame, Object start) {
557-
if (startNode == null) {
555+
private int castSlicePart(VirtualFrame frame, Object idx) {
556+
if (castSliceComponentNode == null) {
558557
CompilerDirectives.transferToInterpreterAndInvalidate();
559-
startNode = insert(CastToIndexNode.create(TypeError, val -> {
560-
throw raise(PythonBuiltinClassType.TypeError, "slice indices must be integers or None or have an __index__ method");
561-
}));
558+
// None should map to 0, overflow to the maximum integer
559+
castSliceComponentNode = insert(CastToSliceComponentNode.create(0, Integer.MAX_VALUE));
562560
}
563-
return startNode.execute(frame, start);
564-
}
565-
566-
private int castEnd(VirtualFrame frame, Object end) {
567-
if (endNode == null) {
568-
CompilerDirectives.transferToInterpreterAndInvalidate();
569-
endNode = insert(CastToIndexNode.create(TypeError, val -> {
570-
throw raise(PythonBuiltinClassType.TypeError, "slice indices must be integers or None or have an __index__ method");
571-
}));
572-
}
573-
return endNode.execute(frame, end);
561+
return castSliceComponentNode.execute(frame, idx);
574562
}
575563

576564
private GetObjectArrayNode ensureGetObjectArrayNode() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/subscript/SliceLiteralNode.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.oracle.graal.python.builtins.objects.ints.PInt;
3434
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3535
import com.oracle.graal.python.builtins.objects.slice.PSlice;
36+
import com.oracle.graal.python.nodes.PGuards;
3637
import com.oracle.graal.python.nodes.PNode;
3738
import com.oracle.graal.python.nodes.PNodeWithContext;
3839
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -45,6 +46,7 @@
4546
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4647
import com.oracle.truffle.api.CompilerDirectives;
4748
import com.oracle.truffle.api.dsl.Cached;
49+
import com.oracle.truffle.api.dsl.Cached.Shared;
4850
import com.oracle.truffle.api.dsl.Fallback;
4951
import com.oracle.truffle.api.dsl.ImportStatic;
5052
import com.oracle.truffle.api.dsl.NodeChild;
@@ -119,14 +121,10 @@ public static SliceLiteralNode create() {
119121
return SliceLiteralNodeGen.create(null, null, null);
120122
}
121123

122-
@ImportStatic(PythonOptions.class)
123-
abstract static class CastToSliceComponentNode extends PNodeWithContext {
124-
125-
@Child private PRaiseNode raiseNode;
126-
124+
@ImportStatic({PythonOptions.class, PGuards.class})
125+
public abstract static class CastToSliceComponentNode extends PNodeWithContext {
127126
private final int defaultValue;
128127
private final int overflowValue;
129-
private final BranchProfile indexErrorProfile = BranchProfile.create();
130128

131129
public CastToSliceComponentNode(int defaultValue, int overflowValue) {
132130
this.defaultValue = defaultValue;
@@ -155,7 +153,7 @@ int doInt(int i) {
155153
}
156154

157155
@Specialization
158-
int doLong(long i) {
156+
int doLong(long i, @Shared("indexErrorProfile") @Cached BranchProfile indexErrorProfile) {
159157
try {
160158
return PInt.intValueExact(i);
161159
} catch (ArithmeticException e) {
@@ -165,7 +163,7 @@ int doLong(long i) {
165163
}
166164

167165
@Specialization
168-
int doPInt(PInt i) {
166+
int doPInt(PInt i, @Shared("indexErrorProfile") @Cached BranchProfile indexErrorProfile) {
169167
try {
170168
return i.intValueExact();
171169
} catch (ArithmeticException e) {
@@ -174,7 +172,7 @@ int doPInt(PInt i) {
174172
}
175173
}
176174

177-
@Specialization(replaces = {"doBoolean", "doInt", "doLong", "doPInt"}, limit = "getCallSiteInlineCacheMaxDepth()")
175+
@Specialization(guards = "!isPNone(i)", replaces = {"doBoolean", "doInt", "doLong", "doPInt"}, limit = "getCallSiteInlineCacheMaxDepth()")
178176
int doGeneric(VirtualFrame frame, Object i,
179177
@Cached PRaiseNode raise,
180178
@CachedLibrary("i") PythonObjectLibrary lib,

0 commit comments

Comments
 (0)