Skip to content

Commit b5c096f

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

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-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

+13-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import util.Store
4040
import plugins._
4141
import java.util.concurrent.atomic.AtomicInteger
4242
import java.nio.file.InvalidPathException
43+
import scala.compiletime.ops.double.Abs
4344

4445
object Contexts {
4546

@@ -246,17 +247,26 @@ object Contexts {
246247
/** Sourcefile corresponding to given abstract file, memoized */
247248
def getSource(file: AbstractFile, codec: => Codec = Codec(settings.encoding.value)) = {
248249
util.Stats.record("Context.getSource")
250+
computeCachedSource(file)(SourceFile(_, codec))
251+
}
252+
253+
/** empty Sourcefile associated to given abstract file, memoized */
254+
def getEmptySource(file: AbstractFile) = {
255+
util.Stats.record("Context.getEmptySource")
256+
computeCachedSource(file)(SourceFile(_, Array.empty[Char]))
257+
}
258+
259+
private inline def computeCachedSource(file: AbstractFile)(inline mkSource: AbstractFile => SourceFile): SourceFile =
249260
base.sources.getOrElseUpdate(file, {
250261
val zincSources = zincInitialFiles
251262
val cachedFile =
252263
if zincSources != null then zincSources.lookup(file) match
253264
case null => file
254-
case cached => cached
265+
case cached: AbstractFile => cached
255266
else
256267
file
257-
SourceFile(cachedFile, codec)
268+
mkSource(cachedFile)
258269
})
259-
}
260270

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

0 commit comments

Comments
 (0)