Skip to content

Commit 75514ca

Browse files
timfelppisl
authored andcommitted
clean up context retrieval a bit more
1 parent 9aa1ebc commit 75514ca

File tree

8 files changed

+40
-33
lines changed

8 files changed

+40
-33
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,8 @@ public abstract static class BuildClassNode extends PythonVarargsBuiltinNode {
21192119
@TruffleBoundary
21202120
private static Object buildJavaClass(Object func, String name, Object base) {
21212121
PythonObjectLibrary factory = PythonObjectLibrary.getUncached();
2122-
Object module = PythonContext.get(factory).getCore().lookupBuiltinModule(BuiltinNames.__GRAALPYTHON__);
2122+
// uncached PythonContext get, since this code path is slow in any case
2123+
Object module = PythonContext.get(null).getCore().lookupBuiltinModule(BuiltinNames.__GRAALPYTHON__);
21232124
Object buildFunction = factory.lookupAttribute(module, null, "build_java_class");
21242125
return CallNode.getUncached().execute(buildFunction, func, name, base);
21252126
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,10 @@ private PCode readCode() {
950950
} catch (IOException e) {
951951
throw CompilerDirectives.shouldNotReachHere();
952952
}
953-
// get a new ID every time we deserialize the same filename in the same context
954-
PythonContext context = PythonContext.get(factory);
953+
// get a new ID every time we deserialize the same filename in the same context. We use
954+
// slow-path context lookup, since this code is likely dominated by the deserialization
955+
// time
956+
PythonContext context = PythonContext.get(null);
955957
ByteBuffer.wrap(codeString).putLong(codeLen, context.getDeserializationId(fileName));
956958
int firstLineNo = readInt();
957959
byte[] lnoTab = readBytes();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class PwdModuleBuiltins extends PythonBuiltins {
100100
@Override
101101
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
102102
PosixSupportLibrary posixLib = PosixSupportLibrary.getUncached();
103-
boolean hasGetpwentries = posixLib.hasGetpwentries(PythonContext.get(posixLib).getPosixSupport());
103+
boolean hasGetpwentries = posixLib.hasGetpwentries(PythonContext.get(null).getPosixSupport());
104104
if (hasGetpwentries) {
105105
return PwdModuleBuiltinsFactory.getFactories();
106106
} else {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ public static double timeSeconds() {
175175
private static final int TM_ISDST = 8; /* daylight saving time */
176176

177177
@TruffleBoundary
178-
private static Object[] getTimeStruct(long seconds, boolean local) {
178+
private static Object[] getTimeStruct(PythonContext context, long seconds, boolean local) {
179179
Object[] timeStruct = new Object[11];
180180
Instant instant = Instant.ofEpochSecond(seconds);
181-
ZoneId zone = (local) ? PythonContext.get(null).getEnv().getTimeZone() : ZoneId.of("GMT");
181+
ZoneId zone = (local) ? context.getEnv().getTimeZone() : ZoneId.of("GMT");
182182
ZonedDateTime zonedDateTime = LocalDateTime.ofInstant(instant, zone).atZone(zone);
183183
timeStruct[TM_YEAR] = zonedDateTime.getYear();
184184
timeStruct[TM_MON] = zonedDateTime.getMonth().ordinal() + 1; /* Want January == 1 */
@@ -278,7 +278,7 @@ public abstract static class PythonGMTimeNode extends PythonBuiltinNode {
278278
@Specialization
279279
public PTuple gmtime(VirtualFrame frame, Object seconds,
280280
@Cached ToLongTime toLongTime) {
281-
return factory().createStructSeq(STRUCT_TIME_DESC, getTimeStruct(toLongTime.execute(frame, seconds), false));
281+
return factory().createStructSeq(STRUCT_TIME_DESC, getTimeStruct(PythonContext.get(this), toLongTime.execute(frame, seconds), false));
282282
}
283283
}
284284

@@ -291,7 +291,7 @@ public abstract static class PythonLocalTimeNode extends PythonBuiltinNode {
291291
@Specialization
292292
public PTuple localtime(VirtualFrame frame, Object seconds,
293293
@Cached ToLongTime toLongTime) {
294-
return factory().createStructSeq(STRUCT_TIME_DESC, getTimeStruct(toLongTime.execute(frame, seconds), true));
294+
return factory().createStructSeq(STRUCT_TIME_DESC, getTimeStruct(PythonContext.get(this), toLongTime.execute(frame, seconds), true));
295295
}
296296
}
297297

@@ -913,13 +913,13 @@ abstract static class MkTimeNode extends PythonUnaryBuiltinNode {
913913
for (int i = 0; i < ELEMENT_COUNT; i++) {
914914
integers[i] = asSizeNode.executeExact(frame, items[i]);
915915
}
916-
return op(integers);
916+
return op(getContext(), integers);
917917
}
918918

919919
@TruffleBoundary
920-
private static long op(int[] integers) {
920+
private static long op(PythonContext context, int[] integers) {
921921
LocalDateTime localtime = LocalDateTime.of(integers[0], integers[1], integers[2], integers[3], integers[4], integers[5]);
922-
ZoneId timeZone = PythonContext.get(null).getEnv().getTimeZone();
922+
ZoneId timeZone = context.getEnv().getTimeZone();
923923
return localtime.toEpochSecond(timeZone.getRules().getOffset(localtime));
924924
}
925925
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/json/JSONScannerBuiltins.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
3939
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
4040
import com.oracle.truffle.api.CompilerAsserts;
41+
import com.oracle.truffle.api.CompilerDirectives;
42+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4143
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4244
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4345
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -73,26 +75,28 @@ public abstract static class CallScannerNode extends PythonTernaryClinicBuiltinN
7375

7476
@Child private HashingStorageLibrary mapLib = HashingStorageLibrary.getFactory().createDispatched(6);
7577

76-
private Shape tupleInstanceShape;
77-
private Shape listInstanceShape;
78-
private Shape dictInstanceShape;
78+
@CompilationFinal private Shape tupleInstanceShape;
79+
@CompilationFinal private Shape listInstanceShape;
80+
@CompilationFinal private Shape dictInstanceShape;
7981

8082
@Override
8183
protected ArgumentClinicProvider getArgumentClinic() {
8284
return JSONScannerBuiltinsClinicProviders.CallScannerNodeClinicProviderGen.INSTANCE;
8385
}
8486

8587
@Specialization
86-
@TruffleBoundary
8788
protected PTuple call(PJSONScanner self, String string, int idx) {
8889
if (tupleInstanceShape == null) {
89-
tupleInstanceShape = PythonLanguage.get(null).getBuiltinTypeInstanceShape(PythonBuiltinClassType.PTuple);
90+
CompilerDirectives.transferToInterpreterAndInvalidate();
91+
tupleInstanceShape = PythonLanguage.get(this).getBuiltinTypeInstanceShape(PythonBuiltinClassType.PTuple);
9092
}
9193
if (listInstanceShape == null) {
92-
listInstanceShape = PythonLanguage.get(null).getBuiltinTypeInstanceShape(PythonBuiltinClassType.PList);
94+
CompilerDirectives.transferToInterpreterAndInvalidate();
95+
listInstanceShape = PythonLanguage.get(this).getBuiltinTypeInstanceShape(PythonBuiltinClassType.PList);
9396
}
9497
if (dictInstanceShape == null) {
95-
dictInstanceShape = PythonLanguage.get(null).getBuiltinTypeInstanceShape(PythonBuiltinClassType.PDict);
98+
CompilerDirectives.transferToInterpreterAndInvalidate();
99+
dictInstanceShape = PythonLanguage.get(this).getBuiltinTypeInstanceShape(PythonBuiltinClassType.PDict);
96100
}
97101
IntRef nextIdx = new IntRef();
98102
Object result = scanOnceUnicode(self, string, idx, nextIdx);
@@ -358,6 +362,7 @@ private Object matchNumberUnicode(PJSONScanner scanner, String string, int start
358362
}
359363
}
360364

365+
@TruffleBoundary
361366
private Object scanOnceUnicode(PJSONScanner scanner, String string, int idx, IntRef nextIdx) {
362367
/*
363368
* Read one JSON term (of any kind) from PyUnicode pystr. idx is the index of the first

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PythonNativeWrapper.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,16 @@ public final Assumption ensureHandleValidAssumption() {
123123
return handleValidAssumption;
124124
}
125125

126-
protected static Assumption singleContextAssumption(Node node) {
127-
return PythonLanguage.get(node).singleContextAssumption;
126+
protected static Assumption singleContextAssumption() {
127+
return PythonLanguage.get(null).singleContextAssumption;
128128
}
129129

130130
@ExportMessage(name = "getDelegate")
131131
protected static class GetDelegate {
132-
@Specialization(guards = {"cachedWrapper == wrapper", "delegate != null"}, assumptions = "singleContextAssumption(lib)")
132+
@Specialization(guards = {"cachedWrapper == wrapper", "delegate != null"}, assumptions = "singleContextAssumption()")
133133
protected static Object getCachedDel(@SuppressWarnings("unused") PythonNativeWrapper wrapper,
134134
@SuppressWarnings("unused") @Cached(value = "wrapper", weak = true) PythonNativeWrapper cachedWrapper,
135-
@Cached(value = "wrapper.getDelegatePrivate()", weak = true) Object delegate,
136-
@SuppressWarnings("unused") @CachedLibrary("delegate") InteropLibrary lib) {
135+
@Cached(value = "wrapper.getDelegatePrivate()", weak = true) Object delegate) {
137136
return delegate;
138137
}
139138

@@ -154,12 +153,11 @@ protected void setDelegate(Object delegate) {
154153

155154
@ExportMessage(name = "getNativePointer")
156155
protected static class GetNativePointer {
157-
@Specialization(guards = {"cachedWrapper == wrapper", "nativePointer != null"}, assumptions = {"singleContextAssumption(lib)", "cachedAssumption"})
156+
@Specialization(guards = {"cachedWrapper == wrapper", "nativePointer != null"}, assumptions = {"singleContextAssumption()", "cachedAssumption"})
158157
protected static Object getCachedPtr(@SuppressWarnings("unused") PythonNativeWrapper wrapper,
159158
@SuppressWarnings("unused") @Cached(value = "wrapper", weak = true) PythonNativeWrapper cachedWrapper,
160159
@SuppressWarnings("unused") @Cached("wrapper.getHandleValidAssumption()") Assumption cachedAssumption,
161-
@Cached(value = "wrapper.getNativePointerPrivate()", weak = true) Object nativePointer,
162-
@SuppressWarnings("unused") @CachedLibrary("nativePointer") InteropLibrary lib) {
160+
@Cached(value = "wrapper.getNativePointerPrivate()", weak = true) Object nativePointer) {
163161
return nativePointer;
164162
}
165163

@@ -184,12 +182,11 @@ public void setNativePointer(Object nativePointer) {
184182

185183
@ExportMessage(name = "isNative")
186184
protected static class IsNative {
187-
@Specialization(guards = {"cachedWrapper == wrapper", "nativePointer != null"}, assumptions = {"singleContextAssumption(lib)", "cachedAssumption"})
185+
@Specialization(guards = {"cachedWrapper == wrapper", "nativePointer != null"}, assumptions = {"singleContextAssumption()", "cachedAssumption"})
188186
protected static boolean isCachedNative(@SuppressWarnings("unused") PythonNativeWrapper wrapper,
189187
@SuppressWarnings("unused") @Cached(value = "wrapper", weak = true) PythonNativeWrapper cachedWrapper,
190188
@SuppressWarnings("unused") @Cached("wrapper.getHandleValidAssumption()") Assumption cachedAssumption,
191-
@SuppressWarnings("unused") @Cached(value = "wrapper.getNativePointerPrivate()", weak = true) Object nativePointer,
192-
@SuppressWarnings("unused") @CachedLibrary("nativePointer") InteropLibrary lib) {
189+
@SuppressWarnings("unused") @Cached(value = "wrapper.getNativePointerPrivate()", weak = true) Object nativePointer) {
193190
return true;
194191
}
195192

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/PythonModule.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4646
import com.oracle.truffle.api.CompilerDirectives;
4747
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
48+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4849
import com.oracle.truffle.api.dsl.Cached;
4950
import com.oracle.truffle.api.dsl.Cached.Shared;
5051
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -100,9 +101,10 @@ private PythonModule(PythonLanguage lang, String moduleName) {
100101
/**
101102
* Only to be used during context creation
102103
*/
104+
@TruffleBoundary
103105
public static PythonModule createInternal(String moduleName) {
104106
PythonObjectFactory factory = PythonObjectFactory.getUncached();
105-
PythonModule pythonModule = new PythonModule(PythonLanguage.get(factory), moduleName);
107+
PythonModule pythonModule = new PythonModule(PythonLanguage.get(null), moduleName);
106108
PDict dict = factory.createDictFixedStorage(pythonModule);
107109
try {
108110
PythonObjectLibrary.getUncached().setDict(pythonModule, dict);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ private void incrementFDRefCount(int fd) {
580580

581581
/**
582582
* Decreases reference count for the given file descriptor.
583-
*
583+
*
584584
* @return {@code true} if ref count was decreased, {@code false} if ref count isn't tracked
585585
* anymore.
586586
*/
@@ -1029,12 +1029,12 @@ public PFrame.Reference peekTopFrameInfo(PythonLanguage lang) {
10291029

10301030
@TruffleBoundary
10311031
public boolean reprEnter(Object item) {
1032-
return getThreadState(PythonLanguage.get(null)).reprEnter(item);
1032+
return getThreadState(language).reprEnter(item);
10331033
}
10341034

10351035
@TruffleBoundary
10361036
public void reprLeave(Object item) {
1037-
getThreadState(PythonLanguage.get(null)).reprLeave(item);
1037+
getThreadState(language).reprLeave(item);
10381038
}
10391039

10401040
public long getPerfCounterStart() {

0 commit comments

Comments
 (0)