Skip to content

Commit f75c7c5

Browse files
committed
cache sources created during startup
1 parent a702f14 commit f75c7c5

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
import java.text.MessageFormat;
3131
import java.util.ArrayList;
3232
import java.util.concurrent.ConcurrentHashMap;
33+
import java.util.function.Supplier;
3334

3435
import org.graalvm.options.OptionDescriptors;
3536

3637
import com.oracle.graal.python.builtins.Python3Core;
3738
import com.oracle.graal.python.builtins.objects.PNone;
3839
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
40+
import com.oracle.graal.python.builtins.objects.code.PCode;
3941
import com.oracle.graal.python.builtins.objects.function.PArguments;
4042
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
4143
import com.oracle.graal.python.builtins.objects.function.PKeyword;
@@ -384,12 +386,22 @@ public static Source newSource(PythonContext ctxt, String src, String name) {
384386
}
385387
}
386388

387-
public static Source newSource(PythonContext ctxt, TruffleFile src, String name) throws IOException {
388-
return newSource(ctxt, Source.newBuilder(ID, src), name);
389+
private final ConcurrentHashMap<Object, Source> cachedSources = new ConcurrentHashMap<>();
390+
391+
public Source newSource(PythonContext ctxt, TruffleFile src, String name) throws IOException {
392+
Source source = cachedSources.get(src);
393+
if (source == null) {
394+
cachedSources.put(src, source = newSource(ctxt, Source.newBuilder(ID, src), name));
395+
}
396+
return source;
389397
}
390398

391-
public static Source newSource(PythonContext ctxt, URL url, String name) throws IOException {
392-
return newSource(ctxt, Source.newBuilder(ID, url), name);
399+
public Source newSource(PythonContext ctxt, URL url, String name) throws IOException {
400+
Source source = cachedSources.get(url);
401+
if (source == null) {
402+
cachedSources.put(url, source = newSource(ctxt, Source.newBuilder(ID, url), name));
403+
}
404+
return source;
393405
}
394406

395407
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder, String name) throws IOException {
@@ -411,4 +423,10 @@ protected void initializeMultipleContexts() {
411423
super.initializeMultipleContexts();
412424
singleContextAssumption.invalidate();
413425
}
426+
427+
private final ConcurrentHashMap<String, PCode> cachedCode = new ConcurrentHashMap<>();
428+
429+
public Object cacheCode(String filename, Supplier<PCode> createCode) {
430+
return cachedCode.computeIfAbsent(filename, f -> createCode.get());
431+
}
414432
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,15 @@ private Source getSource(String basename, String prefix) {
575575
if (url != null) {
576576
// This path is hit when we load the core library e.g. from a Jar file
577577
try {
578-
return PythonLanguage.newSource(ctxt, new URL(url + suffix), basename);
578+
return getLanguage().newSource(ctxt, new URL(url + suffix), basename);
579579
} catch (IOException e) {
580580
throw new RuntimeException("Could not read core library from " + url);
581581
}
582582
} else {
583583
Env env = ctxt.getEnv();
584584
TruffleFile file = env.getTruffleFile(prefix + suffix);
585585
try {
586-
return PythonLanguage.newSource(ctxt, file, basename);
586+
return getLanguage().newSource(ctxt, file, basename);
587587
} catch (SecurityException | IOException t) {
588588
throw new RuntimeException("Could not read core library from " + file);
589589
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,12 @@ Object compile(String expression, String filename, String mode, Object kwFlags,
612612
} else {
613613
throw raise(ValueError, "compile() mode must be 'exec', 'eval' or 'single'");
614614
}
615-
return factory().createCode((RootNode) getCore().getParser().parse(pm, getCore(), source, null));
615+
Supplier<PCode> createCode = () -> factory().createCode((RootNode) getCore().getParser().parse(pm, getCore(), source, null));
616+
if (getCore().isInitialized()) {
617+
return createCode.get();
618+
} else {
619+
return getCore().getLanguage().cacheCode(filename, createCode);
620+
}
616621
}
617622

618623
@SuppressWarnings("unused")

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import java.util.concurrent.locks.ReentrantLock;
5151
import java.util.regex.Pattern;
5252

53-
import com.oracle.graal.python.PythonLanguage;
5453
import com.oracle.graal.python.builtins.Builtin;
5554
import com.oracle.graal.python.builtins.CoreFunctions;
5655
import com.oracle.graal.python.builtins.PythonBuiltins;
@@ -375,7 +374,7 @@ public Object run(String path, PythonModule mod) {
375374
} else {
376375
file = env.getTruffleFile(path);
377376
}
378-
Source src = PythonLanguage.newSource(ctxt, file, fileName);
377+
Source src = getCore().getLanguage().newSource(ctxt, file, fileName);
379378
CallTarget callTarget = env.parse(src);
380379
callTarget.call(PArguments.withGlobals(mod));
381380
} catch (PException e) {

0 commit comments

Comments
 (0)