Skip to content

Commit

Permalink
WIP experiment with removing reverse used name relation
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym committed Jun 10, 2021
1 parent d845f9a commit 2c9a0ce
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@ import sbt.internal.util.Relation

import scala.collection.{ immutable, mutable }

final class RelationBuilder[A, B]() {
final class RelationBuilder[A, B](val ignoreReverse: Boolean = false) {
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]])]()
if (ignoreReverse) null
else 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
if (reverse == null) {
val (internedA, bsBuilder) =
forward.computeIfAbsent(a, (a => (a, immutable.HashSet.newBuilder[B])))
bsBuilder += b
} else {
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 = {
Expand All @@ -46,6 +53,7 @@ final class RelationBuilder[A, B]() {
map.entrySet().forEach(e => builder.+=((e.getKey, e.getValue._2.result())))
builder.result()
}
Relation.make[A, B](toImmutable(forward), toImmutable(reverse))
Relation
.make[A, B](toImmutable(forward), if (reverse == null) Map.empty else toImmutable(reverse))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ object UsedName {
name
}
}

trait UsedNameSet {
def contains(useScope: UseScope): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@

package sbt.internal.inc.binary.converters

import java.nio.file.{Path, Paths}
import java.nio.file.{ Path, Paths }
import java.util
import java.util.{List => JList, Map => JMap}
import java.util.{ List => JList, Map => JMap }
import sbt.internal.inc.Relations.ClassDependencies
import sbt.internal.inc._
import sbt.internal.inc.binary.converters.ProtobufDefaults.EmptyLazyCompanions
import sbt.util.InterfaceUtil
import xsbti.{Position, Problem, Severity, T2, UseScope, VirtualFileRef}
import xsbti.compile.{CompileOrder, FileHash, MiniOptions, MiniSetup, Output, OutputGroup}
import xsbti.compile.analysis.{Compilation, ReadMapper, SourceInfo, Stamp}
import xsbti.{ Position, Problem, Severity, T2, UseScope, VirtualFileRef }
import xsbti.compile.{ CompileOrder, FileHash, MiniOptions, MiniSetup, Output, OutputGroup }
import xsbti.compile.analysis.{ Compilation, ReadMapper, SourceInfo, Stamp }
import sbt.internal.inc.binary.converters.ProtobufDefaults.Feedback.StringToException
import sbt.internal.inc.binary.converters.ProtobufDefaults.Feedback.{Readers => ReadersFeedback}
import sbt.internal.inc.binary.converters.ProtobufDefaults.{Classes, ReadersConstants}
import sbt.internal.inc.binary.converters.ProtobufDefaults.Feedback.{ Readers => ReadersFeedback }
import sbt.internal.inc.binary.converters.ProtobufDefaults.{ Classes, ReadersConstants }
import sbt.internal.util.Relation

import scala.collection.JavaConverters._
import xsbti.api._
import ProtobufDefaults.{MissingInt, MissingString}
import ProtobufDefaults.{ MissingInt, MissingString }

final class ProtobufReaders(mapper: ReadMapper, currentVersion: Schema.Version) {
def fromPathString(path: String): Path = Paths.get(path)
Expand Down Expand Up @@ -708,7 +708,7 @@ final class ProtobufReaders(mapper: ReadMapper, currentVersion: Schema.Version)
def fromUsedNamesMap(
map: java.util.Map[String, Schema.UsedNames]
): Relation[String, UsedName] = {
val builder = new RelationBuilder[String, UsedName]
val builder = new RelationBuilder[String, UsedName](ignoreReverse = true)
for ((k, used) <- map.asScala) {
val usedNames = used.getUsedNamesList.asScala
if (!usedNames.isEmpty) {
Expand Down
33 changes: 33 additions & 0 deletions internal/zinc-persist/src/test/scala/sbt/inc/binary/Scratch.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package sbt.inc.binary

object Scratch {
def timed[T](f: => T) = {
val start = System.nanoTime;
try f
finally {
println(((System.nanoTime - start) / 1000 / 1000) + " ms")
}
}
def main(args: Array[String]): Unit = {
for (i <- 1 to 32) {
println(
timed(
sbt.internal.inc.FileAnalysisStore
.binary(new java.io.File("/Users/jz/code/scala/target/compiler/zinc/inc_compile.zip"))
.get
)
)
}
timed(
for (i <- 1 to 32) {
println(
timed(
sbt.internal.inc.FileAnalysisStore
.binary(new java.io.File("/Users/jz/code/scala/target/compiler/zinc/inc_compile.zip"))
.get
)
)
}
)
}
}

0 comments on commit 2c9a0ce

Please sign in to comment.