Skip to content

Commit 360ea0d

Browse files
committed
Extract side-effecting case into extra node to reduce overhead of non-side-effecting toDisplayString.
1 parent 136213f commit 360ea0d

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,36 +1742,60 @@ public Class<? extends TruffleLanguage<?>> getLanguage() {
17421742
return PythonLanguage.class;
17431743
}
17441744

1745-
@ExportMessage
1746-
public String toDisplayString(boolean allowSideEffects,
1747-
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context,
1748-
@Exclusive @Cached ReadAttributeFromObjectNode readStr,
1749-
@Exclusive @Cached CallNode callNode,
1750-
@Cached CastToJavaStringNode castStr,
1751-
@Cached ConditionProfile conditionProfile) {
1752-
PythonModule builtins = context.getBuiltins();
1753-
String result = null;
1754-
if (conditionProfile.profile(builtins != null && allowSideEffects)) {
1745+
@GenerateUncached
1746+
@SuppressWarnings("unused")
1747+
public abstract static class ToDisplaySideEffectingNode extends Node {
1748+
1749+
public abstract String execute(PythonAbstractObject receiver);
1750+
1751+
@Specialization
1752+
public String doDefault(PythonAbstractObject receiver,
1753+
@CachedContext(PythonLanguage.class) PythonContext context,
1754+
@Cached ReadAttributeFromObjectNode readStr,
1755+
@Cached CallNode callNode,
1756+
@Cached CastToJavaStringNode castStr,
1757+
@Cached ConditionProfile toStringUsed) {
17551758
Object toStrAttr;
17561759
String names;
17571760
if (context.getOption(PythonOptions.UseReprForPrintString)) {
17581761
names = BuiltinNames.REPR;
17591762
} else {
17601763
names = BuiltinNames.STR;
17611764
}
1762-
toStrAttr = readStr.execute(builtins, names);
1763-
result = castStr.execute(callNode.execute(toStrAttr, this));
1764-
// reusing the same condition profile here as it effectively
1765-
// has the same effect.
1766-
if (conditionProfile.profile(result != null)) {
1765+
String result = null;
1766+
PythonModule builtins = context.getBuiltins();
1767+
if (toStringUsed.profile(builtins != null)) {
1768+
toStrAttr = readStr.execute(builtins, names);
1769+
result = castStr.execute(callNode.execute(toStrAttr, receiver));
1770+
}
1771+
if (toStringUsed.profile(result != null)) {
17671772
return result;
1773+
} else {
1774+
return receiver.toStringBoundary();
17681775
}
17691776
}
1770-
return toStringImpl();
1777+
1778+
}
1779+
1780+
@ExportMessage
1781+
@SuppressWarnings("unused")
1782+
public static class ToDisplayString {
1783+
1784+
@Specialization(guards = "allowSideEffects")
1785+
public static String doSideEffecting(PythonAbstractObject receiver, boolean allowSideEffects,
1786+
@Cached ToDisplaySideEffectingNode toDisplayCallnode) {
1787+
return toDisplayCallnode.execute(receiver);
1788+
}
1789+
1790+
@Specialization(guards = "!allowSideEffects")
1791+
public static String doNonSideEffecting(PythonAbstractObject receiver, boolean allowSideEffects) {
1792+
return receiver.toStringBoundary();
1793+
}
1794+
17711795
}
17721796

17731797
@TruffleBoundary
1774-
private String toStringImpl() {
1798+
final String toStringBoundary() {
17751799
return toString();
17761800
}
17771801

0 commit comments

Comments
 (0)