From a5041002481d112f1326134cf139491a025bfbae Mon Sep 17 00:00:00 2001 From: Jiaxiang Chen Date: Fri, 22 Dec 2023 23:20:21 -0800 Subject: [PATCH] Revert "Replace compiler's persistent maps with ours" This reverts commit 9dd4aa1f9b589afa1ae7793847125dd27d36fb32. --- .../com/google/devtools/ksp/PersistentMap.kt | 85 ------------------- .../com/google/devtools/ksp/Incremental.kt | 75 ++++++++++++---- 2 files changed, 60 insertions(+), 100 deletions(-) delete mode 100644 common-util/src/main/kotlin/com/google/devtools/ksp/PersistentMap.kt diff --git a/common-util/src/main/kotlin/com/google/devtools/ksp/PersistentMap.kt b/common-util/src/main/kotlin/com/google/devtools/ksp/PersistentMap.kt deleted file mode 100644 index 18a440ae1f..0000000000 --- a/common-util/src/main/kotlin/com/google/devtools/ksp/PersistentMap.kt +++ /dev/null @@ -1,85 +0,0 @@ -package com.google.devtools.ksp - -import com.intellij.util.io.DataExternalizer -import com.intellij.util.io.IOUtil -import com.intellij.util.io.KeyDescriptor -import com.intellij.util.io.PersistentHashMap -import java.io.DataInput -import java.io.DataInputStream -import java.io.DataOutput -import java.io.File - -abstract class PersistentMap( - storageFile: File, - keyDescriptor: KeyDescriptor, - dataExternalizer: DataExternalizer -) : PersistentHashMap( - storageFile, - keyDescriptor, - dataExternalizer -) { - val keys: Collection - get() = mutableListOf().also { list -> - this.processKeysWithExistingMapping { key -> list.add(key) } - } - - operator fun set(key: K, value: V) = put(key, value) - - fun clear() { - keys.forEach { - remove(it) - } - } - - fun flush() = force() -} - -object FileKeyDescriptor : KeyDescriptor { - override fun read(input: DataInput): File { - return File(IOUtil.readString(input)) - } - - override fun save(output: DataOutput, value: File) { - IOUtil.writeString(value.path, output) - } - - override fun getHashCode(value: File): Int = value.hashCode() - - override fun isEqual(val1: File, val2: File): Boolean = val1 == val2 -} - -object FileExternalizer : DataExternalizer { - override fun read(input: DataInput): File = File(IOUtil.readString(input)) - - override fun save(output: DataOutput, value: File) { - IOUtil.writeString(value.path, output) - } -} - -class ListExternalizer( - private val elementExternalizer: DataExternalizer, -) : DataExternalizer> { - - override fun save(output: DataOutput, value: List) { - value.forEach { elementExternalizer.save(output, it) } - } - - override fun read(input: DataInput): List { - val result = mutableListOf() - val stream = input as DataInputStream - - while (stream.available() > 0) { - result.add(elementExternalizer.read(stream)) - } - - return result - } -} - -class FileToFilesMap( - storageFile: File, -) : PersistentMap>( - storageFile, - FileKeyDescriptor, - ListExternalizer(FileExternalizer) -) diff --git a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt index 1312e65fa6..f7cc473348 100644 --- a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt +++ b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt @@ -27,19 +27,17 @@ import com.intellij.psi.impl.source.PsiClassReferenceType import com.intellij.util.containers.MultiMap import com.intellij.util.io.DataExternalizer import com.intellij.util.io.IOUtil +import com.intellij.util.io.KeyDescriptor import org.jetbrains.kotlin.container.ComponentProvider import org.jetbrains.kotlin.container.get import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.incremental.IncrementalCompilationContext -import org.jetbrains.kotlin.incremental.LookupStorage -import org.jetbrains.kotlin.incremental.LookupSymbol -import org.jetbrains.kotlin.incremental.LookupTrackerImpl +import org.jetbrains.kotlin.incremental.* import org.jetbrains.kotlin.incremental.components.LookupTracker import org.jetbrains.kotlin.incremental.components.Position import org.jetbrains.kotlin.incremental.components.ScopeKind +import org.jetbrains.kotlin.incremental.storage.AppendableAbstractBasicMap import org.jetbrains.kotlin.incremental.storage.FileToPathConverter -import org.jetbrains.kotlin.incremental.update import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperclassesWithoutAny import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.typeUtil.supertypes @@ -48,14 +46,37 @@ import java.io.DataOutput import java.io.File import java.util.* +abstract class PersistentMap, V>( + storageFile: File, + keyDescriptor: KeyDescriptor, + valueExternalizer: DataExternalizer, + icContext: IncrementalCompilationContext, +) : AppendableAbstractBasicMap>(storageFile, keyDescriptor, valueExternalizer, icContext) + class FileToSymbolsMap( storageFile: File, -) : PersistentMap>( + icContext: IncrementalCompilationContext +) : PersistentMap( storageFile, FileKeyDescriptor, - ListExternalizer(LookupSymbolExternalizer), + LookupSymbolExternalizer, + icContext, ) +object FileKeyDescriptor : KeyDescriptor { + override fun read(input: DataInput): File { + return File(IOUtil.readString(input)) + } + + override fun save(output: DataOutput, value: File) { + IOUtil.writeString(value.path, output) + } + + override fun getHashCode(value: File): Int = value.hashCode() + + override fun isEqual(val1: File, val2: File): Boolean = val1 == val2 +} + object LookupSymbolExternalizer : DataExternalizer { override fun read(input: DataInput): LookupSymbol = LookupSymbol(IOUtil.readString(input), IOUtil.readString(input)) @@ -65,6 +86,29 @@ object LookupSymbolExternalizer : DataExternalizer { } } +object FileExternalizer : DataExternalizer { + override fun read(input: DataInput): File = File(IOUtil.readString(input)) + + override fun save(output: DataOutput, value: File) { + IOUtil.writeString(value.path, output) + } +} + +class FileToFilesMap( + storageFile: File, + icContext: IncrementalCompilationContext +) : PersistentMap( + storageFile, + FileKeyDescriptor, + FileExternalizer, + icContext, +) { + override fun dumpKey(key: File): String = key.path + + override fun dumpValue(value: Collection) = + value.dumpCollection() +} + object symbolCollector : KSDefaultVisitor<(LookupSymbol) -> Unit, Unit>() { override fun defaultHandler(node: KSNode, data: (LookupSymbol) -> Unit) = Unit @@ -109,14 +153,15 @@ class IncrementalContext( private val baseDir = options.projectBaseDir private val PATH_CONVERTER = RelativeFileToPathConverter(baseDir) - private val icContext = IncrementalCompilationContext(PATH_CONVERTER, PATH_CONVERTER, true) + private val icContext = + IncrementalCompilationContext(pathConverter = PATH_CONVERTER, trackChangesInLookupCache = true) // Sealed classes / interfaces on which `getSealedSubclasses` is invoked. // This is saved across processing. - private val sealedMap = FileToSymbolsMap(File(options.cachesDir, "sealed")) + private val sealedMap = FileToSymbolsMap(File(options.cachesDir, "sealed"), icContext) // Symbols defined in each file. This is saved across processing. - private val symbolsMap = FileToSymbolsMap(File(options.cachesDir, "symbols")) + private val symbolsMap = FileToSymbolsMap(File(options.cachesDir, "symbols"), icContext) private val cachesUpToDateFile = File(options.cachesDir, "caches.uptodate") private val rebuild = !cachesUpToDateFile.exists() @@ -142,7 +187,7 @@ class IncrementalContext( private val classLookupCacheDir = File(options.cachesDir, "classLookups") private val classLookupCache = LookupStorage(classLookupCacheDir, icContext) - private val sourceToOutputsMap = FileToFilesMap(File(options.cachesDir, "sourceToOutputs")) + private val sourceToOutputsMap = FileToFilesMap(File(options.cachesDir, "sourceToOutputs"), icContext) private fun String.toRelativeFile() = File(this).relativeTo(baseDir) private val KSFile.relativeFile @@ -361,7 +406,7 @@ class IncrementalContext( // Update source-to-outputs map from those reprocessed. sourceToOutputs.forEach { src, outs -> - sourceToOutputsMap[src] = outs.toList() + sourceToOutputsMap[src] = outs } logSourceToOutputs(outputs, sourceToOutputs) @@ -408,14 +453,14 @@ class IncrementalContext( updateLookupCache(dirtyFiles) // Update symbolsMap - fun , V> update(m: PersistentMap>, u: MultiMap) { + fun , V> update(m: PersistentMap, u: MultiMap) { // Update symbol caches from modified files. u.keySet().forEach { - m.set(it, u[it].toList()) + m.set(it, u[it].toSet()) } } - fun , V> remove(m: PersistentMap>, removedKeys: Collection) { + fun , V> remove(m: PersistentMap, removedKeys: Collection) { // Remove symbol caches from removed files. removedKeys.forEach { m.remove(it)