Skip to content

Commit 5bf1b6d

Browse files
committed
materialize Pstring in StringBuiltins.AddNode
1 parent 591f7e1 commit 5bf1b6d

File tree

1 file changed

+39
-14
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str

1 file changed

+39
-14
lines changed

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@
9898
import com.oracle.graal.python.builtins.objects.str.StringNodes.JoinInternalNode;
9999
import com.oracle.graal.python.builtins.objects.str.StringNodes.SpliceNode;
100100
import com.oracle.graal.python.builtins.objects.str.StringNodes.StringLenNode;
101+
import com.oracle.graal.python.builtins.objects.str.StringNodes.StringMaterializeNode;
101102
import com.oracle.graal.python.builtins.objects.str.StringUtils.StripKind;
102103
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
103104
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
105+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsSameTypeNode;
104106
import com.oracle.graal.python.nodes.ErrorMessages;
105107
import com.oracle.graal.python.nodes.PGuards;
106108
import com.oracle.graal.python.nodes.SpecialMethodNames;
@@ -404,30 +406,58 @@ String doSSSimple(String self, String other) {
404406
return self;
405407
}
406408

407-
@Specialization(guards = "!concatGuard(self, other.getCharSequence())")
408-
String doSSSimple(String self, PString other) {
409+
@Specialization(guards = "!concatGuard(self, other.getCharSequence())", limit = "1")
410+
Object doSSSimple(String self, PString other,
411+
@Cached StringMaterializeNode materializeNode,
412+
@Cached IsSameTypeNode isSameType,
413+
@Cached BranchProfile isSameBranch,
414+
@CachedLibrary("other") PythonObjectLibrary lib) {
409415
if (LazyString.length(self, leftProfile1, leftProfile2) == 0) {
410416
// result type has to be str
411-
return toString(other.getCharSequence());
417+
if (isSameType.execute(lib.getLazyPythonClass(other), PythonBuiltinClassType.PString)) {
418+
isSameBranch.enter();
419+
return other;
420+
}
421+
return materializeNode.execute(other);
412422
}
413423
return self;
414424
}
415425

416-
@Specialization(guards = "!concatGuard(self.getCharSequence(), other)")
417-
String doSSSimple(PString self, String other) {
426+
@Specialization(guards = "!concatGuard(self.getCharSequence(), other)", limit = "1")
427+
Object doSSSimple(PString self, String other,
428+
@Cached StringMaterializeNode materializeNode,
429+
@Cached IsSameTypeNode isSameType,
430+
@Cached BranchProfile isSameBranch,
431+
@CachedLibrary("self") PythonObjectLibrary lib) {
418432
if (LazyString.length(self.getCharSequence(), leftProfile1, leftProfile2) == 0) {
419433
return other;
420434
}
421435
// result type has to be str
422-
return toString(self.getCharSequence());
436+
if (isSameType.execute(lib.getLazyPythonClass(self), PythonBuiltinClassType.PString)) {
437+
isSameBranch.enter();
438+
return self;
439+
}
440+
return materializeNode.execute(self);
423441
}
424442

425443
@Specialization(guards = "!concatGuard(self.getCharSequence(), other.getCharSequence())")
426-
String doSSSimple(PString self, PString other) {
444+
Object doSSSimple(PString self, PString other,
445+
@Cached StringMaterializeNode materializeNode,
446+
@Cached IsSameTypeNode isSameType,
447+
@Cached BranchProfile isSameBranch,
448+
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
427449
if (LazyString.length(self.getCharSequence(), leftProfile1, leftProfile2) == 0) {
428-
return toString(other.getCharSequence());
450+
if (isSameType.execute(lib.getLazyPythonClass(other), PythonBuiltinClassType.PString)) {
451+
isSameBranch.enter();
452+
return other;
453+
}
454+
return materializeNode.execute(other);
429455
}
430-
return toString(self.getCharSequence());
456+
if (isSameType.execute(lib.getLazyPythonClass(self), PythonBuiltinClassType.PString)) {
457+
isSameBranch.enter();
458+
return self;
459+
}
460+
return materializeNode.execute(self);
431461
}
432462

433463
@Specialization(guards = "concatGuard(self.getCharSequence(), other)")
@@ -476,11 +506,6 @@ private static String stringConcat(CharSequence left, CharSequence right) {
476506
return left.toString() + right.toString();
477507
}
478508

479-
@TruffleBoundary
480-
private static String toString(CharSequence cs) {
481-
return cs.toString();
482-
}
483-
484509
@Specialization(guards = "isString(self)")
485510
Object doSNative(VirtualFrame frame, Object self, PythonAbstractNativeObject other,
486511
@Cached CastToJavaStringNode cast,

0 commit comments

Comments
 (0)