Skip to content

Commit

Permalink
Merge pull request #1388 from eed3si9n/wip/protobuf_removal
Browse files Browse the repository at this point in the history
[2.x] Remove the Protocol Buffer usage
  • Loading branch information
eed3si9n authored Sep 18, 2024
2 parents 1899979 + 8bc656d commit 1211926
Show file tree
Hide file tree
Showing 24 changed files with 120 additions and 2,812 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version = 3.8.3
runner.dialect = scala212
runner.dialect = scala213

project.git = true
project.excludeFilters = [ /sbt-test/, /input_sources/, /contraband-scala/ ]
Expand Down
38 changes: 2 additions & 36 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ lazy val aggregated: Seq[ProjectReference] = compilerInterface.projectRefs ++
zincCompileCore.projectRefs ++
zincCore.projectRefs ++
zincPersist.projectRefs ++
Seq(zincPersistCore: ProjectReference) ++
zincPersistCoreAssembly.projectRefs ++
zincTesting.projectRefs ++
zinc.projectRefs

Expand Down Expand Up @@ -258,9 +256,9 @@ lazy val zincTesting = (projectMatrix in internalPath / "zinc-testing")
.jvmPlatform(scalaVersions = scala3_only)
.configure(addSbtIO, addSbtUtilLogging)

// Persists the incremental data structures using Protobuf
// Persists the incremental data structures
lazy val zincPersist = (projectMatrix in internalPath / "zinc-persist")
.dependsOn(zincCore, zincCompileCore, zincPersistCoreAssembly, zincCore % "test->test")
.dependsOn(zincCore, zincCompileCore, zincCore % "test->test")
.settings(
name := "zinc Persist",
libraryDependencies += sbinary,
Expand Down Expand Up @@ -293,37 +291,6 @@ lazy val zincPersist = (projectMatrix in internalPath / "zinc-persist")
.jvmPlatform(scalaVersions = scala3_only)
.configure(addBaseSettingsAndTestDeps)

lazy val zincPersistCoreAssembly = (projectMatrix in internalPath / "zinc-persist-core-assembly")
.jvmPlatform(autoScalaLibrary = false)
.settings(
name := "zinc-persist-core-assembly",
crossPaths := false,
autoScalaLibrary := false,
exportJars := true,
Compile / packageBin := (zincPersistCore / Compile / assembly).value,
mimaPreviousArtifacts := Set.empty,
)

lazy val zincPersistCore = (project in internalPath / "zinc-persist-core")
.enablePlugins(ProtobufPlugin)
.settings(
name := "zinc-persist-core",
crossPaths := false,
autoScalaLibrary := false,
exportJars := true,
ProtobufConfig / version := "3.24.4", // sync version w/ plugins.sbt
publish / skip := true,
assembly / assemblyShadeRules := Seq(
ShadeRule
.rename("com.google.protobuf.**" -> "sbt.internal.shaded.com.google.protobuf.@1")
.inAll
),
assembly / assemblyMergeStrategy := { // remove *.proto files
case PathList(ps @ _*) if ps.last.endsWith(".proto") => MergeStrategy.discard
case x => (assembly / assemblyMergeStrategy).value(x)
},
)

// Implements the core functionality of detecting and propagating changes incrementally.
// Defines the data structures for representing file fingerprints and relationships and the overall source analysis
lazy val zincCore = (projectMatrix in internalPath / "zinc-core")
Expand All @@ -332,7 +299,6 @@ lazy val zincCore = (projectMatrix in internalPath / "zinc-core")
zincApiInfo,
zincClasspath,
compilerInterface,
zincPersistCoreAssembly,
// compilerBridge % Test,
zincTesting % Test
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ object Analysis {
}

lazy val dummyOutputPath: Path = Paths.get("/tmp/dummy")
lazy val dummyOutputJarPath: Path = Paths.get("/tmp/dummy/output.jar")
}

private class MAnalysis(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ private[inc] abstract class IncrementalCommon(
options: IncOptions,
profiler: RunProfiler
) extends InvalidationProfilerUtils {
private final val TIMESTAMP_2020 = 1577854800000L

// Work around bugs in classpath handling such as the "currently" problematic -javabootclasspath
private[this] def enableShallowLookup: Boolean =
java.lang.Boolean.getBoolean("xsbt.skip.cp.lookup")
Expand Down Expand Up @@ -306,10 +308,16 @@ private[inc] abstract class IncrementalCommon(
oldAPI: String => AnalyzedClass,
newAPI: String => AnalyzedClass
): APIChanges = {
// ignore timestamp pre-2020 since that likely means that we have a hardcoded 2010 timestamp
def timeStampIsSame(ts1: Long, ts2: Long): Boolean = {
(ts1 < TIMESTAMP_2020) || (ts2 < TIMESTAMP_2020) || (ts1 == ts2)
}
// log.debug(s"[zinc] detectAPIChanges(recompiledClasses = $recompiledClasses)")
def classDiff(className: String, a: AnalyzedClass, b: AnalyzedClass): Option[APIChange] = {
// log.debug(s"[zinc] classDiff($className, ${a.name}, ${b.name})")
if (a.compilationTimestamp() == b.compilationTimestamp() && (a.apiHash == b.apiHash)) None
if (
timeStampIsSame(a.compilationTimestamp(), b.compilationTimestamp()) && (a.apiHash == b.apiHash)
) None
else {
val hasMacro = a.hasMacro || b.hasMacro
if (hasMacro && IncOptions.getRecompileOnMacroDef(options)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

package sbt.internal.inc

import sbt.internal.prof.Zprof

// import sbt.internal.prof.Zprof
import xsbti.UseScope._
import xsbti.VirtualFileRef
import xsbti.compile.{ APIChange => XAPIChange }
Expand All @@ -34,22 +33,23 @@ import scala.collection.mutable.ArrayBuffer
* this class is not thread safe.
*/
abstract class InvalidationProfiler {
def profileRun(): RunProfiler
def registerRun(run: Zprof.ZincRun): Unit
// def profileRun(): RunProfiler
// def registerRun(run: Zprof.ZincRun): Unit
}

object InvalidationProfiler {
final val empty: InvalidationProfiler = new InvalidationProfiler {
override def profileRun(): RunProfiler = RunProfiler.empty
override def registerRun(run: Zprof.ZincRun): Unit = ()
// override def profileRun(): RunProfiler = RunProfiler.empty
// override def registerRun(run: Zprof.ZincRun): Unit = ()
}
}

/*
class ZincInvalidationProfiler extends InvalidationProfiler with XInvalidationProfiler { profiler =>
/* The string table contains any kind of repeated string that is likely to occur
* in the protobuf profiling data. This includes used names, class names, source
* files and class files (their paths), as well as other repeated strings. This is
* done to keep the memory overhead of the profiler to a minimum. */
* in the protobuf profiling data. This includes used names, class names, source
* files and class files (their paths), as well as other repeated strings. This is
* done to keep the memory overhead of the profiler to a minimum. */
private final val stringTable: ArrayBuffer[String] = new ArrayBuffer[String](1000)
private final val stringTableIndices: mutable.HashMap[String, Int] =
new mutable.HashMap[String, Int]
Expand All @@ -61,17 +61,17 @@ class ZincInvalidationProfiler extends InvalidationProfiler with XInvalidationPr
def registerRun(run: Zprof.ZincRun): Unit = runs ::= run
/**
* Returns an immutable zprof profile that can be serialized.
*
* It is recommended to only perform this operation when we are
* going to persist the profiled protobuf data to disk. Do not
* call this function after every compiler iteration as you will
* write a symbol table in every persisted protobuf file. It's
* better to persist this file periodically after several runs
* so that the overhead to disk is not high.
*
* @return An immutable zprof profile that can be persisted via protobuf.
*/
* Returns an immutable zprof profile that can be serialized.
*
* It is recommended to only perform this operation when we are
* going to persist the profiled protobuf data to disk. Do not
* call this function after every compiler iteration as you will
* write a symbol table in every persisted protobuf file. It's
* better to persist this file periodically after several runs
* so that the overhead to disk is not high.
*
* @return An immutable zprof profile that can be persisted via protobuf.
*/
def toProfile: Zprof.Profile =
Zprof.Profile.newBuilder
.addAllRuns(runs.asJava)
Expand Down Expand Up @@ -208,6 +208,7 @@ class ZincInvalidationProfiler extends InvalidationProfiler with XInvalidationPr
private def listMap[A, B](xs: Iterable[A])(f: A => B) = xs.iterator.map(f).toList.asJava
}
}
*/

/**
* Defines the interface of a profiler. This interface is used in the guts of
Expand Down Expand Up @@ -288,5 +289,7 @@ trait InvalidationProfilerUtils {
final val MacroExpansionKind = "macro expansion"
}

/*
// So that others users from outside [[IncrementalCommon]] can use the labels
object InvalidationProfilerUtils extends InvalidationProfilerUtils
*/
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ sealed abstract class UsedNames private {
}

object UsedNames {
def fromJavaMap(map: ju.Map[String, Schema.UsedNames]) = JavaUsedNames(map)
// def fromJavaMap(map: ju.Map[String, Schema.UsedNames]) = JavaUsedNames(map)
def fromMultiMap(map: sc.Map[String, sc.Set[UsedName]]) = ScalaUsedNames(map)

final case class ScalaUsedNames(map: sc.Map[String, sc.Set[UsedName]]) extends UsedNames {
Expand All @@ -68,86 +68,4 @@ object UsedNames {
def affectedNames(modifiedNames: ModifiedNames, from: String): String =
map(from).iterator.filter(modifiedNames.isModified).mkString(", ")
}

final case class JavaUsedNames(map: ju.Map[String, Schema.UsedNames]) extends UsedNames {

import scala.collection.JavaConverters._

private def fromUseScope(useScope: Schema.UseScope, id: Int): UseScope = useScope match {
case Schema.UseScope.DEFAULT => UseScope.Default
case Schema.UseScope.IMPLICIT => UseScope.Implicit
case Schema.UseScope.PATMAT => UseScope.PatMatTarget
case Schema.UseScope.UNRECOGNIZED =>
sys.error(s"Unrecognized ${classOf[Schema.UseScope].getName} with value `$id`.")
}

private def fromUsedName(usedName: Schema.UsedName): UsedName = {
val name = usedName.getName.intern() // ?
val useScopes = ju.EnumSet.noneOf(classOf[UseScope])
val len = usedName.getScopesCount
for (i <- 0 to len - 1)
useScopes.add(fromUseScope(usedName.getScopes(i), usedName.getScopesValue(i)))
UsedName.make(name, useScopes)
}

private def fromUsedNamesMap(map: ju.Map[String, Schema.UsedNames]) =
for ((k, used) <- map.asScala)
yield k -> used.getUsedNamesList.asScala.iterator.map(fromUsedName).toSet

lazy val toMultiMap: sc.Map[String, sc.Set[UsedName]] = fromUsedNamesMap(map)
private lazy val convert: UsedNames = fromMultiMap(toMultiMap)

def isEmpty = map.isEmpty

def ++(other: UsedNames) = convert ++ other

def --(classes: Iterable[String]) = convert -- classes

def iterator = convert.iterator

def hasAffectedNames(modifiedNames: ModifiedNames, from: String): Boolean = {
val usedNames = map.get(from)
var i = 0
val n = usedNames.getUsedNamesCount
while (i < n) {
val usedName = usedNames.getUsedNames(i)
val name = usedName.getName
var i2 = 0
val n2 = usedName.getScopesCount
while (i2 < n2) {
val scope = fromUseScope(usedName.getScopes(i2), usedName.getScopesValue(i2))
if (modifiedNames.isModifiedRaw(name, scope)) {
return true
}
i2 += 1
}
i += 1
}
false
}

def affectedNames(modifiedNames: ModifiedNames, from: String): String = {
val b = new StringBuilder()
val usedNames = map.get(from)
var first = true
var i = 0
val n = usedNames.getUsedNamesCount
while (i < n) {
val usedName = usedNames.getUsedNames(i)
val name = usedName.getName
var i2 = 0
val n2 = usedName.getScopesCount
while (i2 < n2) {
val scope = fromUseScope(usedName.getScopes(i2), usedName.getScopesValue(i2))
if (modifiedNames.isModifiedRaw(name, scope)) {
if (first) first = false else b.append(", ")
b.append(name)
}
i2 += 1
}
i += 1
}
b.toString
}
}
}
Loading

0 comments on commit 1211926

Please sign in to comment.