Skip to content

Commit 79c2531

Browse files
committed
Avoid PString.getValue and Object.toString with CastToJavaStringNode
1 parent f9f4f80 commit 79c2531

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
import com.oracle.truffle.api.dsl.TypeSystemReference;
9191
import com.oracle.truffle.api.frame.VirtualFrame;
9292
import com.oracle.truffle.api.library.CachedLibrary;
93-
import com.oracle.truffle.api.profiles.ValueProfile;
9493

9594
@CoreFunctions(defineModule = "_codecs")
9695
public class CodecsModuleBuiltins extends PythonBuiltins {
@@ -322,41 +321,41 @@ public abstract static class CodecsEncodeNode extends EncodeBaseNode {
322321

323322
@Specialization(guards = "isString(str)")
324323
Object encode(Object str, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
325-
@Cached("createClassProfile()") ValueProfile strTypeProfile) {
326-
Object profiledStr = strTypeProfile.profile(str);
327-
PBytes bytes = encodeString(profiledStr.toString(), "utf-8", "strict");
324+
@Shared("castStr") @Cached CastToJavaStringNode castStr) {
325+
String profiledStr = castStr.execute(str);
326+
PBytes bytes = encodeString(profiledStr, "utf-8", "strict");
328327
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
329328
}
330329

331330
@Specialization(guards = {"isString(str)", "isString(encoding)"})
332331
Object encode(Object str, Object encoding, @SuppressWarnings("unused") PNone errors,
333-
@Cached("createClassProfile()") ValueProfile strTypeProfile,
334-
@Cached("createClassProfile()") ValueProfile encodingTypeProfile) {
335-
Object profiledStr = strTypeProfile.profile(str);
336-
Object profiledEncoding = encodingTypeProfile.profile(encoding);
337-
PBytes bytes = encodeString(profiledStr.toString(), profiledEncoding.toString(), "strict");
332+
@Shared("castStr") @Cached CastToJavaStringNode castStr,
333+
@Shared("castEncoding") @Cached CastToJavaStringNode castEncoding) {
334+
String profiledStr = castStr.execute(str);
335+
String profiledEncoding = castEncoding.execute(encoding);
336+
PBytes bytes = encodeString(profiledStr, profiledEncoding, "strict");
338337
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
339338
}
340339

341340
@Specialization(guards = {"isString(str)", "isString(errors)"})
342341
Object encode(Object str, @SuppressWarnings("unused") PNone encoding, Object errors,
343-
@Cached("createClassProfile()") ValueProfile strTypeProfile,
344-
@Cached("createClassProfile()") ValueProfile errorsTypeProfile) {
345-
Object profiledStr = strTypeProfile.profile(str);
346-
Object profiledErrors = errorsTypeProfile.profile(errors);
347-
PBytes bytes = encodeString(profiledStr.toString(), "utf-8", profiledErrors.toString());
342+
@Shared("castStr") @Cached CastToJavaStringNode castStr,
343+
@Shared("castErrors") @Cached CastToJavaStringNode castErrors) {
344+
String profiledStr = castStr.execute(str);
345+
String profiledErrors = castErrors.execute(errors);
346+
PBytes bytes = encodeString(profiledStr, "utf-8", profiledErrors);
348347
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
349348
}
350349

351350
@Specialization(guards = {"isString(str)", "isString(encoding)", "isString(errors)"})
352351
Object encode(Object str, Object encoding, Object errors,
353-
@Cached("createClassProfile()") ValueProfile strTypeProfile,
354-
@Cached("createClassProfile()") ValueProfile encodingTypeProfile,
355-
@Cached("createClassProfile()") ValueProfile errorsTypeProfile) {
356-
Object profiledStr = strTypeProfile.profile(str);
357-
Object profiledEncoding = encodingTypeProfile.profile(encoding);
358-
Object profiledErrors = errorsTypeProfile.profile(errors);
359-
PBytes bytes = encodeString(profiledStr.toString(), profiledEncoding.toString(), profiledErrors.toString());
352+
@Shared("castStr") @Cached CastToJavaStringNode castStr,
353+
@Shared("castEncoding") @Cached CastToJavaStringNode castEncoding,
354+
@Shared("castErrors") @Cached CastToJavaStringNode castErrors) {
355+
String profiledStr = castStr.execute(str);
356+
String profiledEncoding = castEncoding.execute(encoding);
357+
String profiledErrors = castErrors.execute(errors);
358+
PBytes bytes = encodeString(profiledStr, profiledEncoding, profiledErrors);
360359
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
361360
}
362361

@@ -546,9 +545,9 @@ Object decode(PIBytesLike bytes, @SuppressWarnings("unused") PNone errors) {
546545

547546
@Specialization(guards = {"isString(errors)"})
548547
Object decode(PIBytesLike bytes, Object errors,
549-
@Cached("createClassProfile()") ValueProfile errorsTypeProfile) {
550-
Object profiledErrors = errorsTypeProfile.profile(errors);
551-
String string = decodeBytes(getBytesBuffer(bytes), profiledErrors.toString());
548+
@Cached CastToJavaStringNode castStr) {
549+
String profiledErrors = castStr.execute(errors);
550+
String string = decodeBytes(getBytesBuffer(bytes), profiledErrors);
552551
return factory().createTuple(new Object[]{string, string.length()});
553552
}
554553

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/DynamicObjectStorage.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromDynamicObjectNode;
5454
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
5555
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
56+
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
5657
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
5758
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5859
import com.oracle.truffle.api.dsl.Cached;
@@ -155,10 +156,11 @@ static Object string(DynamicObjectStorage self, String key, ThreadState state,
155156

156157
@Specialization(guards = "isBuiltinString(key, profile)", limit = "1")
157158
static Object pstring(DynamicObjectStorage self, PString key, ThreadState state,
159+
@Shared("castStr") @Cached CastToJavaStringNode castStr,
158160
@Shared("readKey") @Cached ReadAttributeFromDynamicObjectNode readKey,
159161
@Shared("builtinStringProfile") @Cached IsBuiltinClassProfile profile,
160162
@Exclusive @Cached("createBinaryProfile()") ConditionProfile noValueProfile) {
161-
return string(self, key.getValue(), state, readKey, noValueProfile);
163+
return string(self, castStr.execute(key), state, readKey, noValueProfile);
162164
}
163165

164166
@Specialization(guards = {"cachedShape == self.store.getShape()", "!isBuiltinString(key, profile)"}, limit = "1")
@@ -254,10 +256,11 @@ static HashingStorage string(DynamicObjectStorage self, String key, Object value
254256

255257
@Specialization(guards = "isBuiltinString(key, profile)", limit = "1")
256258
static HashingStorage pstring(DynamicObjectStorage self, PString key, Object value, ThreadState state,
259+
@Shared("castStr") @Cached CastToJavaStringNode castStr,
257260
@Shared("hasMroprofile") @Cached BranchProfile hasMro,
258261
@Shared("setitemWrite") @Cached WriteAttributeToDynamicObjectNode writeNode,
259262
@Shared("builtinStringProfile") @Cached IsBuiltinClassProfile profile) {
260-
return string(self, key.getValue(), value, state, hasMro, writeNode);
263+
return string(self, castStr.execute(key), value, state, hasMro, writeNode);
261264
}
262265

263266
// n.b: do not replace the other two specializations here, because that would make the

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictBuiltins.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,9 @@ Object getItem(VirtualFrame frame, PDict self, Object key,
317317
public abstract static class MissingNode extends PythonBinaryBuiltinNode {
318318
@SuppressWarnings("unused")
319319
@Specialization
320-
Object run(Object self, PString key) {
321-
throw raise(KeyError, "%s", key.getValue());
320+
Object run(Object self, PString key,
321+
@Cached CastToJavaStringNode castStr) {
322+
throw raise(KeyError, "%s", castStr.execute(key));
322323
}
323324

324325
@SuppressWarnings("unused")

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@ static String doGeneric(VirtualFrame frame, String string, Object iterable,
287287
@Cached IsBuiltinClassProfile errorProfile0,
288288
@Cached IsBuiltinClassProfile errorProfile1,
289289
@Cached IsBuiltinClassProfile errorProfile2,
290-
@Cached("createBinaryProfile()") ConditionProfile errorProfile3) {
290+
@Cached CastToJavaStringNode castStrNode) {
291291

292292
try {
293293
Object iterator = getIterator.executeWith(frame, iterable);
294294
StringBuilder str = new StringBuilder();
295295
try {
296-
append(str, checkItem(next.execute(frame, iterator), 0, errorProfile3, raise));
296+
append(str, checkItem(next.execute(frame, iterator), 0, castStrNode, raise));
297297
} catch (PException e) {
298298
e.expectStopIteration(errorProfile1);
299299
return "";
@@ -309,19 +309,21 @@ static String doGeneric(VirtualFrame frame, String string, Object iterable,
309309
return toString(str);
310310
}
311311
append(str, string);
312-
append(str, checkItem(value, i++, errorProfile3, raise));
312+
append(str, checkItem(value, i++, castStrNode, raise));
313313
}
314314
} catch (PException e) {
315315
e.expect(PythonBuiltinClassType.TypeError, errorProfile0);
316316
throw raise.raise(PythonBuiltinClassType.TypeError, "can only join an iterable");
317317
}
318318
}
319319

320-
private static String checkItem(Object item, int pos, ConditionProfile profile, PRaiseNode raise) {
321-
if (profile.profile(PGuards.isString(item))) {
322-
return item.toString();
320+
private static String checkItem(Object item, int pos, CastToJavaStringNode castNode, PRaiseNode raise) {
321+
String result = castNode.execute(item);
322+
if (result != null) {
323+
return result;
324+
} else {
325+
throw raise.raise(TypeError, INVALID_SEQ_ITEM, pos, item);
323326
}
324-
throw raise.raise(TypeError, INVALID_SEQ_ITEM, pos, item);
325327
}
326328

327329
@TruffleBoundary(allowInlining = true)

0 commit comments

Comments
 (0)