-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Memory efficiency for used names collection
- Build the maps in the used name relation in a mutable map, then use a HashMap builder in a second pass to build the resulting maps. In Scala 2.12.12+ or 2.13.0+, use of the builder is fastest as the builder mutates the structure internally. - Intern the keys and values, so we don't have a separate instance of `UsedName("toString", EnumSet.of(UseScope.Default)` for each call site.
- Loading branch information
Showing
3 changed files
with
64 additions
and
12 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
51 changes: 51 additions & 0 deletions
51
internal/zinc-core/src/main/scala/sbt/internal/inc/RelationBuilder.scala
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,51 @@ | ||
/* | ||
* Zinc - The incremental compiler for Scala. | ||
* Copyright Lightbend, Inc. and Mark Harrah | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package sbt.internal.inc | ||
|
||
import sbt.internal.util.Relation | ||
|
||
import scala.collection.{ immutable, mutable } | ||
|
||
final class RelationBuilder[A <: AnyRef, B <: AnyRef]() { | ||
private[this] val forward = | ||
new java.util.HashMap[A, (A, mutable.Builder[B, immutable.HashSet[B]])]() | ||
private[this] val reverse = | ||
new java.util.HashMap[B, (B, mutable.Builder[A, immutable.HashSet[A]])]() | ||
|
||
def update(a: A, b: B): Unit = { | ||
val (internedB, asBuilder) = | ||
reverse.computeIfAbsent(b, (b => (b, immutable.HashSet.newBuilder[A]))) | ||
val (internedA, bsBuilder) = | ||
forward.computeIfAbsent(a, (a => (a, immutable.HashSet.newBuilder[B]))) | ||
asBuilder += internedA | ||
bsBuilder += internedB | ||
} | ||
|
||
def +=(other: Relation[A, B]): Unit = { | ||
for ((a, bs) <- other.forwardMap.iterator) { | ||
for (b <- bs) { | ||
update(a, b) | ||
} | ||
} | ||
} | ||
|
||
def result(): Relation[A, B] = { | ||
def toImmutable[K, V]( | ||
map: java.util.HashMap[K, (K, mutable.Builder[V, immutable.HashSet[V]])] | ||
): immutable.HashMap[K, immutable.HashSet[V]] = { | ||
val builder = immutable.HashMap.newBuilder[K, immutable.HashSet[V]] | ||
map.entrySet().forEach(e => builder.+=((e.getKey, e.getValue._2.result()))) | ||
builder.result() | ||
} | ||
Relation.make[A, B](toImmutable(forward), toImmutable(reverse)) | ||
} | ||
} |
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