3030import static com .oracle .graal .python .nodes .StringLiterals .J_PY_EXTENSION ;
3131import static com .oracle .graal .python .nodes .StringLiterals .T_PY_EXTENSION ;
3232import static com .oracle .graal .python .nodes .truffle .TruffleStringMigrationHelpers .isJavaString ;
33+ import static com .oracle .graal .python .util .PythonUtils .ARRAY_ACCESSOR ;
3334import static com .oracle .graal .python .util .PythonUtils .TS_ENCODING ;
3435import static com .oracle .graal .python .util .PythonUtils .tsLiteral ;
3536
6061import com .oracle .graal .python .annotations .PythonOS ;
6162import com .oracle .graal .python .builtins .Python3Core ;
6263import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
63- import com .oracle .graal .python .builtins .modules .MarshalModuleBuiltins ;
64+ import com .oracle .graal .python .builtins .modules .ImpModuleBuiltins ;
6465import com .oracle .graal .python .builtins .modules .SignalModuleBuiltins ;
6566import com .oracle .graal .python .builtins .objects .PNone ;
6667import com .oracle .graal .python .builtins .objects .PNotImplemented ;
160161 "text/x-python-\2 \u0100 -eval" , "text/x-python-\2 \u0100 -compile" , "text/x-python-\0 \u0040 -eval" , "text/x-python-\0 \u0040 -compile" , "text/x-python-\1 \u0040 -eval" ,
161162 "text/x-python-\1 \u0040 -compile" , "text/x-python-\2 \u0040 -eval" , "text/x-python-\2 \u0040 -compile" , "text/x-python-\0 \u0140 -eval" , "text/x-python-\0 \u0140 -compile" ,
162163 "text/x-python-\1 \u0140 -eval" , "text/x-python-\1 \u0140 -compile" , "text/x-python-\2 \u0140 -eval" , "text/x-python-\2 \u0140 -compile" }, //
163- byteMimeTypes = {PythonLanguage .MIME_TYPE_BYTECODE }, //
164164 defaultMimeType = PythonLanguage .MIME_TYPE , //
165165 dependentLanguages = {"nfi" , "llvm" }, //
166166 interactive = true , internal = false , //
@@ -312,8 +312,6 @@ private static boolean mimeTypesComplete(ArrayList<String> mimeJavaStrings) {
312312 assert mimeTypesComplete (mimeJavaStrings ) : "Expected all of {" + String .join (", " , mimeJavaStrings ) + "} in the PythonLanguage characterMimeTypes" ;
313313 }
314314
315- public static final String MIME_TYPE_BYTECODE = "application/x-python-bytecode" ;
316-
317315 public static final TruffleString [] T_DEFAULT_PYTHON_EXTENSIONS = new TruffleString []{T_PY_EXTENSION , tsLiteral (".pyc" )};
318316
319317 public static final TruffleLogger LOGGER = TruffleLogger .getLogger (ID , PythonLanguage .class );
@@ -557,11 +555,6 @@ protected CallTarget parse(ParsingRequest request) {
557555 if (!request .getArgumentNames ().isEmpty ()) {
558556 throw new IllegalStateException ("parse with arguments is only allowed for " + MIME_TYPE + " mime type" );
559557 }
560- if (MIME_TYPE_BYTECODE .equals (source .getMimeType ())) {
561- byte [] bytes = source .getBytes ().toByteArray ();
562- CodeUnit code = MarshalModuleBuiltins .deserializeCodeUnit (null , context , bytes );
563- return callTargetFromBytecode (context , source , code );
564- }
565558
566559 String mime = source .getMimeType ();
567560 String prefix = mime .substring (0 , MIME_PREFIX .length ());
@@ -586,7 +579,7 @@ protected CallTarget parse(ParsingRequest request) {
586579 return parse (context , source , type , false , optimize , false , null , FutureFeature .fromFlags (flags ));
587580 }
588581
589- public RootCallTarget callTargetFromBytecode (PythonContext context , Source source , CodeUnit code ) {
582+ public static RootCallTarget callTargetFromBytecode (PythonContext context , Source source , CodeUnit code ) {
590583 boolean internal = shouldMarkSourceInternal (context );
591584 SourceBuilder builder = null ;
592585 // The original file path should be passed as the name
@@ -616,7 +609,7 @@ public RootCallTarget callTargetFromBytecode(PythonContext context, Source sourc
616609 // TODO lazily load source in bytecode DSL interpreter too
617610 rootNode = ((BytecodeDSLCodeUnit ) code ).createRootNode (context , lazySource .getSource ());
618611 } else {
619- rootNode = PBytecodeRootNode .create (this , (BytecodeCodeUnit ) code , lazySource , internal );
612+ rootNode = PBytecodeRootNode .create (context . getLanguage () , (BytecodeCodeUnit ) code , lazySource , internal );
620613 }
621614
622615 return PythonUtils .getOrCreateCallTarget (rootNode );
@@ -999,10 +992,17 @@ protected void initializeMultipleContexts() {
999992 singleContext = false ;
1000993 }
1001994
1002- private final ConcurrentHashMap <TruffleString , CallTarget > cachedCode = new ConcurrentHashMap <>();
995+ public record CodeCacheKey (TruffleString filename , long codeHash ) {
996+ }
997+
998+ private final ConcurrentHashMap <CodeCacheKey , CallTarget > cachedCode = new ConcurrentHashMap <>();
1003999
1004- @ TruffleBoundary
10051000 public CallTarget cacheCode (TruffleString filename , Supplier <CallTarget > createCode ) {
1001+ return cacheCode (new CodeCacheKey (filename , 0 ), createCode );
1002+ }
1003+
1004+ @ TruffleBoundary
1005+ public CallTarget cacheCode (CodeCacheKey filename , Supplier <CallTarget > createCode ) {
10061006 if (!singleContext ) {
10071007 return cachedCode .computeIfAbsent (filename , f -> {
10081008 LOGGER .log (Level .FINEST , () -> "Caching CallTarget for " + filename );
@@ -1013,6 +1013,19 @@ public CallTarget cacheCode(TruffleString filename, Supplier<CallTarget> createC
10131013 }
10141014 }
10151015
1016+ public long cacheKeyForBytecode (byte [] code , int length ) {
1017+ if (singleContext ) {
1018+ // No caching in single context
1019+ return 0 ;
1020+ }
1021+ byte [] hashBytes = ImpModuleBuiltins .SourceHashNode .hashSource (0 , code , length );
1022+ return ARRAY_ACCESSOR .getLong (hashBytes , 0 );
1023+ }
1024+
1025+ public long cacheKeyForBytecode (byte [] code ) {
1026+ return cacheKeyForBytecode (code , code .length );
1027+ }
1028+
10161029 private static final Source LINEBREAK_REGEX_SOURCE = Source .newBuilder ("regex" , "/\r \n |[\n \u000B \u000C \r \u0085 \u2028 \u2029 ]/" , "re_linebreak" ) //
10171030 .option ("regex.Flavor" , "Python" ) //
10181031 .option ("regex.Encoding" , "UTF-32" ) //
0 commit comments