Skip to content

Commit 95ed582

Browse files
committed
cache source file for TASTy sources
1 parent 2458b8f commit 95ed582

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

compiler/src/dotty/tools/dotc/CompilationUnit.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ object CompilationUnit {
115115
/** Make a compilation unit for top class `clsd` with the contents of the `unpickled` tree */
116116
def apply(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit =
117117
val file = clsd.symbol.associatedFile.nn
118-
apply(SourceFile(file, Array.empty[Char]), unpickled, forceTrees)
118+
apply(ctx.getEmptySource(file), unpickled, forceTrees)
119119

120120
/** Make a compilation unit, given picked bytes and unpickled tree */
121121
def apply(source: SourceFile, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit = {

compiler/src/dotty/tools/dotc/core/Contexts.scala

+12-3
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,26 @@ object Contexts {
246246
/** Sourcefile corresponding to given abstract file, memoized */
247247
def getSource(file: AbstractFile, codec: => Codec = Codec(settings.encoding.value)) = {
248248
util.Stats.record("Context.getSource")
249+
computeCachedSource(file)(SourceFile(_, codec))
250+
}
251+
252+
/** empty Sourcefile associated to given abstract file, memoized */
253+
def getEmptySource(file: AbstractFile) = {
254+
util.Stats.record("Context.getEmptySource")
255+
computeCachedSource(file)(SourceFile(_, Array.empty[Char]))
256+
}
257+
258+
private inline def computeCachedSource(file: AbstractFile)(inline mkSource: AbstractFile => SourceFile): SourceFile =
249259
base.sources.getOrElseUpdate(file, {
250260
val zincSources = zincInitialFiles
251261
val cachedFile =
252262
if zincSources != null then zincSources.lookup(file) match
253263
case null => file
254-
case cached => cached
264+
case cached: AbstractFile => cached
255265
else
256266
file
257-
SourceFile(cachedFile, codec)
267+
mkSource(cachedFile)
258268
})
259-
}
260269

261270
/** SourceFile with given path name, memoized */
262271
def getSource(path: TermName): SourceFile = getFile(path) match

0 commit comments

Comments
 (0)