Skip to content

Commit b8ecb65

Browse files
committed
[GR-21822] Avoid unnecessary logger lookups.
PullRequest: graalpython/854
2 parents 2bfe6d1 + 17abac7 commit b8ecb65

File tree

10 files changed

+62
-32
lines changed

10 files changed

+62
-32
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
121121
public static final String EXTENSION = ".py";
122122
public static final String[] DEFAULT_PYTHON_EXTENSIONS = new String[]{EXTENSION, ".pyc"};
123123

124+
private static final TruffleLogger LOGGER = TruffleLogger.getLogger(ID, PythonLanguage.class);
125+
124126
public final Assumption singleContextAssumption = Truffle.getRuntime().createAssumption("Only a single context is active");
125127

126128
private final NodeFactory nodeFactory;
@@ -449,8 +451,8 @@ protected Iterable<Scope> findTopScopes(PythonContext context) {
449451
}
450452

451453
@TruffleBoundary
452-
public static TruffleLogger getLogger() {
453-
return TruffleLogger.getLogger(ID);
454+
public static TruffleLogger getLogger(Class<?> clazz) {
455+
return TruffleLogger.getLogger(ID, clazz);
454456
}
455457

456458
public static Source newSource(PythonContext ctxt, String src, String name, boolean mayBeFile) {
@@ -508,7 +510,7 @@ protected void initializeMultipleContexts() {
508510
@TruffleBoundary
509511
public CallTarget cacheCode(String filename, Supplier<CallTarget> createCode) {
510512
return cachedCode.computeIfAbsent(filename, f -> {
511-
PythonLanguage.getLogger().log(Level.FINEST, () -> "Caching CallTarget for " + filename);
513+
LOGGER.log(Level.FINEST, () -> "Caching CallTarget for " + filename);
512514
return createCode.get();
513515
});
514516
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import java.util.function.Supplier;
4141
import java.util.logging.Level;
4242

43+
import org.graalvm.nativeimage.ImageInfo;
44+
4345
import com.oracle.graal.python.PythonLanguage;
4446
import com.oracle.graal.python.builtins.modules.ArrayModuleBuiltins;
4547
import com.oracle.graal.python.builtins.modules.AstModuleBuiltins;
@@ -94,8 +96,8 @@
9496
import com.oracle.graal.python.builtins.objects.PNone;
9597
import com.oracle.graal.python.builtins.objects.array.ArrayBuiltins;
9698
import com.oracle.graal.python.builtins.objects.bool.BoolBuiltins;
97-
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins;
9899
import com.oracle.graal.python.builtins.objects.bytes.ByteArrayBuiltins;
100+
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins;
99101
import com.oracle.graal.python.builtins.objects.cell.CellBuiltins;
100102
import com.oracle.graal.python.builtins.objects.code.CodeBuiltins;
101103
import com.oracle.graal.python.builtins.objects.complex.ComplexBuiltins;
@@ -182,19 +184,19 @@
182184
import com.oracle.truffle.api.Truffle;
183185
import com.oracle.truffle.api.TruffleFile;
184186
import com.oracle.truffle.api.TruffleLanguage.Env;
187+
import com.oracle.truffle.api.TruffleLogger;
185188
import com.oracle.truffle.api.TruffleOptions;
186189
import com.oracle.truffle.api.nodes.Node;
187190
import com.oracle.truffle.api.nodes.RootNode;
188191
import com.oracle.truffle.api.source.Source;
189192
import com.oracle.truffle.api.source.SourceSection;
190193

191-
import org.graalvm.nativeimage.ImageInfo;
192-
193194
/**
194195
* The core is intended to the immutable part of the interpreter, including most modules and most
195196
* types.
196197
*/
197198
public final class Python3Core implements PythonCore {
199+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(Python3Core.class);
198200
private final String[] coreFiles;
199201

200202
private static final String[] initializeCoreFiles() {
@@ -625,7 +627,7 @@ private Source getInternalSource(String basename, String prefix) {
625627
} catch (SecurityException e) {
626628
errorMessage = "Startup failed, a security exception occurred while reading from " + file + ". Maybe you need to set python.CoreHome and python.StdLibHome.";
627629
}
628-
PythonLanguage.getLogger().log(Level.SEVERE, errorMessage);
630+
LOGGER.log(Level.SEVERE, errorMessage);
629631
PException e = new PException(null, null);
630632
e.setMessage(errorMessage);
631633
throw e;

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import com.oracle.truffle.api.RootCallTarget;
9898
import com.oracle.truffle.api.TruffleFile;
9999
import com.oracle.truffle.api.TruffleLanguage.Env;
100+
import com.oracle.truffle.api.TruffleLogger;
100101
import com.oracle.truffle.api.dsl.Cached;
101102
import com.oracle.truffle.api.dsl.Cached.Shared;
102103
import com.oracle.truffle.api.dsl.CachedContext;
@@ -165,6 +166,8 @@ public boolean run() {
165166
@Builtin(name = "__create_dynamic__", minNumOfPositionalArgs = 2)
166167
@GenerateNodeFactory
167168
public abstract static class CreateDynamic extends PythonBuiltinNode {
169+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(CreateDynamic.class);
170+
168171
protected static final String INITIALIZE_CAPI = "initialize_capi";
169172
protected static final String IMPORT_NATIVE_MEMORYVIEW = "import_native_memoryview";
170173
protected static final String RUN_CAPI_LOADED_HOOKS = "run_capi_loaded_hooks";
@@ -219,7 +222,7 @@ private Object loadDynamicModuleWithSpec(String name, String path, InteropLibrar
219222
CallTarget callTarget = env.parseInternal(Source.newBuilder(LLVM_LANGUAGE, context.getPublicTruffleFileRelaxed(path, extSuffix)).build());
220223
sulongLibrary = (TruffleObject) callTarget.call();
221224
} catch (SecurityException | IOException e) {
222-
PythonLanguage.getLogger().severe(() -> String.format("cannot load C extension '%s'", path));
225+
LOGGER.severe(() -> String.format("cannot load C extension '%s'", path));
223226
logJavaException(e);
224227
throw raise(ImportError, "cannot load %s: %m", path, e);
225228
} catch (RuntimeException e) {
@@ -275,8 +278,8 @@ private void ensureCapiWasLoaded() {
275278
}
276279
capi = context.getEnv().parseInternal(capiSrcBuilder.build()).call();
277280
} catch (IOException | RuntimeException e) {
278-
PythonLanguage.getLogger().severe(() -> String.format(CAPI_LOAD_ERROR, capiFile.getAbsoluteFile().getPath()));
279-
PythonLanguage.getLogger().severe(() -> "Original error was: " + e);
281+
LOGGER.severe(() -> String.format(CAPI_LOAD_ERROR, capiFile.getAbsoluteFile().getPath()));
282+
LOGGER.severe(() -> "Original error was: " + e);
280283
e.printStackTrace();
281284
throw raise(PythonErrorType.ImportError, CAPI_LOAD_ERROR, capiFile.getAbsoluteFile().getPath());
282285
}
@@ -296,7 +299,7 @@ private void ensureCapiWasLoaded() {
296299
}
297300

298301
private static void logJavaException(Exception e) {
299-
PythonLanguage.getLogger().fine(() -> String.format("Original error was: %s\n%s", e, getJavaStacktrace(e)));
302+
LOGGER.fine(() -> String.format("Original error was: %s\n%s", e, getJavaStacktrace(e)));
300303
}
301304

302305
@TruffleBoundary
@@ -459,6 +462,8 @@ public Object run(PCode code, String path) {
459462
@Builtin(name = "graal_python_cache_module_code", minNumOfPositionalArgs = 3)
460463
@GenerateNodeFactory
461464
public abstract static class CacheModuleCode extends PythonTernaryBuiltinNode {
465+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(CacheModuleCode.class);
466+
462467
@Specialization
463468
public Object run(String modulename, String moduleFile, @SuppressWarnings("unused") PNone modulepath,
464469
@Shared("ctxt") @CachedContext(PythonLanguage.class) PythonContext ctxt,
@@ -495,7 +500,7 @@ private Object doCache(String modulename, String moduleFile, String[] modulepath
495500
private static Object cacheWithModulePath(String modulename, String[] modulepath, PythonLanguage lang, final CallTarget ct) {
496501
CallTarget cachedCt = lang.cacheCode(modulename, () -> ct, modulepath);
497502
if (cachedCt != ct) {
498-
PythonLanguage.getLogger().log(Level.WARNING, () -> "Invalid attempt to re-cache " + modulename);
503+
LOGGER.log(Level.WARNING, () -> "Invalid attempt to re-cache " + modulename);
499504
}
500505
return PNone.NONE;
501506
}
@@ -530,13 +535,15 @@ public Object run(String modulename, PCode code, PList modulepath,
530535
@Builtin(name = "graal_python_has_cached_code", minNumOfPositionalArgs = 1)
531536
@GenerateNodeFactory
532537
public abstract static class HasCachedCode extends PythonUnaryBuiltinNode {
538+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(HasCachedCode.class);
539+
533540
@Specialization
534541
public boolean run(String modulename,
535542
@CachedContext(PythonLanguage.class) PythonContext ctxt,
536543
@CachedLanguage PythonLanguage lang) {
537544
boolean b = PythonOptions.getFlag(ctxt, PythonOptions.WithCachedSources) && lang.hasCachedCode(modulename);
538545
if (b) {
539-
PythonLanguage.getLogger().log(Level.FINEST, () -> "Cached code re-used for " + modulename);
546+
LOGGER.log(Level.FINEST, () -> "Cached code re-used for " + modulename);
540547
}
541548
return b;
542549
}
@@ -545,6 +552,8 @@ public boolean run(String modulename,
545552
@Builtin(name = "graal_python_get_cached_code_path", minNumOfPositionalArgs = 1)
546553
@GenerateNodeFactory
547554
public abstract static class CachedCodeIsPackage extends PythonUnaryBuiltinNode {
555+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(CachedCodeIsPackage.class);
556+
548557
@Specialization
549558
public Object run(String modulename,
550559
@CachedContext(PythonLanguage.class) PythonContext ctxt,
@@ -556,7 +565,7 @@ public Object run(String modulename,
556565
if (modulePath != null) {
557566
Object[] outPath = new Object[modulePath.length];
558567
System.arraycopy(modulePath, 0, outPath, 0, modulePath.length);
559-
PythonLanguage.getLogger().log(Level.FINEST, () -> "Cached code re-used for " + modulename);
568+
LOGGER.log(Level.FINEST, () -> "Cached code re-used for " + modulename);
560569
return factory().createList(outPath);
561570
} else {
562571
return PNone.NONE;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7979
import com.oracle.truffle.api.TruffleFile;
8080
import com.oracle.truffle.api.TruffleLanguage.Env;
81+
import com.oracle.truffle.api.TruffleLogger;
8182
import com.oracle.truffle.api.dsl.Cached;
8283
import com.oracle.truffle.api.dsl.CachedContext;
8384
import com.oracle.truffle.api.dsl.Fallback;
@@ -241,6 +242,8 @@ protected boolean isMimeType(String lang) {
241242
@Builtin(name = "export_value", minNumOfPositionalArgs = 1, parameterNames = {"name", "value"})
242243
@GenerateNodeFactory
243244
public abstract static class ExportSymbolNode extends PythonBuiltinNode {
245+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(ExportSymbolNode.class);
246+
244247
@Child private GetAttributeNode getNameAttributeNode;
245248
@Child private CastToJavaStringNode castToStringNode;
246249

@@ -258,15 +261,15 @@ public Object exportSymbolKeyValue(String name, Object value) {
258261
@Specialization(guards = "!isString(value)")
259262
@TruffleBoundary
260263
public Object exportSymbolValueKey(Object value, String name) {
261-
PythonLanguage.getLogger().warning("[deprecation] polyglot.export_value(value, name) is deprecated " +
264+
LOGGER.warning("[deprecation] polyglot.export_value(value, name) is deprecated " +
262265
"and will be removed. Please swap the arguments.");
263266
return exportSymbolKeyValue(name, value);
264267
}
265268

266269
@Specialization(guards = "isString(arg1)")
267270
@TruffleBoundary
268271
public Object exportSymbolAmbiguous(Object arg1, String arg2) {
269-
PythonLanguage.getLogger().warning("[deprecation] polyglot.export_value(str, str) is ambiguous. In the future, this will " +
272+
LOGGER.warning("[deprecation] polyglot.export_value(str, str) is ambiguous. In the future, this will " +
270273
"default to using the first argument as the name and the second as value, but now it " +
271274
"uses the first argument as value and the second as the name.");
272275
return exportSymbolValueKey(arg1, arg2);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
142142
import com.oracle.truffle.api.TruffleFile;
143143
import com.oracle.truffle.api.TruffleLanguage.Env;
144+
import com.oracle.truffle.api.TruffleLogger;
144145
import com.oracle.truffle.api.dsl.Cached;
145146
import com.oracle.truffle.api.dsl.Cached.Shared;
146147
import com.oracle.truffle.api.dsl.Fallback;
@@ -1484,6 +1485,8 @@ PTuple waitpidFallback(VirtualFrame frame, Object pid, Object options,
14841485
@GenerateNodeFactory
14851486
@TypeSystemReference(PythonArithmeticTypes.class)
14861487
abstract static class SystemNode extends PythonBuiltinNode {
1488+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(SystemNode.class);
1489+
14871490
static final String[] shell;
14881491
static {
14891492
String osProperty = System.getProperty("os.name");
@@ -1538,7 +1541,7 @@ int system(String cmd) {
15381541
if (!context.isExecutableAccessAllowed()) {
15391542
return -1;
15401543
}
1541-
PythonLanguage.getLogger().fine(() -> "os.system: " + cmd);
1544+
LOGGER.fine(() -> "os.system: " + cmd);
15421545
String[] command = new String[]{shell[0], shell[1], cmd};
15431546
Env env = context.getEnv();
15441547
try {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import com.oracle.graal.python.runtime.PythonContext;
7373
import com.oracle.graal.python.runtime.PythonOptions;
7474
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
75+
import com.oracle.truffle.api.TruffleLogger;
7576
import com.oracle.truffle.api.dsl.Cached;
7677
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7778
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -91,6 +92,8 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9192
"errpipe_read", "errpipe_write", "restore_signals", "call_setsid", "preexec_fn"}, needsFrame = true)
9293
@GenerateNodeFactory
9394
abstract static class ForkExecNode extends PythonBuiltinNode {
95+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(ForkExecNode.class);
96+
9497
@Child private BytesNodes.ToBytesNode toBytes = BytesNodes.ToBytesNode.create();
9598

9699
@Specialization
@@ -148,7 +151,7 @@ private synchronized int forkExec(PList args, @SuppressWarnings("unused") PList
148151
}
149152
}
150153

151-
PythonLanguage.getLogger().fine(() -> "_posixsubprocess.fork_exec: " + String.join(" ", argStrings));
154+
LOGGER.fine(() -> "_posixsubprocess.fork_exec: " + String.join(" ", argStrings));
152155
ProcessBuilder pb = new ProcessBuilder(argStrings);
153156
if (p2cread != -1 && p2cwrite != -1) {
154157
pb.redirectInput(Redirect.PIPE);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
import com.oracle.truffle.api.CompilerAsserts;
137137
import com.oracle.truffle.api.CompilerDirectives;
138138
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
139+
import com.oracle.truffle.api.TruffleLogger;
139140
import com.oracle.truffle.api.dsl.Cached;
140141
import com.oracle.truffle.api.dsl.Cached.Exclusive;
141142
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -576,6 +577,7 @@ public static ReadTypeNativeMemberNode create() {
576577

577578
@GenerateUncached
578579
abstract static class ReadObjectNativeMemberNode extends ReadNativeMemberNode {
580+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(ReadObjectNativeMemberNode.class);
579581

580582
@Specialization(guards = "eq(D_COMMON, key)")
581583
Object doDCommon(Object o, @SuppressWarnings("unused") String key,
@@ -894,14 +896,15 @@ Object doGeneric(Object object, String key,
894896

895897
@TruffleBoundary(allowInlining = true)
896898
private static void logGeneric(String key) {
897-
PythonLanguage.getLogger().log(Level.FINE, "read of Python struct native member " + key);
899+
LOGGER.log(Level.FINE, "read of Python struct native member " + key);
898900
}
899901
}
900902

901903
// WRITE
902904
@GenerateUncached
903905
@ImportStatic({NativeMemberNames.class, PGuards.class, SpecialMethodNames.class, SpecialAttributeNames.class})
904906
abstract static class WriteNativeMemberNode extends Node {
907+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(WriteNativeMemberNode.class);
905908

906909
abstract Object execute(Object receiver, String key, Object value) throws UnsupportedMessageException, UnknownIdentifierException, UnsupportedTypeException;
907910

@@ -1035,7 +1038,7 @@ Object err(Object object, String key, Object value) throws UnknownIdentifierExce
10351038

10361039
@TruffleBoundary(allowInlining = true)
10371040
private static void logGeneric(String key) {
1038-
PythonLanguage.getLogger().log(Level.FINE, "write of Python struct native member " + key);
1041+
LOGGER.log(Level.FINE, "write of Python struct native member " + key);
10391042
}
10401043

10411044
protected static boolean eq(String expected, String actual) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/ListLiteralNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@
3939
import com.oracle.truffle.api.CompilerDirectives;
4040
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4141
import com.oracle.truffle.api.CompilerDirectives.ValueType;
42+
import com.oracle.truffle.api.TruffleLogger;
4243
import com.oracle.truffle.api.frame.VirtualFrame;
4344
import com.oracle.truffle.api.nodes.ExplodeLoop;
4445

4546
public final class ListLiteralNode extends SequenceLiteralNode {
47+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(ListLiteralNode.class);
48+
4649
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
4750
@Child private SequenceStorageNodes.ConcatNode concatStoragesNode;
4851
@Child private SequenceStorageNodes.AppendNode appendNode;
@@ -144,14 +147,14 @@ public void reportUpdatedCapacity(BasicSequenceStorage newStore) {
144147
if (PythonOptions.getFlag(lookupContextReference(PythonLanguage.class).get(), PythonOptions.OverallocateLiteralLists)) {
145148
if (newStore.capacity() > initialCapacity.estimate()) {
146149
initialCapacity.updateFrom(newStore.capacity());
147-
PythonLanguage.getLogger().fine(() -> {
150+
LOGGER.fine(() -> {
148151
return String.format("Updating list size estimate at %s. Observed capacity: %d, new estimate: %d", getSourceSection().toString(), newStore.capacity(),
149152
initialCapacity.estimate());
150153
});
151154
}
152155
if (newStore.getElementType().generalizesFrom(type)) {
153156
type = newStore.getElementType();
154-
PythonLanguage.getLogger().fine(() -> {
157+
LOGGER.fine(() -> {
155158
return String.format("Updating list type estimate at %s. New type: %s", getSourceSection().toString(), type.name());
156159
});
157160
}

0 commit comments

Comments
 (0)