Skip to content

Commit 730dd48

Browse files
bishaboshaKordyjan
authored andcommitted
only use hashmap lookup to map to virtualfile
[Cherry-picked 5fde72c]
1 parent 1494b87 commit 730dd48

13 files changed

+39
-89
lines changed

compiler/src/dotty/tools/backend/jvm/CodeGen.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import scala.tools.asm
3030
import scala.tools.asm.tree._
3131
import tpd._
3232
import dotty.tools.io.AbstractFile
33+
import dotty.tools.dotc.util
3334
import dotty.tools.dotc.util.NoSourcePosition
3435

3536

@@ -106,7 +107,7 @@ class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)(
106107
}
107108

108109
// Creates a callback that will be evaluated in PostProcessor after creating a file
109-
private def onFileCreated(cls: ClassNode, claszSymbol: Symbol, sourceFile: interfaces.SourceFile): AbstractFile => Unit = clsFile => {
110+
private def onFileCreated(cls: ClassNode, claszSymbol: Symbol, sourceFile: util.SourceFile): AbstractFile => Unit = clsFile => {
110111
val (fullClassName, isLocal) = atPhase(sbtExtractDependenciesPhase) {
111112
(ExtractDependencies.classNameAsString(claszSymbol), claszSymbol.isLocal)
112113
}

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

Lines changed: 1 addition & 1 deletion
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(ctx.getEmptySource(file), unpickled, forceTrees)
118+
apply(SourceFile(file, Array.empty[Char]), 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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ object Contexts {
5353
private val (notNullInfosLoc, store8) = store7.newLocation[List[NotNullInfo]]()
5454
private val (importInfoLoc, store9) = store8.newLocation[ImportInfo | Null]()
5555
private val (typeAssignerLoc, store10) = store9.newLocation[TypeAssigner](TypeAssigner)
56-
private val (zincInitialFilesLoc, store11) = store10.newLocation[util.ReadOnlySet[AbstractFile] | Null]()
5756

58-
private val initialStore = store11
57+
private val initialStore = store10
5958

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

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

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

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 =
259-
base.sources.getOrElseUpdate(file, {
260-
val zincSources = zincInitialFiles
261-
val cachedFile =
262-
if zincSources != null then zincSources.lookup(file) match
263-
case null => file
264-
case cached: AbstractFile => cached
265-
else
266-
file
267-
mkSource(cachedFile)
268-
})
269-
270250
/** SourceFile with given path name, memoized */
271251
def getSource(path: TermName): SourceFile = getFile(path) match
272252
case NoAbstractFile => NoSource
@@ -695,7 +675,6 @@ object Contexts {
695675

696676
def setCompilerCallback(callback: CompilerCallback): this.type = updateStore(compilerCallbackLoc, callback)
697677
def setIncCallback(callback: IncrementalCallback): this.type = updateStore(incCallbackLoc, callback)
698-
def setZincInitialFiles(zincInitialFiles: util.ReadOnlySet[AbstractFile]): this.type = updateStore(zincInitialFilesLoc, zincInitialFiles)
699678
def setPrinterFn(printer: Context => Printer): this.type = updateStore(printerFnLoc, printer)
700679
def setSettings(settingsState: SettingsState): this.type = updateStore(settingsStateLoc, settingsState)
701680
def setRun(run: Run | Null): this.type = updateStore(runLoc, run)

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,16 @@ class ExtractDependencies extends Phase {
9191
} finally pw.close()
9292
}
9393

94-
if (ctx.incrementalEnabled) {
95-
ctx.withIncCallback: cb =>
96-
collector.usedNames.foreach {
97-
case (clazz, usedNames) =>
98-
val className = classNameAsString(clazz)
99-
usedNames.names.foreach {
100-
case (usedName, scopes) =>
101-
cb.usedName(className, usedName.toString, scopes)
102-
}
103-
}
104-
94+
ctx.withIncCallback: cb =>
95+
collector.usedNames.foreach {
96+
case (clazz, usedNames) =>
97+
val className = classNameAsString(clazz)
98+
usedNames.names.foreach {
99+
case (usedName, scopes) =>
100+
cb.usedName(className, usedName.toString, scopes)
101+
}
102+
}
105103
collector.dependencies.foreach(recordDependency)
106-
}
107104
}
108105

109106
/*

compiler/src/dotty/tools/dotc/sbt/interfaces/IncrementalCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.dotc.sbt.interfaces;
22

3-
import dotty.tools.dotc.interfaces.SourceFile;
3+
import dotty.tools.dotc.util.SourceFile;
44

55
import java.util.EnumSet;
66
import java.nio.file.Path;

sbt-bridge/src/dotty/tools/xsbt/AbstractZincFile.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.xsbt;
22

3-
import dotty.tools.dotc.interfaces.SourceFile;
3+
import dotty.tools.dotc.util.SourceFile;
44

55
public class BasicPathBasedFile extends PlaceholderVirtualFile implements xsbti.PathBasedFile {
66

@@ -9,7 +9,7 @@ public BasicPathBasedFile(SourceFile sourceFile) {
99
}
1010

1111
public java.nio.file.Path toPath() {
12-
return sourceFile.jfile().get().toPath();
12+
return sourceFile.file().jpath();
1313
}
1414

1515
}

sbt-bridge/src/dotty/tools/xsbt/CompilerBridgeDriver.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ public boolean sourcesRequired() {
5656
}
5757

5858
private static VirtualFile asVirtualFile(SourceFile sourceFile, DelegatingReporter reporter,
59-
Map<String, VirtualFile> placeholders) {
60-
if (sourceFile.file() instanceof AbstractZincFile) {
61-
return ((AbstractZincFile) sourceFile.file()).underlying();
59+
HashMap<AbstractFile, VirtualFile> lookup, Map<AbstractFile, VirtualFile> placeholders) {
60+
VirtualFile maybeCached = lookup.get(sourceFile.file());
61+
if (maybeCached != null) {
62+
return maybeCached;
6263
} else {
6364
return fallbackVirtualFile(reporter, sourceFile, placeholders);
6465
}
6566
}
6667

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

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

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

9797
for (int i = 0; i < sources.length; i++) {
9898
VirtualFile source = sortedSources[i];
9999
AbstractFile abstractFile = asDottyFile(source);
100100
sourcesBuffer.append(abstractFile);
101-
sourcesSet.put(abstractFile);
101+
lookup.put(abstractFile, source);
102102
}
103103

104-
HashMap<String, VirtualFile> placeholders = new HashMap<>();
104+
HashMap<AbstractFile, VirtualFile> placeholders = new HashMap<>();
105105

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

109-
IncrementalCallback incCallback = new IncrementalCallback(callback, sourceFile -> {
110-
if (sourceFile instanceof SourceFile) {
111-
return asVirtualFile(((SourceFile) sourceFile), reporter, placeholders);
112-
} else {
113-
return fallbackVirtualFile(reporter, sourceFile, placeholders);
114-
}
115-
});
109+
IncrementalCallback incCallback = new IncrementalCallback(callback, sourceFile ->
110+
asVirtualFile(sourceFile, reporter, lookup, placeholders)
111+
);
116112

117113
try {
118114
log.debug(this::infoOnCachedCompiler);
119115

120116
Contexts.Context initialCtx = initCtx()
121117
.fresh()
122118
.setReporter(reporter)
123-
.setZincInitialFiles(sourcesSet)
124119
.setIncCallback(incCallback);
125120

126121
Contexts.Context context = setup(args, initialCtx).map(t -> t._2).getOrElse(() -> initialCtx);

sbt-bridge/src/dotty/tools/xsbt/IncrementalCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.xsbt;
22

3-
import dotty.tools.dotc.interfaces.SourceFile;
3+
import dotty.tools.dotc.util.SourceFile;
44
import java.util.function.Function;
55

66
public final class IncrementalCallback implements dotty.tools.dotc.sbt.interfaces.IncrementalCallback {

sbt-bridge/src/dotty/tools/xsbt/OldIncrementalCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.xsbt;
22

3-
import dotty.tools.dotc.interfaces.SourceFile;
3+
import dotty.tools.dotc.util.SourceFile;
44
import java.util.function.Function;
55
import java.util.Optional;
66

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

1818
private static File asJavaFile(SourceFile sourceFile) {
19-
File jfileOrNull = sourceFile.jfile().orElse(null);
19+
File jfileOrNull = sourceFile.file().file();
2020
if (jfileOrNull != null) return jfileOrNull;
2121
throw new IllegalArgumentException("SourceFile " + sourceFile + " is not backed by a java.io.File");
2222
}

sbt-bridge/src/dotty/tools/xsbt/PlaceholderVirtualFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.xsbt;
22

3-
import dotty.tools.dotc.interfaces.SourceFile;
3+
import dotty.tools.dotc.util.SourceFile;
44
import java.io.InputStream;
55
import java.nio.charset.StandardCharsets;
66

sbt-bridge/src/dotty/tools/xsbt/ZincPlainFile.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@
77

88
import xsbti.PathBasedFile;
99

10-
public class ZincPlainFile extends dotty.tools.io.PlainFile implements AbstractZincFile {
11-
private final PathBasedFile _underlying;
12-
10+
public class ZincPlainFile extends dotty.tools.io.PlainFile {
1311
public ZincPlainFile(PathBasedFile underlying) {
1412
super(new dotty.tools.io.Path(underlying.toPath()));
15-
this._underlying = underlying;
16-
}
17-
18-
@Override
19-
public PathBasedFile underlying() {
20-
return _underlying;
2113
}
2214
}

sbt-bridge/src/dotty/tools/xsbt/ZincVirtualFile.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212
import java.io.InputStream;
1313
import java.io.OutputStream;
1414

15-
public class ZincVirtualFile extends dotty.tools.io.VirtualFile implements AbstractZincFile {
16-
private final VirtualFile _underlying;
15+
public class ZincVirtualFile extends dotty.tools.io.VirtualFile {
1716

1817
public ZincVirtualFile(VirtualFile underlying) throws IOException {
1918
super(underlying.name(), underlying.id());
20-
this._underlying = underlying;
2119

2220
// fill in the content
2321
try (OutputStream output = output()) {
@@ -32,9 +30,4 @@ public InputStream inputStream() {
3230
}
3331
}
3432
}
35-
36-
@Override
37-
public VirtualFile underlying() {
38-
return _underlying;
39-
}
4033
}

0 commit comments

Comments
 (0)