@@ -5,38 +5,33 @@ package scala.tools.nsc.classpath
5
5
6
6
import java .io .File
7
7
import java .net .URL
8
+ import java .nio .file .Files
9
+ import java .nio .file .attribute .{BasicFileAttributes , FileTime }
8
10
9
11
import scala .annotation .tailrec
10
- import scala .reflect .io .{ AbstractFile , FileZipArchive , ManifestResources }
12
+ import scala .reflect .io .{AbstractFile , FileZipArchive , ManifestResources }
11
13
import scala .tools .nsc .util .{ClassPath , ClassRepresentation }
12
14
import scala .tools .nsc .Settings
13
15
import FileUtils ._
14
16
15
17
/**
16
18
* A trait providing an optional cache for classpath entries obtained from zip and jar files.
17
- * It's possible to create such a cache assuming that entries in such files won't change (at
18
- * least will be the same each time we'll load classpath during the lifetime of JVM process)
19
- * - unlike class and source files in directories, which can be modified and recompiled.
20
19
* It allows us to e.g. reduce significantly memory used by PresentationCompilers in Scala IDE
21
20
* when there are a lot of projects having a lot of common dependencies.
22
21
*/
23
22
sealed trait ZipAndJarFileLookupFactory {
24
- private val cache = collection.mutable.Map .empty[AbstractFile , ClassPath ]
23
+ private case class Stamp (lastModified : FileTime , fileKey : Object )
24
+ private val cache = new FileBasedCache [ClassPath ]
25
25
26
26
def create (zipFile : AbstractFile , settings : Settings ): ClassPath = {
27
- if (settings.YdisableFlatCpCaching ) createForZipFile(zipFile)
27
+ if (settings.YdisableFlatCpCaching || zipFile.file == null ) createForZipFile(zipFile)
28
28
else createUsingCache(zipFile, settings)
29
29
}
30
30
31
31
protected def createForZipFile (zipFile : AbstractFile ): ClassPath
32
32
33
- private def createUsingCache (zipFile : AbstractFile , settings : Settings ): ClassPath = cache.synchronized {
34
- def newClassPathInstance = {
35
- if (settings.verbose || settings.Ylogcp )
36
- println(s " $zipFile is not yet in the classpath cache " )
37
- createForZipFile(zipFile)
38
- }
39
- cache.getOrElseUpdate(zipFile, newClassPathInstance)
33
+ private def createUsingCache (zipFile : AbstractFile , settings : Settings ): ClassPath = {
34
+ cache.getOrCreate(zipFile.file.toPath, () => createForZipFile(zipFile))
40
35
}
41
36
}
42
37
@@ -181,3 +176,27 @@ object ZipAndJarSourcePathFactory extends ZipAndJarFileLookupFactory {
181
176
182
177
override protected def createForZipFile (zipFile : AbstractFile ): ClassPath = ZipArchiveSourcePath (zipFile.file)
183
178
}
179
+
180
+ final class FileBasedCache [T ] {
181
+ private case class Stamp (lastModified : FileTime , fileKey : Object )
182
+ private val cache = collection.mutable.Map .empty[java.nio.file.Path , (Stamp , T )]
183
+
184
+ def getOrCreate (path : java.nio.file.Path , create : () => T ): T = cache.synchronized {
185
+ val lastModified = Files .getLastModifiedTime(path)
186
+ val attrs = Files .readAttributes(path, classOf [BasicFileAttributes ])
187
+ val stamp = Stamp (lastModified, attrs.fileKey())
188
+ cache.get(path) match {
189
+ case Some ((cachedStamp, cached)) if cachedStamp == stamp => cached
190
+ case _ =>
191
+ val value = create()
192
+ cache.put(path, (stamp, value))
193
+ value
194
+ }
195
+ }
196
+
197
+ def clear (): Unit = cache.synchronized {
198
+ // TODO support closing
199
+ // cache.valuesIterator.foreach(_.close())
200
+ cache.clear()
201
+ }
202
+ }
0 commit comments