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

Make Stamps deterministic #42

Merged
merged 2 commits into from
Apr 10, 2024
Merged
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 @@ -6,7 +6,9 @@ import java.io.{File, InputStream, OutputStream, OutputStreamWriter}
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, NoSuchFileException, Path, Paths}
import java.nio.file.attribute.FileTime
import java.util
import java.util.concurrent.ConcurrentHashMap
import java.util.LinkedHashMap
import java.util.zip.{GZIPInputStream, GZIPOutputStream}
import java.util.Optional
import sbt.internal.inc.binary.converters.{ProtobufReaders, ProtobufWriters}
Expand All @@ -15,12 +17,10 @@ import sbt.internal.inc.{APIs, Analysis, PlainVirtualFile, PlainVirtualFileConve
import sbt.internal.inc.Schema.{Access, AnalyzedClass, Annotation, AnnotationArgument, ClassDefinition, ClassDependencies, ClassLike, Companions, MethodParameter, NameHash, ParameterList, Path => SchemaPath, Qualifier, Type, TypeParameter, UsedName, UsedNames, Values}
import sbt.internal.shaded.com.google.protobuf.GeneratedMessageV3
import sbt.io.IO
import scala.math.Ordering
import scala.collection.mutable.StringBuilder
import scala.collection.immutable.TreeMap
import xsbti.compile.analysis.{GenericMapper, ReadMapper, ReadWriteMappers, Stamp, WriteMapper}
import xsbti.compile.{AnalysisContents, AnalysisStore, MiniSetup}
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import xsbti.VirtualFileRef
import java.util.Objects

Expand Down Expand Up @@ -97,7 +97,7 @@ class AnxAnalysisStore(files: AnalysisFiles, analyses: AnxAnalyses) extends Anal
apis = analyses.apis().read(files.apis),
relations = analyses.relations.read(files.relations),
infos = analyses.sourceInfos.read(files.sourceInfos),
stamps = analyses.stamps.read(files.stamps)
stamps = analyses.stamps().read(files.stamps)
)
val miniSetup = analyses.miniSetup().read(files.miniSetup)
Optional.of(AnalysisContents.create(analysis, miniSetup))
Expand All @@ -115,7 +115,7 @@ class AnxAnalysisStore(files: AnalysisFiles, analyses: AnxAnalyses) extends Anal
analyses.apis().write(files.apis, analysis.apis)
analyses.relations.write(files.relations, analysis.relations)
analyses.sourceInfos.write(files.sourceInfos, analysis.infos)
analyses.stamps.write(files.stamps, analysis.stamps)
analyses.stamps().write(files.stamps, analysis.stamps)
val miniSetup = analysisContents.getMiniSetup
analyses.miniSetup().write(files.miniSetup, miniSetup)
}
Expand Down Expand Up @@ -690,10 +690,38 @@ class AnxAnalyses(format: AnxAnalysisStore.Format) {
(stream, value) => format.write(writer.toSourceInfos(value), stream)
)

def stamps = new Store[Stamps](
stream => reader.fromStamps(format.read(Schema.Stamps.getDefaultInstance, stream)),
(stream, value) => format.write(writer.toStamps(value), stream)
)
def stamps(): Store[Stamps] = {
new Store[Stamps](
stream => reader.fromStamps(format.read(Schema.Stamps.getDefaultInstance, stream)),
(stream, value) =>
format.write(
sortStamps(writer.toStamps(value)),
stream
)
)
}

def sortStamps(stamps: Schema.Stamps): Schema.Stamps = {
val sortedProductStamps = sortStampTypeMap(stamps.getProductStampsMap).asJava
val sortedBinaryStamps = sortStampTypeMap(stamps.getBinaryStampsMap).asJava
val sortedSourceStamps = sortStampTypeMap(stamps.getSourceStampsMap).asJava

Schema.Stamps
.newBuilder(stamps)
.clearProductStamps()
.putAllProductStamps(sortedProductStamps)
.clearBinaryStamps()
.putAllBinaryStamps(sortedBinaryStamps)
.clearSourceStamps()
.putAllSourceStamps(sortedSourceStamps)
.build()
}

def sortStampTypeMap(
stampTypeMap: util.Map[String, Schema.Stamps.StampType]
): TreeMap[String, Schema.Stamps.StampType] = {
TreeMap[String, Schema.Stamps.StampType]() ++ stampTypeMap.asScala
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
TreeMap[String, Schema.Stamps.StampType]() ++ stampTypeMap.asScala
TreeMap.from(stampTypeMap.asScala)

Though going from a Java type to a Scala type just to put it ordered map and then convert back to a Java type seems wasteful. Why not go directly to the ordered Java map?

Copy link
Author

Choose a reason for hiding this comment

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

Good questions, I don't remember this code was written so long ago. Lemme go take another look. I bet we can go directly to the ordered Java map and that was a rough edge I forgot about last year.

Copy link
Author

Choose a reason for hiding this comment

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

I updated this to use a java.util.TreeMap directly.

}
}

object AnxMapper {
Expand Down
Loading