@@ -365,7 +365,11 @@ public boolean isSingleContext() {
365365 */
366366 private final ConcurrentHashMap <Object , Source > sourceCache = new ConcurrentHashMap <>();
367367
368- private final ConcurrentHashMap <Source , TruffleFile > originalFiles = new ConcurrentHashMap <>();
368+ /*
369+ * A map from sources without content to either a Source object with content or a TruffleFile
370+ * that can be used to construct such object.
371+ */
372+ private final WeakHashMap <Source , Object > originalSources = new WeakHashMap <>();
369373
370374 @ Idempotent
371375 public static PythonLanguage get (Node node ) {
@@ -1226,23 +1230,30 @@ public Source getOrCreateSourceWithContent(Source sourceWithoutContent) {
12261230 if (sourceWithoutContent .hasCharacters ()) {
12271231 return sourceWithoutContent ;
12281232 }
1229- TruffleFile originalFile = originalFiles .get (sourceWithoutContent );
1230- if (originalFile == null ) {
1231- return sourceWithoutContent ;
1232- }
1233- return getOrCreateSource (ignored -> loadSourceWithContent (sourceWithoutContent , originalFile ), sourceWithoutContent );
1234- }
1235-
1236- private Source loadSourceWithContent (Source sourceWithoutContent , TruffleFile originalFile ) {
1237- try {
1238- return Source .newBuilder (ID , originalFile ).name (sourceWithoutContent .getName ()).internal (sourceWithoutContent .isInternal ()).mimeType (MIME_TYPE ).build ();
1239- } catch (IOException | SecurityException | UnsupportedOperationException | IllegalArgumentException e ) {
1233+ synchronized (originalSources ) {
1234+ Object original = originalSources .get (sourceWithoutContent );
1235+ if (original instanceof Source originalSource ) {
1236+ return originalSource ;
1237+ }
1238+ if (original instanceof TruffleFile originalFile ) {
1239+ Source source ;
1240+ try {
1241+ source = Source .newBuilder (ID , originalFile ).name (sourceWithoutContent .getName ()).internal (sourceWithoutContent .isInternal ()).mimeType (MIME_TYPE ).build ();
1242+ } catch (IOException | SecurityException | UnsupportedOperationException | IllegalArgumentException e ) {
1243+ source = sourceWithoutContent ;
1244+ }
1245+ originalSources .put (sourceWithoutContent , source );
1246+ return source ;
1247+ }
1248+ assert original == null ;
12401249 return sourceWithoutContent ;
12411250 }
12421251 }
12431252
12441253 public void registerOriginalFile (Source sourceWithoutContent , TruffleFile originalFile ) {
1245- originalFiles .put (sourceWithoutContent , originalFile );
1254+ synchronized (originalSources ) {
1255+ originalSources .put (sourceWithoutContent , originalFile );
1256+ }
12461257 }
12471258
12481259 public static PythonOS getPythonOS () {
0 commit comments