Skip to content

Commit 395d1f7

Browse files
committed
Add a default no-op GlobalCache
1 parent 0bce3a8 commit 395d1f7

File tree

5 files changed

+49
-35
lines changed

5 files changed

+49
-35
lines changed

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

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,58 @@ import java.nio.file.attribute.BasicFileAttributes
55
import java.nio.file.attribute.FileTime
66

77
import dotty.tools.dotc.core.Contexts.Context
8+
import dotty.tools.dotc.core.Decorators.em
9+
import dotty.tools.dotc.report
810
import dotty.tools.io.{AbstractFile, ClassPath}
911

12+
trait GlobalCache:
13+
def getOrCreateClassPath(key: AbstractFile, createValue: => ClassPath)(using Context): ClassPath
14+
1015
/** Global cache that can be shared across [[Driver]] instances.
1116
*
1217
* This class is thread-safe.
1318
*/
14-
final class GlobalCache():
15-
import GlobalCache.FileBasedCache
16-
19+
private final class GlobalCacheImpl() extends GlobalCache:
1720
private val classPathCache = FileBasedCache[ClassPath]()
1821

1922
def getOrCreateClassPath(key: AbstractFile, createValue: => ClassPath)(using Context): ClassPath =
2023
classPathCache.getOrCreate(key.file.nn.toPath, () => createValue)
2124

2225
object GlobalCache:
26+
def apply(): GlobalCache =
27+
GlobalCacheImpl()
2328

24-
/** A cache for values associated with files on disk, that invalidates
25-
* the cached value when the file is modified.
26-
*
27-
* See https://github.com/scala/bug/issues/10295 for some context on the
28-
* invalidation strategy.
29-
*
30-
* Moved from [[ZipAndJarFileLookupFactory]] in December 2025.
31-
*
32-
* @author @allanrenucci
33-
*/
34-
private class FileBasedCache[T]:
35-
private case class Stamp(lastModified: FileTime, fileKey: Object)
36-
private val cache = collection.mutable.Map.empty[java.nio.file.Path, (Stamp, T)]
37-
38-
def getOrCreate(path: java.nio.file.Path, create: () => T): T =
39-
cache.synchronized:
40-
val attrs = Files.readAttributes(path, classOf[BasicFileAttributes])
41-
val lastModified = attrs.lastModifiedTime()
42-
// null on some platforms, but that's okay, we just use the last
43-
// modified timestamp as our stamp in that case
44-
val fileKey = attrs.fileKey()
45-
val stamp = Stamp(lastModified, fileKey)
46-
cache.get(path) match
47-
case Some((cachedStamp, cached)) if cachedStamp == stamp =>
48-
cached
49-
case _ =>
50-
val value = create()
51-
cache.put(path, (stamp, value))
52-
value
29+
object NoGlobalCache extends GlobalCache:
30+
def getOrCreateClassPath(key: AbstractFile, createValue: => ClassPath)(using Context): ClassPath =
31+
report.configurationWarning(em"Not GlobalCache set")
32+
createValue
33+
34+
/** A cache for values associated with files on disk, that invalidates
35+
* the cached value when the file is modified.
36+
*
37+
* See https://github.com/scala/bug/issues/10295 for some context on the
38+
* invalidation strategy.
39+
*
40+
* Moved from [[ZipAndJarFileLookupFactory]] in December 2025.
41+
*
42+
* @author @allanrenucci
43+
*/
44+
private class FileBasedCache[T]:
45+
private case class Stamp(lastModified: FileTime, fileKey: Object)
46+
private val cache = collection.mutable.Map.empty[java.nio.file.Path, (Stamp, T)]
47+
48+
def getOrCreate(path: java.nio.file.Path, create: () => T): T =
49+
cache.synchronized:
50+
val attrs = Files.readAttributes(path, classOf[BasicFileAttributes])
51+
val lastModified = attrs.lastModifiedTime()
52+
// null on some platforms, but that's okay, we just use the last
53+
// modified timestamp as our stamp in that case
54+
val fileKey = attrs.fileKey()
55+
val stamp = Stamp(lastModified, fileKey)
56+
cache.get(path) match
57+
case Some((cachedStamp, cached)) if cachedStamp == stamp =>
58+
cached
59+
case _ =>
60+
val value = create()
61+
cache.put(path, (stamp, value))
62+
value

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ object Contexts {
779779
.updated(notNullInfosLoc, Nil)
780780
.updated(compilationUnitLoc, NoCompilationUnit)
781781
.updated(profilerLoc, Profiler.NoOp)
782+
.updated(globalCacheLoc, GlobalCache.NoGlobalCache)
782783
c._searchHistory = new SearchRoot
783784
c._gadtState = GadtState(GadtConstraint.empty)
784785
c

compiler/test/dotty/tools/DottyTest.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ import dotc.core.Symbols._
1212
import Types._, Symbols._, Decorators._
1313
import dotc.core.Decorators._
1414
import dotc.ast.tpd
15-
import dotc.Compiler
15+
import dotc.{Compiler, GlobalCache}
1616

1717
import dotc.core.Phases.Phase
1818

1919
trait DottyTest extends ContextEscapeDetection {
2020

2121
dotc.parsing.Scanners // initialize keywords
2222

23+
private val globalCache: GlobalCache = GlobalCache()
24+
2325
implicit var ctx: Context = initialCtx
2426

2527
protected def initialCtx: FreshContext = {
@@ -42,6 +44,7 @@ trait DottyTest extends ContextEscapeDetection {
4244
fc.setSetting(fc.settings.classpath, TestConfiguration.basicClasspath)
4345
fc.setSetting(fc.settings.language, List("experimental.erasedDefinitions").asInstanceOf)
4446
fc.setProperty(ContextDoc, new ContextDocstrings)
47+
fc.setGlobalCache(globalCache)
4548
}
4649

4750
protected def defaultCompiler: Compiler = new Compiler()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import xsbti.compile.Output;
1818

1919
public final class CompilerBridge implements CompilerInterface2 {
20-
private final GlobalCache globalCache = new GlobalCache();
20+
private final GlobalCache globalCache = GlobalCache.apply();
2121

2222
@Override
2323
public void run(VirtualFile[] sources, DependencyChanges changes, String[] options, Output output,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public CompilerBridgeDriver(String[] scalacOptions, Output output, GlobalCache g
5454
}
5555

5656
public CompilerBridgeDriver(String[] scalacOptions, Output output) {
57-
this(scalacOptions, output, new GlobalCache());
57+
this(scalacOptions, output, GlobalCache.apply());
5858
}
5959

6060
private static final String StopInfoError =

0 commit comments

Comments
 (0)