[IC] Add methods to write without explicit registering in a transaction

#KT-49785 In Progress
This commit is contained in:
Alexander.Likhachev
2023-01-16 16:49:37 +01:00
committed by Space Team
parent 581bc89849
commit 3dcf5af4b0
7 changed files with 42 additions and 30 deletions
@@ -37,6 +37,24 @@ interface CompilationTransaction : Closeable {
fun markAsSuccessful()
}
fun CompilationTransaction.write(file: Path, writeAction: () -> Unit) {
registerAddedOrChangedFile(file)
writeAction()
}
fun CompilationTransaction.writeText(file: Path, text: String) {
writeBytes(file, text.toByteArray())
}
fun CompilationTransaction.writeBytes(file: Path, array: ByteArray) {
write(file) {
if (!Files.exists(file.parent)) {
Files.createDirectories(file.parent)
}
Files.write(file, array)
}
}
/**
* A dummy implementation of compilation transaction
*/
@@ -72,9 +72,7 @@ open class IncrementalJsCache(
var header: ByteArray
get() = headerFile.readBytes()
set(value) {
icContext.transaction.registerAddedOrChangedFile(headerFile.toPath())
cachesDir.mkdirs()
headerFile.writeBytes(value)
icContext.transaction.writeBytes(headerFile.toPath(), value)
}
override fun markDirty(removedAndCompiledSources: Collection<File>) {
@@ -148,13 +148,7 @@ open class LookupStorage(
try {
if (size != oldSize) {
if (size > 0) {
icContext.transaction.registerAddedOrChangedFile(countersFile.toPath())
if (!countersFile.exists()) {
countersFile.parentFile.mkdirs()
countersFile.createNewFile()
}
countersFile.writeText("$size\n0")
icContext.transaction.writeText(countersFile.toPath(), "$size\n0")
}
}
} finally {
@@ -145,9 +145,10 @@ class AbiSnapshotImpl(override val protos: MutableMap<FqName, ProtoData>) : AbiS
}
fun write(icContext: IncrementalCompilationContext, buildInfo: AbiSnapshot, file: File) {
icContext.transaction.registerAddedOrChangedFile(file.toPath())
ObjectOutputStream(FileOutputStream(file)).use {
it.writeAbiSnapshot(buildInfo)
icContext.transaction.write(file.toPath()) {
ObjectOutputStream(FileOutputStream(file)).use {
it.writeAbiSnapshot(buildInfo)
}
}
}
@@ -64,21 +64,23 @@ data class BuildDiffsStorage(val buildDiffs: List<BuildDifference>) {
return null
}
fun writeToFile(file: File, storage: BuildDiffsStorage, reporter: ICReporter?) {
fun writeToFile(icContext: IncrementalCompilationContext, file: File, storage: BuildDiffsStorage) {
file.parentFile.mkdirs()
try {
ObjectOutputStream(file.outputStream().buffered()).use { output ->
output.writeInt(CURRENT_VERSION)
icContext.transaction.write(file.toPath()) {
ObjectOutputStream(file.outputStream().buffered()).use { output ->
output.writeInt(CURRENT_VERSION)
val diffsToWrite = storage.buildDiffs.sortedBy { it.ts }.takeLast(MAX_DIFFS_ENTRIES)
output.writeInt(diffsToWrite.size)
for (diff in diffsToWrite) {
output.writeBuildDifference(diff)
val diffsToWrite = storage.buildDiffs.sortedBy { it.ts }.takeLast(MAX_DIFFS_ENTRIES)
output.writeInt(diffsToWrite.size)
for (diff in diffsToWrite) {
output.writeBuildDifference(diff)
}
}
}
} catch (e: IOException) {
reporter?.info { "Could not write diff to file $file: $e" }
icContext.reporter.info { "Could not write diff to file $file: $e" }
}
}
@@ -495,8 +495,7 @@ abstract class IncrementalCompilerRunner<
dirtySources.addAll(compiledSources)
allDirtySources.addAll(dirtySources)
val text = allDirtySources.joinToString(separator = System.getProperty("line.separator")) { it.normalize().absolutePath }
transaction.registerAddedOrChangedFile(dirtySourcesSinceLastTimeFile.toPath())
dirtySourcesSinceLastTimeFile.writeText(text)
transaction.writeText(dirtySourcesSinceLastTimeFile.toPath(), text)
val generatedFiles = outputItemsCollector.outputs.map {
it.toGeneratedFile(jvmMetadataVersionFromLanguageVersion)
@@ -583,7 +582,7 @@ abstract class IncrementalCompilerRunner<
}
val dirtyData = DirtyData(buildDirtyLookupSymbols, buildDirtyFqNames)
processChangesAfterBuild(compilationMode, currentBuildInfo, dirtyData, transaction)
processChangesAfterBuild(icContext, compilationMode, currentBuildInfo, dirtyData)
return exitCode
}
@@ -615,10 +614,10 @@ abstract class IncrementalCompilerRunner<
open fun runWithNoDirtyKotlinSources(caches: CacheManager): Boolean = false
private fun processChangesAfterBuild(
icContext: IncrementalCompilationContext,
compilationMode: CompilationMode,
currentBuildInfo: BuildInfo,
dirtyData: DirtyData,
transaction: CompilationTransaction,
) = reporter.measure(BuildTime.IC_WRITE_HISTORY_FILE) {
val prevDiffs = BuildDiffsStorage.readFromFile(buildHistoryFile, reporter)?.buildDiffs ?: emptyList()
val newDiff = if (compilationMode is CompilationMode.Incremental) {
@@ -628,8 +627,7 @@ abstract class IncrementalCompilerRunner<
BuildDifference(currentBuildInfo.startTS, false, emptyDirtyData)
}
transaction.registerAddedOrChangedFile(buildHistoryFile.toPath())
BuildDiffsStorage.writeToFile(buildHistoryFile, BuildDiffsStorage(prevDiffs + newDiff), reporter)
BuildDiffsStorage.writeToFile(icContext, buildHistoryFile, BuildDiffsStorage(prevDiffs + newDiff))
}
companion object {
@@ -28,6 +28,7 @@ import java.util.*
class BuildDiffsStorageTest {
lateinit var storageFile: File
private val random = Random(System.currentTimeMillis())
private val icContext = IncrementalCompilationContext(null)
@Before
fun setUp() {
@@ -54,7 +55,7 @@ class BuildDiffsStorageTest {
@Test
fun writeReadSimple() {
val diffs = BuildDiffsStorage(listOf(getRandomDiff()))
BuildDiffsStorage.writeToFile(storageFile, diffs, reporter = null)
BuildDiffsStorage.writeToFile(icContext, storageFile, diffs)
val diffsDeserialized = BuildDiffsStorage.readFromFile(storageFile, reporter = null)
Assert.assertEquals(diffs.toString(), diffsDeserialized.toString())
@@ -64,7 +65,7 @@ class BuildDiffsStorageTest {
fun writeReadMany() {
val generated = Array(20) { getRandomDiff() }.toList()
val diffs = BuildDiffsStorage(generated)
BuildDiffsStorage.writeToFile(storageFile, diffs, reporter = null)
BuildDiffsStorage.writeToFile(icContext, storageFile, diffs)
val diffsDeserialized = BuildDiffsStorage.readFromFile(storageFile, reporter = null)
val expected = generated.sortedBy { it.ts }.takeLast(BuildDiffsStorage.MAX_DIFFS_ENTRIES).toTypedArray()
@@ -82,7 +83,7 @@ class BuildDiffsStorageTest {
@Test
fun versionChanged() {
val diffs = BuildDiffsStorage(listOf(getRandomDiff()))
BuildDiffsStorage.writeToFile(storageFile, diffs, reporter = null)
BuildDiffsStorage.writeToFile(icContext, storageFile, diffs)
val versionBackup = BuildDiffsStorage.CURRENT_VERSION
try {