Skip to content

Commit b2a2350

Browse files
committed
ImpModuleBuiltins.GetMagic create cached specialization for single and multi context execution modes
1 parent c60b3b7 commit b2a2350

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import com.oracle.graal.python.runtime.PythonOptions;
9393
import com.oracle.graal.python.runtime.exception.PException;
9494
import com.oracle.graal.python.util.PythonUtils;
95+
import com.oracle.truffle.api.Assumption;
9596
import com.oracle.truffle.api.CallTarget;
9697
import com.oracle.truffle.api.CompilerDirectives;
9798
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -174,19 +175,36 @@ public boolean run() {
174175
@Builtin(name = "get_magic")
175176
@GenerateNodeFactory
176177
public abstract static class GetMagic extends PythonBuiltinNode {
177-
@Specialization
178-
public PBytes run(@SuppressWarnings("unused") VirtualFrame frame,
179-
@SuppressWarnings("unused") @Cached IntBuiltins.ToBytesNode toBytesNode,
180-
@SuppressWarnings("unused") @CachedLibrary(limit = "1") PythonObjectLibrary pol,
181-
@Cached("getMagicNumberBytes(frame, toBytesNode, pol)") PBytes magicBytes) {
178+
static final int MAGIC_NUMBER = 3413;
179+
180+
@Child IntBuiltins.ToBytesNode toBytesNode = IntBuiltins.ToBytesNode.create();
181+
@Child PythonObjectLibrary pol = PythonObjectLibrary.getFactory().createDispatched(1);
182+
183+
public static final Assumption singleContextAssumption() {
184+
return PythonLanguage.getCurrent().singleContextAssumption;
185+
}
186+
187+
@Specialization(assumptions = "singleContextAssumption()")
188+
public PBytes runCachedSingleContext(@SuppressWarnings("unused") VirtualFrame frame,
189+
@Cached(value = "getMagicNumberPBytes(frame, toBytesNode, pol)", weak = true) PBytes magicBytes) {
182190
return magicBytes;
183191
}
184192

185-
protected PBytes getMagicNumberBytes(VirtualFrame frame, IntBuiltins.ToBytesNode toBytesNode, PythonObjectLibrary pol) {
193+
@Specialization(replaces = "runCachedSingleContext")
194+
public PBytes run(@SuppressWarnings("unused") VirtualFrame frame,
195+
@Cached(value = "getMagicNumberBytes(frame, toBytesNode, pol)", dimensions = 1) byte[] magicBytes) {
196+
return factory().createBytes(magicBytes);
197+
}
198+
199+
protected PBytes getMagicNumberPBytes(VirtualFrame frame, IntBuiltins.ToBytesNode toBytesNode, PythonObjectLibrary pol) {
200+
return factory().createBytes(getMagicNumberBytes(frame, toBytesNode, pol));
201+
}
202+
203+
protected byte[] getMagicNumberBytes(VirtualFrame frame, IntBuiltins.ToBytesNode toBytesNode, PythonObjectLibrary pol) {
186204
try {
187-
PBytes magic = toBytesNode.execute(frame, 3413, 2, "little", false);
205+
PBytes magic = toBytesNode.execute(frame, MAGIC_NUMBER, 2, "little", false);
188206
byte[] magicBytes = pol.getBufferBytes(magic);
189-
return factory().createBytes(new byte[]{magicBytes[0], magicBytes[1], '\r', '\n'});
207+
return new byte[]{magicBytes[0], magicBytes[1], '\r', '\n'};
190208
} catch (UnsupportedMessageException e) {
191209
CompilerDirectives.transferToInterpreterAndInvalidate();
192210
throw new IllegalStateException("magicBytes does not support getBufferBytes()");

0 commit comments

Comments
 (0)