Use IOUtil instead of DataOutput for readUTF/writeUTF in file based index

It uses less space and also may be a little bit faster
This commit is contained in:
Alexander Udalov
2016-11-15 11:18:57 +03:00
parent 0fb5a18a26
commit 3d75b78dc5
2 changed files with 18 additions and 28 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.vfilefinder
import com.intellij.ide.highlighter.JavaClassFileType
import com.intellij.openapi.diagnostic.Logger
import com.intellij.util.indexing.*
import com.intellij.util.io.IOUtil
import com.intellij.util.io.KeyDescriptor
import org.jetbrains.kotlin.idea.caches.IDEKotlinBinaryClassCache
import org.jetbrains.kotlin.idea.decompiler.js.JsMetaFileUtils
@@ -32,9 +33,9 @@ abstract class KotlinFileIndexBase<T>(classOfIndex: Class<T>) : ScalarIndexExten
val KEY: ID<FqName, Void> = ID.create(classOfIndex.canonicalName)
private val KEY_DESCRIPTOR : KeyDescriptor<FqName> = object : KeyDescriptor<FqName> {
override fun save(output: DataOutput, value: FqName) = output.writeUTF(value.asString())
override fun save(output: DataOutput, value: FqName) = IOUtil.writeUTF(output, value.asString())
override fun read(input: DataInput) = FqName(input.readUTF())
override fun read(input: DataInput) = FqName(IOUtil.readUTF(input))
override fun getHashCode(value: FqName) = value.asString().hashCode()
@@ -76,9 +77,9 @@ object KotlinClassFileIndex : KotlinFileIndexBase<KotlinClassFileIndex>(KotlinCl
override fun getVersion() = VERSION
private val VERSION = 2
private val VERSION = 3
private val INDEXER = indexer() { fileContent ->
private val INDEXER = indexer { fileContent ->
val headerInfo = IDEKotlinBinaryClassCache.getKotlinBinaryClassHeaderData(fileContent.file, fileContent.content)
if (headerInfo != null && headerInfo.classHeader.metadataVersion.isCompatible()) headerInfo.classId.asSingleFqName() else null
}
@@ -92,9 +93,9 @@ object KotlinJavaScriptMetaFileIndex : KotlinFileIndexBase<KotlinJavaScriptMetaF
override fun getVersion() = VERSION
private val VERSION = 2
private val VERSION = 3
private val INDEXER = indexer() { fileContent ->
private val INDEXER = indexer { fileContent ->
if (fileContent.fileType == KotlinJavaScriptMetaFileType) JsMetaFileUtils.getClassFqName(fileContent.file) else null
}
@@ -16,9 +16,9 @@
package org.jetbrains.kotlin.idea.vfilefinder
import com.intellij.openapi.diagnostic.Logger
import com.intellij.util.indexing.*
import com.intellij.util.io.DataExternalizer
import com.intellij.util.io.IOUtil
import com.intellij.util.io.KeyDescriptor
import org.jetbrains.kotlin.load.kotlin.ModuleMapping
import org.jetbrains.kotlin.load.kotlin.PackageParts
@@ -27,14 +27,12 @@ import java.io.DataOutput
object KotlinModuleMappingIndex : FileBasedIndexExtension<String, PackageParts>() {
private val classOfIndex = KotlinModuleMappingIndex::class.java.canonicalName
val KEY: ID<String, PackageParts> = ID.create(classOfIndex)
val KEY: ID<String, PackageParts> = ID.create(KotlinModuleMappingIndex::class.java.canonicalName)
private val KEY_DESCRIPTOR = object : KeyDescriptor<String> {
override fun save(output: DataOutput, value: String) = output.writeUTF(value)
override fun save(output: DataOutput, value: String) = IOUtil.writeUTF(output, value)
override fun read(input: DataInput) = input.readUTF()
override fun read(input: DataInput) = IOUtil.readUTF(input)
override fun getHashCode(value: String) = value.hashCode()
@@ -42,26 +40,17 @@ object KotlinModuleMappingIndex : FileBasedIndexExtension<String, PackageParts>(
}
private val VALUE_EXTERNALIZER = object : DataExternalizer<PackageParts> {
override fun read(`in`: DataInput): PackageParts? {
val packageFqName = `in`.readUTF()
val parts = PackageParts(packageFqName)
val size = `in`.readInt()
(1..size).forEach {
parts.parts.add(`in`.readUTF())
}
return parts
}
override fun read(input: DataInput): PackageParts? =
PackageParts(IOUtil.readUTF(input)).apply {
parts.addAll(IOUtil.readStringList(input))
}
override fun save(out: DataOutput, value: PackageParts?) {
out.writeUTF(value!!.packageFqName)
out.writeInt(value.parts.size)
value.parts.forEach { out.writeUTF(it) }
IOUtil.writeUTF(out, value!!.packageFqName)
IOUtil.writeStringList(out, value.parts)
}
}
private val LOG = Logger.getInstance(classOfIndex)
override fun getName() = KEY
override fun dependsOnFileContent() = true
@@ -74,7 +63,7 @@ object KotlinModuleMappingIndex : FileBasedIndexExtension<String, PackageParts>(
return FileBasedIndex.InputFilter { file -> file.extension == ModuleMapping.MAPPING_FILE_EXT }
}
override fun getVersion(): Int = 1
override fun getVersion(): Int = 2
override fun getIndexer(): DataIndexer<String, PackageParts, FileContent> {
return DataIndexer<String, PackageParts, FileContent> { inputData ->