Skip to content

Commit

Permalink
only use hashmap lookup to map to virtualfile
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Jul 17, 2023
1 parent 8f128fd commit 5fde72c
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 89 deletions.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import scala.tools.asm
import scala.tools.asm.tree._
import tpd._
import dotty.tools.io.AbstractFile
import dotty.tools.dotc.util
import dotty.tools.dotc.util.NoSourcePosition


Expand Down Expand Up @@ -106,7 +107,7 @@ class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)(
}

// Creates a callback that will be evaluated in PostProcessor after creating a file
private def onFileCreated(cls: ClassNode, claszSymbol: Symbol, sourceFile: interfaces.SourceFile): AbstractFile => Unit = clsFile => {
private def onFileCreated(cls: ClassNode, claszSymbol: Symbol, sourceFile: util.SourceFile): AbstractFile => Unit = clsFile => {
val (fullClassName, isLocal) = atPhase(sbtExtractDependenciesPhase) {
(ExtractDependencies.classNameAsString(claszSymbol), claszSymbol.isLocal)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ object CompilationUnit {
/** Make a compilation unit for top class `clsd` with the contents of the `unpickled` tree */
def apply(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit =
val file = clsd.symbol.associatedFile.nn
apply(ctx.getEmptySource(file), unpickled, forceTrees)
apply(SourceFile(file, Array.empty[Char]), unpickled, forceTrees)

/** Make a compilation unit, given picked bytes and unpickled tree */
def apply(source: SourceFile, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit = {
Expand Down
25 changes: 2 additions & 23 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ object Contexts {
private val (notNullInfosLoc, store8) = store7.newLocation[List[NotNullInfo]]()
private val (importInfoLoc, store9) = store8.newLocation[ImportInfo | Null]()
private val (typeAssignerLoc, store10) = store9.newLocation[TypeAssigner](TypeAssigner)
private val (zincInitialFilesLoc, store11) = store10.newLocation[util.ReadOnlySet[AbstractFile] | Null]()

private val initialStore = store11
private val initialStore = store10

/** The current context */
inline def ctx(using ctx: Context): Context = ctx
Expand Down Expand Up @@ -167,7 +166,6 @@ object Contexts {

/** The Zinc callback implementation if we are run from Zinc, null otherwise */
def incCallback: IncrementalCallback | Null = store(incCallbackLoc)
def zincInitialFiles: util.ReadOnlySet[AbstractFile] | Null = store(zincInitialFilesLoc)

/** Run `op` if there exists an incremental callback */
inline def withIncCallback(inline op: IncrementalCallback => Unit): Unit =
Expand Down Expand Up @@ -246,27 +244,9 @@ object Contexts {
/** Sourcefile corresponding to given abstract file, memoized */
def getSource(file: AbstractFile, codec: => Codec = Codec(settings.encoding.value)) = {
util.Stats.record("Context.getSource")
computeCachedSource(file)(SourceFile(_, codec))
base.sources.getOrElseUpdate(file, SourceFile(file, codec))
}

/** empty Sourcefile associated to given abstract file, memoized */
def getEmptySource(file: AbstractFile) = {
util.Stats.record("Context.getEmptySource")
computeCachedSource(file)(SourceFile(_, Array.empty[Char]))
}

private inline def computeCachedSource(file: AbstractFile)(inline mkSource: AbstractFile => SourceFile): SourceFile =
base.sources.getOrElseUpdate(file, {
val zincSources = zincInitialFiles
val cachedFile =
if zincSources != null then zincSources.lookup(file) match
case null => file
case cached: AbstractFile => cached
else
file
mkSource(cachedFile)
})

/** SourceFile with given path name, memoized */
def getSource(path: TermName): SourceFile = getFile(path) match
case NoAbstractFile => NoSource
Expand Down Expand Up @@ -695,7 +675,6 @@ object Contexts {

def setCompilerCallback(callback: CompilerCallback): this.type = updateStore(compilerCallbackLoc, callback)
def setIncCallback(callback: IncrementalCallback): this.type = updateStore(incCallbackLoc, callback)
def setZincInitialFiles(zincInitialFiles: util.ReadOnlySet[AbstractFile]): this.type = updateStore(zincInitialFilesLoc, zincInitialFiles)
def setPrinterFn(printer: Context => Printer): this.type = updateStore(printerFnLoc, printer)
def setSettings(settingsState: SettingsState): this.type = updateStore(settingsStateLoc, settingsState)
def setRun(run: Run | Null): this.type = updateStore(runLoc, run)
Expand Down
21 changes: 9 additions & 12 deletions compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,16 @@ class ExtractDependencies extends Phase {
} finally pw.close()
}

if (ctx.incrementalEnabled) {
ctx.withIncCallback: cb =>
collector.usedNames.foreach {
case (clazz, usedNames) =>
val className = classNameAsString(clazz)
usedNames.names.foreach {
case (usedName, scopes) =>
cb.usedName(className, usedName.toString, scopes)
}
}

ctx.withIncCallback: cb =>
collector.usedNames.foreach {
case (clazz, usedNames) =>
val className = classNameAsString(clazz)
usedNames.names.foreach {
case (usedName, scopes) =>
cb.usedName(className, usedName.toString, scopes)
}
}
collector.dependencies.foreach(recordDependency)
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dotty.tools.dotc.sbt.interfaces;

import dotty.tools.dotc.interfaces.SourceFile;
import dotty.tools.dotc.util.SourceFile;

import java.util.EnumSet;
import java.nio.file.Path;
Expand Down
7 changes: 0 additions & 7 deletions sbt-bridge/src/dotty/tools/xsbt/AbstractZincFile.java

This file was deleted.

4 changes: 2 additions & 2 deletions sbt-bridge/src/dotty/tools/xsbt/BasicPathBasedFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dotty.tools.xsbt;

import dotty.tools.dotc.interfaces.SourceFile;
import dotty.tools.dotc.util.SourceFile;

public class BasicPathBasedFile extends PlaceholderVirtualFile implements xsbti.PathBasedFile {

Expand All @@ -9,7 +9,7 @@ public BasicPathBasedFile(SourceFile sourceFile) {
}

public java.nio.file.Path toPath() {
return sourceFile.jfile().get().toPath();
return sourceFile.file().jpath();
}

}
37 changes: 16 additions & 21 deletions sbt-bridge/src/dotty/tools/xsbt/CompilerBridgeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ public boolean sourcesRequired() {
}

private static VirtualFile asVirtualFile(SourceFile sourceFile, DelegatingReporter reporter,
Map<String, VirtualFile> placeholders) {
if (sourceFile.file() instanceof AbstractZincFile) {
return ((AbstractZincFile) sourceFile.file()).underlying();
HashMap<AbstractFile, VirtualFile> lookup, Map<AbstractFile, VirtualFile> placeholders) {
VirtualFile maybeCached = lookup.get(sourceFile.file());
if (maybeCached != null) {
return maybeCached;
} else {
return fallbackVirtualFile(reporter, sourceFile, placeholders);
}
}

private static void reportMissingFile(DelegatingReporter reporter, dotty.tools.dotc.interfaces.SourceFile sourceFile) {
private static void reportMissingFile(DelegatingReporter reporter, SourceFile sourceFile) {
String underline = String.join("", Collections.nCopies(sourceFile.path().length(), "^"));
String message =
sourceFile.path() + ": Missing virtual file\n" +
Expand All @@ -74,12 +75,11 @@ private static void reportMissingFile(DelegatingReporter reporter, dotty.tools.d
reporter.reportBasicWarning(message);
}

private static VirtualFile fallbackVirtualFile(DelegatingReporter reporter,
dotty.tools.dotc.interfaces.SourceFile sourceFile,
Map<String, VirtualFile> placeholders) {
return placeholders.computeIfAbsent(sourceFile.path(), path -> {
private static VirtualFile fallbackVirtualFile(DelegatingReporter reporter, SourceFile sourceFile,
Map<AbstractFile, VirtualFile> placeholders) {
return placeholders.computeIfAbsent(sourceFile.file(), path -> {
reportMissingFile(reporter, sourceFile);
if (sourceFile.jfile().isPresent())
if (sourceFile.file().jpath() != null)
return new BasicPathBasedFile(sourceFile);
else
return new PlaceholderVirtualFile(sourceFile);
Expand All @@ -92,35 +92,30 @@ synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, L
Arrays.sort(sortedSources, (x0, x1) -> x0.id().compareTo(x1.id()));

ListBuffer<AbstractFile> sourcesBuffer = new ListBuffer<>();
dotty.tools.dotc.util.HashSet<AbstractFile> sourcesSet = new dotty.tools.dotc.util.HashSet<>(8, 2);
HashMap<AbstractFile, VirtualFile> lookup = new HashMap<>(sources.length, 0.25f);

for (int i = 0; i < sources.length; i++) {
VirtualFile source = sortedSources[i];
AbstractFile abstractFile = asDottyFile(source);
sourcesBuffer.append(abstractFile);
sourcesSet.put(abstractFile);
lookup.put(abstractFile, source);
}

HashMap<String, VirtualFile> placeholders = new HashMap<>();
HashMap<AbstractFile, VirtualFile> placeholders = new HashMap<>();

DelegatingReporter reporter = new DelegatingReporter(delegate, (self, sourceFile) ->
asVirtualFile(sourceFile, self, placeholders).id());
asVirtualFile(sourceFile, self, lookup, placeholders).id());

IncrementalCallback incCallback = new IncrementalCallback(callback, sourceFile -> {
if (sourceFile instanceof SourceFile) {
return asVirtualFile(((SourceFile) sourceFile), reporter, placeholders);
} else {
return fallbackVirtualFile(reporter, sourceFile, placeholders);
}
});
IncrementalCallback incCallback = new IncrementalCallback(callback, sourceFile ->
asVirtualFile(sourceFile, reporter, lookup, placeholders)
);

try {
log.debug(this::infoOnCachedCompiler);

Contexts.Context initialCtx = initCtx()
.fresh()
.setReporter(reporter)
.setZincInitialFiles(sourcesSet)
.setIncCallback(incCallback);

Contexts.Context context = setup(args, initialCtx).map(t -> t._2).getOrElse(() -> initialCtx);
Expand Down
2 changes: 1 addition & 1 deletion sbt-bridge/src/dotty/tools/xsbt/IncrementalCallback.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dotty.tools.xsbt;

import dotty.tools.dotc.interfaces.SourceFile;
import dotty.tools.dotc.util.SourceFile;
import java.util.function.Function;

public final class IncrementalCallback implements dotty.tools.dotc.sbt.interfaces.IncrementalCallback {
Expand Down
4 changes: 2 additions & 2 deletions sbt-bridge/src/dotty/tools/xsbt/OldIncrementalCallback.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dotty.tools.xsbt;

import dotty.tools.dotc.interfaces.SourceFile;
import dotty.tools.dotc.util.SourceFile;
import java.util.function.Function;
import java.util.Optional;

Expand All @@ -16,7 +16,7 @@ public OldIncrementalCallback(xsbti.AnalysisCallback delegate) {
}

private static File asJavaFile(SourceFile sourceFile) {
File jfileOrNull = sourceFile.jfile().orElse(null);
File jfileOrNull = sourceFile.file().file();
if (jfileOrNull != null) return jfileOrNull;
throw new IllegalArgumentException("SourceFile " + sourceFile + " is not backed by a java.io.File");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dotty.tools.xsbt;

import dotty.tools.dotc.interfaces.SourceFile;
import dotty.tools.dotc.util.SourceFile;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

Expand Down
10 changes: 1 addition & 9 deletions sbt-bridge/src/dotty/tools/xsbt/ZincPlainFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@

import xsbti.PathBasedFile;

public class ZincPlainFile extends dotty.tools.io.PlainFile implements AbstractZincFile {
private final PathBasedFile _underlying;

public class ZincPlainFile extends dotty.tools.io.PlainFile {
public ZincPlainFile(PathBasedFile underlying) {
super(new dotty.tools.io.Path(underlying.toPath()));
this._underlying = underlying;
}

@Override
public PathBasedFile underlying() {
return _underlying;
}
}
9 changes: 1 addition & 8 deletions sbt-bridge/src/dotty/tools/xsbt/ZincVirtualFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
import java.io.InputStream;
import java.io.OutputStream;

public class ZincVirtualFile extends dotty.tools.io.VirtualFile implements AbstractZincFile {
private final VirtualFile _underlying;
public class ZincVirtualFile extends dotty.tools.io.VirtualFile {

public ZincVirtualFile(VirtualFile underlying) throws IOException {
super(underlying.name(), underlying.id());
this._underlying = underlying;

// fill in the content
try (OutputStream output = output()) {
Expand All @@ -32,9 +30,4 @@ public InputStream inputStream() {
}
}
}

@Override
public VirtualFile underlying() {
return _underlying;
}
}

0 comments on commit 5fde72c

Please sign in to comment.