-
Notifications
You must be signed in to change notification settings - Fork 18
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
Cache Scalafix instances globally #206
Changes from 2 commits
7d57735
f959deb
c8798c6
617f307
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.goyeau.mill.scalafix | ||
|
||
import com.goyeau.mill.scalafix.CoursierUtils | ||
import coursier.core.Repository | ||
import scalafix.interfaces.Scalafix | ||
|
||
import java.util.concurrent.ConcurrentHashMap | ||
import scala.jdk.CollectionConverters._ | ||
import scala.ref.SoftReference | ||
|
||
private[scalafix] object ScalafixCache { | ||
|
||
private val cache = new ConcurrentHashMap[(String, Seq[Repository]), SoftReference[Scalafix]] | ||
|
||
def getOrElseCreate(scalaVersion: String, repositories: Seq[Repository]) = | ||
cache.get((scalaVersion, repositories)) match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a blocking method like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a great idea! |
||
case SoftReference(value) => value | ||
case _ => | ||
val newResult = | ||
Scalafix.fetchAndClassloadInstance(scalaVersion, repositories.map(CoursierUtils.toApiRepository).asJava) | ||
cache.put((scalaVersion, repositories), SoftReference(newResult)) | ||
newResult | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ import mill.api.{Logger, PathRef, Result} | |
import mill.scalalib.{Dep, ScalaModule} | ||
import mill.define.Command | ||
|
||
import scalafix.interfaces.Scalafix | ||
import scalafix.interfaces.ScalafixError.* | ||
|
||
import scala.compat.java8.OptionConverters.* | ||
|
@@ -77,8 +76,8 @@ object ScalafixModule { | |
wd: os.Path | ||
): Result[Unit] = | ||
if (sources.nonEmpty) { | ||
val scalafix = Scalafix | ||
.fetchAndClassloadInstance(scalaVersion, repositories.map(CoursierUtils.toApiRepository).asJava) | ||
val scalafix = ScalafixCache | ||
.getOrElseCreate(scalaVersion, repositories) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential tweak even further: as I was mentioning no later than yesterday in the docs, only a major.minor version is required, so FWIW you could strip the patch version to maximize cache hit ratio (the instance will be the same for 3.3.3 and 3.3.4). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That assumption is true now but it might become false in future versions of scalafix. Since it's very rare to use different patch versions in the same build, I wouldn't bother trying to be clever here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally fair. FTR, that assumption is based on Scala current semver semantics ensuring backward and forward source compatibility for patch releases, but it could indeed change in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, we could use the full scalafix classpath (as |
||
.newArguments() | ||
.withParsedArguments(args.asJava) | ||
.withWorkingDirectory(wd.toNIO) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
repositories
is provided, should we have it in the cache key as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do, don't we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops 🙈 my bad!