|
92 | 92 | import com.oracle.graal.python.runtime.PythonOptions;
|
93 | 93 | import com.oracle.graal.python.runtime.exception.PException;
|
94 | 94 | import com.oracle.graal.python.util.PythonUtils;
|
| 95 | +import com.oracle.truffle.api.Assumption; |
95 | 96 | import com.oracle.truffle.api.CallTarget;
|
96 | 97 | import com.oracle.truffle.api.CompilerDirectives;
|
97 | 98 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
@@ -174,19 +175,36 @@ public boolean run() {
|
174 | 175 | @Builtin(name = "get_magic")
|
175 | 176 | @GenerateNodeFactory
|
176 | 177 | 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) { |
182 | 190 | return magicBytes;
|
183 | 191 | }
|
184 | 192 |
|
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) { |
186 | 204 | try {
|
187 |
| - PBytes magic = toBytesNode.execute(frame, 3413, 2, "little", false); |
| 205 | + PBytes magic = toBytesNode.execute(frame, MAGIC_NUMBER, 2, "little", false); |
188 | 206 | 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'}; |
190 | 208 | } catch (UnsupportedMessageException e) {
|
191 | 209 | CompilerDirectives.transferToInterpreterAndInvalidate();
|
192 | 210 | throw new IllegalStateException("magicBytes does not support getBufferBytes()");
|
|
0 commit comments