Skip to content

Memoize SourceFile creation #5642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
if (f.isDirectory) {
ctx.error(s"expected file, received directory '$fileName'")
NoSource
} else if (f.exists) {
val encoding = ctx.settings.encoding.value
new SourceFile(f, Codec(encoding))
} else {
}
else if (f.exists)
ctx.getSource(f)
else {
ctx.error(s"not found: $fileName")
NoSource
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1136,8 +1136,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
*/
def sourceFile(call: Tree)(implicit ctx: Context): SourceFile = {
val file = call.symbol.sourceFile
val encoding = ctx.settings.encoding.value
if (file != null && file.exists) new SourceFile(file, Codec(encoding)) else NoSource
if (file != null && file.exists) ctx.getSource(file) else NoSource
}

/** Desugar identifier into a select node. Return the tree itself if not possible */
Expand Down
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import config.Settings._
import config.Config
import reporting._
import reporting.diagnostic.Message
import io.AbstractFile
import scala.io.Codec
import collection.mutable
import printing._
import config.{JavaPlatform, SJSPlatform, Platform, ScalaSettings}
Expand Down Expand Up @@ -224,6 +226,10 @@ object Contexts {
implicitsCache
}

/** Sourcefile corresponding to given abstract file, memoized */
def getSource(file: AbstractFile, codec: => Codec = Codec(settings.encoding.value)) =
base.sources.getOrElseUpdate(file, new SourceFile(file, codec))

/** Those fields are used to cache phases created in withPhase.
* phasedCtx is first phase with altered phase ever requested.
* phasedCtxs is array that uses phaseId's as indexes,
Expand Down Expand Up @@ -624,6 +630,9 @@ object Contexts {

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

/** Sources that were loaded */
val sources: mutable.HashMap[AbstractFile, SourceFile] = new mutable.HashMap[AbstractFile, SourceFile]

// Types state
/** A table for hash consing unique types */
private[core] val uniques: util.HashSet[Type] = new util.HashSet[Type](Config.initialUniquesCapacity) {
Expand Down Expand Up @@ -696,6 +705,7 @@ object Contexts {
def reset(): Unit = {
for ((_, set) <- uniqueSets) set.clear()
errorTypeMsg.clear()
sources.clear()
}

// Test that access is single threaded
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/interactive/SourceTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object SourceTree {
case PackageDef(_, stats) =>
stats.flatMap(sourceTreeOfClass).headOption
case tree: tpd.TypeDef if tree.symbol == sym =>
val sourceFile = new SourceFile(sym.sourceFile, Codec.UTF8)
val sourceFile = ctx.getSource(sym.sourceFile, Codec.UTF8)
Some(SourceTree(tree, sourceFile))
case _ =>
None
Expand Down
3 changes: 1 addition & 2 deletions doc-tool/src/dotty/tools/dottydoc/util/syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ object syntax {

implicit class SymbolExtensions(val sym: Symbol) extends AnyVal {
def sourcePosition(pos: Position)(implicit ctx: Context): SourcePosition =
new SourceFile(sym.sourceFile, Codec(ctx.settings.encoding.value)) atPos pos

ctx.getSource(sym.sourceFile).atPos(pos)
}
}