Skip to content

WIP: Re-use previous analysis if dependency graph is unchanged #4927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ final class ModuleInitializer private (

def withModuleID(moduleID: String): ModuleInitializer =
new ModuleInitializer(initializer, moduleID)

override def equals(that: Any): Boolean = that match {
case that: ModuleInitializer =>
this.initializer == that.initializer && this.moduleID == that.moduleID

case _ => false
}
}

/** Factory for [[ModuleInitializer]]s. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ object Analysis {
* versions, possibly causing `LinkageError`s if you extend it.
*/
trait ClassInfo {
private[analyzer] val data: Infos.ClassInfo

def className: ClassName
def kind: ClassKind
def superClass: Option[ClassInfo]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,96 @@ final class Analyzer(config: CommonPhaseConfig, initial: Boolean,
)
}

private var previousRunState: Analyzer.PreviousRunState = _

def computeReachability(moduleInitializers: Seq[ModuleInitializer],
symbolRequirements: SymbolRequirement, logger: Logger)(implicit ec: ExecutionContext): Future[Analysis] = {

infoLoader.update(logger)

val run = new AnalyzerRun(config, initial, infoLoader)(
adjustExecutionContextForParallelism(ec, config.parallel))
val adjustedEc = adjustExecutionContextForParallelism(ec, config.parallel)

val fullRun = new AnalyzerRun(config, initial, infoLoader)

// Start a full run in any case for speed.
val fullRunResultFuture = fullRun.computeReachability(moduleInitializers, symbolRequirements)

val analysisFuture = {
if (previousRunState != null) {
analysisChanged(moduleInitializers, symbolRequirements).flatMap { changed =>
if (changed) {
fullRunResultFuture
} else {
logger.debug("Analyzer: Re-using previous analysis")
//fullRun.abort()
Future.successful(previousRunState.analysis)
}
}
} else {
fullRunResultFuture
}
}

analysisFuture
.andThen {
case Success(analysis) => infoLoader.cleanAfterRun(analysis.classInfos.keySet)
case Failure(_) => infoLoader.cleanAfterRun(Set.empty)
}
.andThen {
case Success(analysis) if analysis.errors.isEmpty =>
previousRunState = new Analyzer.PreviousRunState(analysis, moduleInitializers, symbolRequirements)

case _ =>
previousRunState = null
}
.map { analysis =>
if (failOnError && analysis.errors.nonEmpty)
reportErrors(analysis.errors, logger)

analysis
}
}

private def analysisChanged(moduleInitializers: Seq[ModuleInitializer],
symbolRequirements: SymbolRequirement)(implicit ec: ExecutionContext): Future[Boolean] = {

val changedFuture = {
if (symbolRequirements != previousRunState.symbolRequirements ||
moduleInitializers != previousRunState.moduleInitializers ||
/* Check that all classes with entry points are known.
* Otherwise there is a new entry point and the dependency graph changed.
*/
infoLoader.classesWithEntryPoints().exists(clazz => !previousRunState.analysis.classInfos.contains(clazz))
) {
Future.successful(true)
} else {
val changedFutures = for {
(className, prevInfo) <- previousRunState.analysis.classInfos
} yield Future.unit.flatMap { _ =>
infoLoader.loadInfo(className) match {
case None => Future.successful(false)
case Some(fut) => fut.map(_ != prevInfo.data)
}
}

run
.computeReachability(moduleInitializers, symbolRequirements)
.map { _ =>
if (failOnError && run.errors.nonEmpty)
reportErrors(run.errors, logger)
Future.foldLeft(changedFutures.toList)(false)(_ || _)
}
}

run
for {
changed <- changedFuture
} yield {
// Internal hook for benchmarking.
val simulateChanged = {
val propName = "org.scalajs.linker.analyzer.internal.simulateChangedInfo"
System.getProperty(propName, "false").toBoolean
}
.andThen { case _ => infoLoader.cleanAfterRun() }

changed || simulateChanged
}
}

private def reportErrors(errors: List[Error], logger: Logger): Unit = {
private def reportErrors(errors: Seq[Error], logger: Logger): Unit = {
require(errors.nonEmpty)

val maxDisplayErrors = {
Expand Down Expand Up @@ -101,6 +171,14 @@ final class Analyzer(config: CommonPhaseConfig, initial: Boolean,
}
}

private object Analyzer {
private class PreviousRunState(
val analysis: Analysis,
val moduleInitializers: Seq[ModuleInitializer],
val symbolRequirements: SymbolRequirement,
)
}

private class AnalyzerRun(config: CommonPhaseConfig, initial: Boolean,
infoLoader: InfoLoader)(implicit ec: ExecutionContext) extends Analysis {
import AnalyzerRun._
Expand Down Expand Up @@ -128,7 +206,7 @@ private class AnalyzerRun(config: CommonPhaseConfig, initial: Boolean,
def topLevelExportInfos: scala.collection.Map[(ModuleID, String), Analysis.TopLevelExportInfo] = _topLevelExportInfos

def computeReachability(moduleInitializers: Seq[ModuleInitializer],
symbolRequirements: SymbolRequirement): Future[Unit] = {
symbolRequirements: SymbolRequirement): Future[Analysis] = {
loadObjectClass(() => loadEverything(moduleInitializers, symbolRequirements))

workTracker
Expand Down Expand Up @@ -185,7 +263,7 @@ private class AnalyzerRun(config: CommonPhaseConfig, initial: Boolean,
reachInitializers(moduleInitializers)
}

private def postLoad(moduleInitializers: Seq[ModuleInitializer]): Unit = {
private def postLoad(moduleInitializers: Seq[ModuleInitializer]): Analysis = {
_classInfos = classLoader.loadedInfos()

if (isNoModule) {
Expand All @@ -201,6 +279,8 @@ private class AnalyzerRun(config: CommonPhaseConfig, initial: Boolean,

// Reach additional data, based on reflection methods used
reachDataThroughReflection()

this
}

private def reachSymbolRequirement(requirement: SymbolRequirement): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ private[analyzer] final class InfoLoader(irLoader: IRLoader, irCheckMode: InfoLo
}
}

def cleanAfterRun(): Unit = {
def cleanAfterRun(retain: scala.collection.Set[ClassName]): Unit = {
logger = null
cache.filterInPlace((_, infoCache) => infoCache.cleanAfterRun())

cache.filterInPlace((className, _) => retain.contains(className))
cache.values.foreach(_.cleanAfterRun())
}
}

Expand Down Expand Up @@ -152,16 +154,11 @@ private[analyzer] object InfoLoader {
}

/** Returns true if the cache has been used and should be kept. */
def cleanAfterRun(): Boolean = synchronized {
val result = cacheUsed
def cleanAfterRun(): Unit = synchronized {
cacheUsed = false
if (result) {
// No point in cleaning the inner caches if the whole class disappears
methodsInfoCaches.cleanAfterRun()
jsConstructorInfoCache.cleanAfterRun()
exportedMembersInfoCaches.cleanAfterRun()
}
result
methodsInfoCaches.cleanAfterRun()
jsConstructorInfoCache.cleanAfterRun()
exportedMembersInfoCaches.cleanAfterRun()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object Infos {
final case class NamespacedMethodName(
namespace: MemberNamespace, methodName: MethodName)

final class ClassInfo private[Infos] (
final case class ClassInfo private[Infos] (
val className: ClassName,
val kind: ClassKind,
val superClass: Option[ClassName], // always None for interfaces
Expand All @@ -67,7 +67,7 @@ object Infos {
override def toString(): String = className.nameString
}

final class MethodInfo private (
final case class MethodInfo private (
val methodName: MethodName,
val namespace: MemberNamespace,
val isAbstract: Boolean,
Expand All @@ -86,13 +86,13 @@ object Infos {
}
}

final class TopLevelExportInfo private[Infos] (
final case class TopLevelExportInfo private[Infos] (
val reachability: ReachabilityInfo,
val moduleID: ModuleID,
val exportName: String
)

final class ReachabilityInfo private[Infos] (
final case class ReachabilityInfo private[Infos] (
val byClass: List[ReachabilityInfoInClass],
val globalFlags: ReachabilityInfo.Flags
)
Expand All @@ -107,7 +107,7 @@ object Infos {
}

/** Things from a given class that are reached by one method. */
final class ReachabilityInfoInClass private[Infos] (
final case class ReachabilityInfoInClass private[Infos] (
val className: ClassName,
val fieldsRead: List[FieldName],
val fieldsWritten: List[FieldName],
Expand Down
6 changes: 6 additions & 0 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ object ExposedValues extends AutoPlugin {
*
* ggplot(d, aes(x = op, color = variant, y = t_ns)) + geom_boxplot()
* ggsave("plot.png", width = 9, height = 5)
*
* # Medians per variant and op
*
* d %>%
* group_by(variant, op) %>%
* summarise(t_ns = median(t_ns))
* }}}
*/
val loggerTimingVariant = settingKey[String]("Variant identifier for logger timings.")
Expand Down