Skip to content
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

Opt-in caching for scalafix invocations #102

Merged
merged 13 commits into from
May 19, 2020
Merged

Conversation

bjaglin
Copy link
Collaborator

@bjaglin bjaglin commented May 9, 2020

See https://gitter.im/scalacenter/scalafix?at=5ea5f83aa634f42f7a0524a8.

It took much more effort than expected, mostly due to the compat issues between sbt versions. I was about to give up and say that caching would be effective in sbt 1.x only, but I think I found a decent way to keep the codebase as common as possible.

As classpath-only changes (i.e. no source file updated) do not invalidate the cache and the implementation is probably rough on the edges anyway (false positives?), I was thinking about leaving this opt-in & advertise it as "experimental" for now.

There will most likely be upcoming Scalafix features that trigger false positive cache hits (any new flag/syntax that references a local/remote resource won't be stamped correctly), which can be a problem if the sbt plugin version evolves independently of the Scalafix version. I guess the right thing to do would be to implement caching in Scalafix itself in the long run.

@bjaglin bjaglin force-pushed the caching branch 13 times, most recently from 2af116f to 6177b8b Compare May 11, 2020 00:57
@bjaglin bjaglin marked this pull request as ready for review May 11, 2020 01:13
Comment on lines +27 to +36
override final type Internal = Array[Byte]
override final def convert(keys: Seq[Arg.CacheKey]): Array[Byte] = {
val baos = new ByteArrayOutputStream()
output.withValue(new JavaOutput(baos)) {
keys.foreach(stamp)
}
Hash.apply(baos.toByteArray)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this effectively replicates the 1.x behavior (although with a binary representation and a different hashing algorithm)

object StampingImpossible extends RuntimeException with NoStackTrace

implicit val stamper = new CacheKeysStamper {
override protected def stamp: Arg.CacheKey => Unit = {
Copy link
Collaborator Author

@bjaglin bjaglin May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall stamp for cacheKeyArgs is the concatenation of each arg stamp, taken in the order they were set, which depends on the flow of the scalafix InputTask implementation (so relatively stable). As we control this order (as opposed to the user, which can only affect the order of the ParsedArgs), it's not necessary to append the type/name of each arg to disambuiguate 2 args that would have the same value, as they cannot be in the same position across 2 invocations.


case object NoCache extends Arg with CacheKey {
override def apply(sa: ScalafixArguments): ScalafixArguments =
sa // caching is currently implemented in sbt-scalafix itself
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming --no-cache could become a scalafix-cli argument in the future if/when caching is implemented upstream.

@bjaglin bjaglin force-pushed the caching branch 3 times, most recently from 175da1d to 7514078 Compare May 13, 2020 14:21
@bjaglin
Copy link
Collaborator Author

bjaglin commented May 13, 2020

Rebased against #103

@bjaglin bjaglin force-pushed the caching branch 2 times, most recently from 84269dd to d497fc2 Compare May 13, 2020 18:33
@bjaglin
Copy link
Collaborator Author

bjaglin commented May 19, 2020

Rebased against #100

@bjaglin bjaglin changed the title Cache scalafix invocations Opt-in caching for scalafix invocations May 19, 2020
@bjaglin
Copy link
Collaborator Author

bjaglin commented May 19, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants