-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
share custom toolclasspath classloader across projects & configs
Since d81dd72 (see commit description), when `scalafix` or `scalafixAll` is run on a build root that aggregates many projects, many identical classloaders may be created when an external rule is provided on the CLI or when projects use local rules via ScalafixConfig. This allows aggregated tasks across projects & configs to share a single, warm classloader when possible (same Scalafix classpath). Note that cache is bound on purpose to a task value, so two successive invocations of `scalafix` with the exact same arguments & environment will not share the same classloader. This is for simplicity, as local rules can be updated from one run to the other, and to my knowledge, there is no simple way to invalidate a URLClassLoader if the underlying class files change.
- Loading branch information
1 parent
e3f0660
commit 7f800d4
Showing
2 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package scalafix.internal.sbt | ||
|
||
import scala.collection.mutable | ||
|
||
/** A basic thread-safe cache without any eviction. */ | ||
class BlockingCache[K, V] { | ||
private val underlying = new mutable.HashMap[K, V] | ||
|
||
/** | ||
* @param value By-name parameter evaluated when the key if missing. Value computation is guaranteed | ||
* to be called only once per key across all invocations. | ||
*/ | ||
def getOrElseUpdate(key: K, value: => V): V = { | ||
// ConcurrentHashMap does not guarantee that there is only one evaluation of the value, so | ||
// we use our own (global) locking, which is OK as the number of keys is expected to be | ||
// very small (bound by the number of projects in the sbt build). | ||
underlying.synchronized { | ||
underlying.getOrElseUpdate(key, value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters