Skip to content

Commit 729adc5

Browse files
committed
Memoize sourceFile creation
1 parent 6b33da7 commit 729adc5

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
104104
if (f.isDirectory) {
105105
ctx.error(s"expected file, received directory '$fileName'")
106106
NoSource
107-
} else if (f.exists) {
108-
val encoding = ctx.settings.encoding.value
109-
new SourceFile(f, Codec(encoding))
110-
} else {
107+
}
108+
else if (f.exists)
109+
ctx.getSource(f)
110+
else {
111111
ctx.error(s"not found: $fileName")
112112
NoSource
113113
}

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11361136
*/
11371137
def sourceFile(call: Tree)(implicit ctx: Context): SourceFile = {
11381138
val file = call.symbol.sourceFile
1139-
val encoding = ctx.settings.encoding.value
1140-
if (file != null && file.exists) new SourceFile(file, Codec(encoding)) else NoSource
1139+
if (file != null && file.exists) ctx.getSource(file) else NoSource
11411140
}
11421141

11431142
/** Desugar identifier into a select node. Return the tree itself if not possible */

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import config.Settings._
2020
import config.Config
2121
import reporting._
2222
import reporting.diagnostic.Message
23+
import io.AbstractFile
24+
import scala.io.Codec
2325
import collection.mutable
2426
import printing._
2527
import config.{JavaPlatform, SJSPlatform, Platform, ScalaSettings}
@@ -224,6 +226,10 @@ object Contexts {
224226
implicitsCache
225227
}
226228

229+
/** Sourcefile corresponding to given abstract file, memoized */
230+
def getSource(file: AbstractFile, codec: => Codec = Codec(settings.encoding.value)) =
231+
base.sources.getOrElseUpdate(file, new SourceFile(file, codec))
232+
227233
/** Those fields are used to cache phases created in withPhase.
228234
* phasedCtx is first phase with altered phase ever requested.
229235
* phasedCtxs is array that uses phaseId's as indexes,
@@ -624,6 +630,9 @@ object Contexts {
624630

625631
def nextId: Int = { _nextId += 1; _nextId }
626632

633+
/** Sources that were loaded */
634+
val sources: mutable.HashMap[AbstractFile, SourceFile] = new mutable.HashMap[AbstractFile, SourceFile]
635+
627636
// Types state
628637
/** A table for hash consing unique types */
629638
private[core] val uniques: util.HashSet[Type] = new util.HashSet[Type](Config.initialUniquesCapacity) {
@@ -696,6 +705,7 @@ object Contexts {
696705
def reset(): Unit = {
697706
for ((_, set) <- uniqueSets) set.clear()
698707
errorTypeMsg.clear()
708+
sources.clear()
699709
}
700710

701711
// Test that access is single threaded

compiler/src/dotty/tools/dotc/interactive/SourceTree.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ object SourceTree {
6161
case PackageDef(_, stats) =>
6262
stats.flatMap(sourceTreeOfClass).headOption
6363
case tree: tpd.TypeDef if tree.symbol == sym =>
64-
val sourceFile = new SourceFile(sym.sourceFile, Codec.UTF8)
64+
val sourceFile = ctx.getSource(sym.sourceFile, Codec.UTF8)
6565
Some(SourceTree(tree, sourceFile))
6666
case _ =>
6767
None

0 commit comments

Comments
 (0)