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

Optimize knownProducts for direct-to-JAR compilation #939

Merged
merged 3 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -46,17 +46,24 @@ object JarUtils {
* "C:\develop\zinc\target\output.jar!sbt\internal\inc\Compile.class"
*/
class ClassInJar(override val toString: String) extends AnyVal {
def toClassFilePath: Option[ClassFilePath] = splitJarReference._2
def toClassFilePath: Option[ClassFilePath] = Option(toClassFilePathOrNull)
def toClassFilePathOrNull: ClassFilePath = {
val idx = toString.indexOf('!')
if (idx < 0) null
else toClassFilePath(idx)
}
def splitJarReference: (File, Option[ClassFilePath]) = {
if (toString.contains("!")) {
val Array(jar, cls) = toString.split("!")
// ClassInJar stores RelClass part with File.separatorChar, however actual paths in zips always use '/'
val classFilePath = cls.replace('\\', '/')
(new File(jar), Some(classFilePath))
} else {
val idx = toString.indexOf('!')
if (idx < 0) {
(new File(toString), None)
} else {
(new File(toString.substring(0, idx)), Some(toClassFilePath(idx)))
}
}
private def toClassFilePath(idx: Int): String = {
// ClassInJar stores RelClass part with File.separatorChar, however actual paths in zips always use '/'
toString.substring(idx + 1).replace('\\', '/')
}

/**
* Wraps the string value inside a java.io.File object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@ object Incremental {

private object AnalysisCallback {

private val parallelKnownProducts = "true" == System.getProperty(
"sbt.analysis.known.products.parallel"
)

/** Allow creating new callback instance to be used in each compile iteration */
class Builder(
internalBinaryToSourceClassName: String => Option[String],
Expand Down Expand Up @@ -1086,20 +1090,29 @@ private final class AnalysisCallback(

private def knownProducts(merged: Analysis) = {
// List classes defined in the files that were compiled in this run.
val ps = java.util.concurrent.ConcurrentHashMap.newKeySet[String]
val knownProducts: ParVector[VirtualFileRef] =
new ParVector(merged.relations.allSources.toVector)
.flatMap(merged.relations.products)
// extract product paths in parallel
val ps: java.util.Set[String] =
if (AnalysisCallback.parallelKnownProducts)
java.util.concurrent.ConcurrentHashMap.newKeySet[String]
else
new java.util.HashSet[String]()
val knownProducts: Vector[VirtualFileRef] =
merged.relations.allSources.toVector.flatMap(merged.relations.products)

def knownProductsPar =
if (AnalysisCallback.parallelKnownProducts) new ParVector(knownProducts) else knownProducts
Copy link
Member

Choose a reason for hiding this comment

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

Does this use the default fork/join scheduler? If so, could it end up competing against other tasks or potentially other Zinc compilation that are also trying to do the same thing? Or it finishes quickly enough that it won't surface? (But takes long enough time if it's not parallelized?)

Copy link
Member

Choose a reason for hiding this comment

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

Looks like I added the parallel thing in 94ba81e, but I'd be happy to see it gone if it's not working.

Copy link
Member Author

@retronym retronym Nov 9, 2020

Choose a reason for hiding this comment

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

In this case I think the best idea is to keep it sequential. If this is a bottle neck, we can try to first reduce the overhead in sequential code (like I've done here, but I'm sure there are more opportunities.) Sequential code is a bit easier to profile and tune. Using parallelism should be done with a careful understanding of whether the task size suitable for parallel collections (e.g. something like (1 to 1000).par.count(_ % 2 == 0) is slower than the sequential version.

I've added a commit to remove the ParVector altogether.


// extract product paths
jo2o(output.getSingleOutputAsPath) match {
case Some(so) if so.getFileName.toString.endsWith(".jar") =>
knownProducts foreach { product =>
new JarUtils.ClassInJar(product.id).toClassFilePath foreach { path =>
ps.add(path.replace('\\', '/'))
knownProductsPar foreach { product =>
new JarUtils.ClassInJar(product.id).toClassFilePathOrNull match {
case null =>
case path =>
ps.add(path.replace('\\', '/'))
}
}
case Some(so) =>
knownProducts foreach { product =>
knownProductsPar foreach { product =>
val productPath = converter.toPath(product)
try {
ps.add(so.relativize(productPath).toString.replace('\\', '/'))
Expand Down