Skip to content

Commit

Permalink
implement time-wrap stamper
Browse files Browse the repository at this point in the history
This cached the underlying farmhash using the timestamp, intended to reuse content hash for libraries.
  • Loading branch information
eed3si9n committed Feb 13, 2020
1 parent e732a05 commit d79dcf1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
51 changes: 51 additions & 0 deletions internal/zinc-core/src/main/scala/sbt/internal/inc/Stamp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ object Stamper {
}
}
}

private[sbt] def timeWrap(
cache: collection.mutable.Map[VirtualFileRef, (Long, XStamp)],
converter: FileConverter,
getStamp: VirtualFileRef => XStamp
): VirtualFileRef => XStamp = { key: VirtualFileRef =>
val p = converter.toPath(key)
val ts = try {
IO.getModifiedTimeOrZero(p.toFile)
} catch {
case _: Throwable => 0L
}
synchronized {
cache.get(key) match {
case Some((ts1, value)) if ts == ts1 && ts > 0 => value
case _ =>
val value = getStamp(key)
cache.put(key, (ts, value))
value
}
}
}
}

object Stamps {
Expand All @@ -258,6 +280,12 @@ object Stamps {

def initial(underlying: ReadStamps): ReadStamps = new InitialStamps(underlying)

def timeWrapLibraryStamps(underlying: ReadStamps, converter: FileConverter): ReadStamps =
new TimeWrapLibraryStamps(underlying, converter)

def timeWrapLibraryStamps(converter: FileConverter): ReadStamps =
new TimeWrapLibraryStamps(uncachedStamps(converter), converter)

def uncachedStamps(converter: FileConverter): ReadStamps =
uncachedStamps(
Stamper.forHashInRootPaths(converter),
Expand Down Expand Up @@ -397,6 +425,29 @@ private class InitialStamps(
synchronized { libraries.getOrElseUpdate(lib, underlying.library(lib)) }
}

private class TimeWrapLibraryStamps(
underlying: ReadStamps,
converter: FileConverter
) extends ReadStamps {
import collection.mutable.{ HashMap, Map }

// cached stamps for files that do not change during compilation
private val libraries: Map[VirtualFileRef, (Long, XStamp)] = new HashMap

import scala.collection.JavaConverters.mapAsJavaMapConverter
override def getAllLibraryStamps: util.Map[VirtualFileRef, XStamp] =
mapAsJavaMapConverter(libraries map { case (k, (_, v2)) => (k, v2) }).asJava
override def getAllSourceStamps: util.Map[VirtualFileRef, XStamp] =
underlying.getAllSourceStamps
override def getAllProductStamps: util.Map[VirtualFileRef, XStamp] =
underlying.getAllProductStamps

override def product(prod: VirtualFileRef): XStamp = underlying.product(prod)
override def source(src: VirtualFile): XStamp = underlying.source(src)
val library0 = Stamper.timeWrap(libraries, converter, underlying.library(_))
override def library(lib: VirtualFileRef): XStamp = library0(lib)
}

/**
* Creates a raw stamper without caching.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ case class ProjectStructure(
((javaSourceDirectory.toFile ** "*.java").get.toList ++
(baseDirectory.toFile * "*.java").get.toList)
.map(_.toPath)
val stamper = Stamps.uncachedStamps(converter)
val stamper = Stamps.timeWrapLibraryStamps(converter)
val cacheFile = baseDirectory / "target" / "inc_compile.zip"
val fileStore = FileAnalysisStore.binary(cacheFile.toFile)
val cachedStore = AnalysisStore.cached(fileStore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IncrementalCompilerImpl extends IncrementalCompiler {
val scalac = compilers.scalac
val extraOptions = extra.toList.map(_.toScalaTuple)
val conv = converter.toOption.getOrElse(???)
val defaultStampReader = Stamps.uncachedStamps(conv)
val defaultStampReader = Stamps.timeWrapLibraryStamps(conv)
compileIncrementally(
scalac,
javacChosen,
Expand Down
2 changes: 1 addition & 1 deletion zinc/src/test/scala/sbt/inc/BaseCompilerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class BaseCompilerSpec extends BridgeProviderSpecification {
converter.toVirtualFile(x.toPath)
}) ++
classpath.toVector
val stamper = Stamps.uncachedStamps(converter)
val stamper = Stamps.timeWrapLibraryStamps(converter)
val in = compiler.inputs(
cp.toArray,
sources.toArray,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MultiProjectIncrementalSpec extends BridgeProviderSpecification {
val incOptions = IncOptions
.of()
.withApiDebug(true)
val stamper = Stamps.uncachedStamps(converter)
val stamper = Stamps.timeWrapLibraryStamps(converter)
val vs = sources map converter.toVirtualFile
val reporter = new ManagedLoggedReporter(maxErrors, log)
val setup = compiler.setup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object Depender2 {
val rootPaths = Array(tempDir.toPath, localBoot, javaHome)
val converter = new MappedFileConverter(rootPaths, true)
val mapper = VirtualFileUtil.sourcePositionMapper(converter)
val stamper = Stamps.uncachedStamps(converter)
val stamper = Stamps.timeWrapLibraryStamps(converter)
val cp: Vector[VirtualFile] =
(si.allJars.toVector.map(_.toPath) ++ Vector(targetDir, binarySampleFile))
.map(converter.toVirtualFile)
Expand Down

0 comments on commit d79dcf1

Please sign in to comment.