|
| 1 | +package dotty.tools.dotc.core |
| 2 | + |
| 3 | +import java.nio.file.Files |
| 4 | +import java.nio.file.Path |
| 5 | +import java.nio.file.attribute.{BasicFileAttributes, FileTime} |
| 6 | +import java.util.concurrent.ConcurrentHashMap |
| 7 | +import java.util.concurrent.atomic.LongAdder |
| 8 | + |
| 9 | +import scala.collection.mutable.Map |
| 10 | + |
| 11 | +import dotty.tools.io.AbstractFile |
| 12 | + |
| 13 | +object Caches: |
| 14 | + |
| 15 | + /** Cache interface |
| 16 | + * |
| 17 | + * @tparam K |
| 18 | + * the type of keys |
| 19 | + * @tparam S |
| 20 | + * the type of stamps |
| 21 | + * @tparam V |
| 22 | + * the type of cached values |
| 23 | + */ |
| 24 | + trait Cache[K, V]: |
| 25 | + def apply(key: K, value: => V): V |
| 26 | + def stats(): CacheStats |
| 27 | + override def toString: String = |
| 28 | + s"${this.getClass.getSimpleName}(stats() = ${stats()})" |
| 29 | + |
| 30 | + /** Statistics about a cache */ |
| 31 | + final class CacheStats(total: Long, misses: Long, uncached: Long): |
| 32 | + val hits: Long = total - misses - uncached |
| 33 | + |
| 34 | + override def toString: String = |
| 35 | + s"(total = $total, hits = $hits, misses = $misses, uncached = $uncached)" |
| 36 | + |
| 37 | + /** A no-op cache implementation that does not cache anything. */ |
| 38 | + final class NoopCache[K, V] extends Cache[K, V]: |
| 39 | + private var total = 0L |
| 40 | + |
| 41 | + def apply(key: K, value: => V): V = |
| 42 | + total += 1 |
| 43 | + value |
| 44 | + |
| 45 | + def stats(): CacheStats = |
| 46 | + CacheStats(total, misses = 0, uncached = total) |
| 47 | + |
| 48 | + /** A thread-unsafe cache implementation based on a mutable [[Map]]. |
| 49 | + * |
| 50 | + * Entries are not evicted. |
| 51 | + */ |
| 52 | + final class MapCache[K, S, V](getStamp: K => Option[S]) extends Cache[K, V]: |
| 53 | + private val map = Map.empty[K, (S, V)] |
| 54 | + private var total = 0L |
| 55 | + private var misses = 0L |
| 56 | + private var uncached = 0L |
| 57 | + |
| 58 | + def apply(key: K, value: => V): V = |
| 59 | + total += 1 |
| 60 | + getStamp(key) match |
| 61 | + case None => |
| 62 | + uncached += 1 |
| 63 | + value |
| 64 | + case Some(stamp) => |
| 65 | + map.get(key) match |
| 66 | + case Some((cachedStamp, cachedValue)) if cachedStamp == stamp => |
| 67 | + cachedValue |
| 68 | + case _ => |
| 69 | + misses += 1 |
| 70 | + val v = value |
| 71 | + map.put(key, (stamp, v)) |
| 72 | + v |
| 73 | + |
| 74 | + def stats(): CacheStats = |
| 75 | + CacheStats(total, misses, uncached) |
| 76 | + |
| 77 | + /** A thread-safe cache implementation based on a Java [[ConcurrentHashMap]]. |
| 78 | + * |
| 79 | + * Entries are not evicted. |
| 80 | + */ |
| 81 | + final class SynchronizedMapCache[K, S, V](getStamp: K => Option[S]) extends Cache[K, V]: |
| 82 | + private val map = ConcurrentHashMap[K, (S, V)]() |
| 83 | + private val total = LongAdder() |
| 84 | + private val misses = LongAdder() |
| 85 | + private val uncached = LongAdder() |
| 86 | + |
| 87 | + def apply(key: K, value: => V): V = |
| 88 | + total.increment() |
| 89 | + getStamp(key) match |
| 90 | + case None => |
| 91 | + uncached.increment() |
| 92 | + value |
| 93 | + case Some(stamp) => |
| 94 | + map.compute( |
| 95 | + key, |
| 96 | + (_, cached) => |
| 97 | + if cached != null && cached._1 == stamp then |
| 98 | + cached |
| 99 | + else |
| 100 | + misses.increment() |
| 101 | + (stamp, value) |
| 102 | + )._2 |
| 103 | + |
| 104 | + def stats(): CacheStats = |
| 105 | + CacheStats(total.longValue(), misses.longValue(), uncached.longValue()) |
| 106 | + |
| 107 | + /** A cache where keys are [[AbstractFile]]s. |
| 108 | + * |
| 109 | + * The cache uses file modification time and file key (inode) as stamp to |
| 110 | + * invalidate cached entries when the underlying file has changed. |
| 111 | + * |
| 112 | + * For files with an underlying source (e.g. files inside a zip/jar), the |
| 113 | + * stamp is obtained from the underlying source file. |
| 114 | + * |
| 115 | + * If the [[AbstractFile]] does not correspond to a physical file on disk, no |
| 116 | + * caching is performed. |
| 117 | + * |
| 118 | + * See https://github.com/scala/bug/issues/10295 for discussion about the |
| 119 | + * invalidation strategy. |
| 120 | + */ |
| 121 | + final class FileBasedCache[V]() extends Cache[AbstractFile, V]: |
| 122 | + private case class FileStamp(lastModified: FileTime, fileKey: Object) |
| 123 | + |
| 124 | + private def getFileStamp(abstractFile: AbstractFile): Option[FileStamp] = |
| 125 | + abstractFile.underlyingSource match |
| 126 | + case Some(underlyingSource) if underlyingSource ne abstractFile => |
| 127 | + getFileStamp(underlyingSource) |
| 128 | + case _ => |
| 129 | + val javaFile = abstractFile.file |
| 130 | + if javaFile == null then |
| 131 | + None |
| 132 | + else |
| 133 | + val attrs = Files.readAttributes(javaFile.toPath, classOf[BasicFileAttributes]) |
| 134 | + val lastModified = attrs.lastModifiedTime() |
| 135 | + // This can be `null` on some platforms, but that's okay, we just use |
| 136 | + // the last modified timestamp as our stamp in that case. |
| 137 | + val fileKey = attrs.fileKey() |
| 138 | + Some(FileStamp(lastModified, fileKey)) |
| 139 | + |
| 140 | + private val underlying = SynchronizedMapCache[AbstractFile, FileStamp, V](getFileStamp) |
| 141 | + |
| 142 | + def apply(key: AbstractFile, value: => V): V = |
| 143 | + underlying(key, value) |
| 144 | + |
| 145 | + def stats(): CacheStats = |
| 146 | + underlying.stats() |
| 147 | + |
| 148 | + override def toString: String = |
| 149 | + s"FileBasedCache(${underlying.toString})" |
| 150 | + |
| 151 | + |
0 commit comments