avoid unnecessary reads of file content during indexing

This commit is contained in:
Dmitry Jemerov
2015-11-30 16:16:54 +01:00
parent f9306d9c3c
commit 05a62c5892
3 changed files with 14 additions and 13 deletions
@@ -25,7 +25,7 @@ import com.intellij.openapi.vfs.VirtualFile
class KotlinBinaryClassCache : Disposable { class KotlinBinaryClassCache : Disposable {
private class RequestCache { private class RequestCache {
internal lateinit var virtualFile: VirtualFile internal var virtualFile: VirtualFile? = null
internal var modificationStamp: Long = 0 internal var modificationStamp: Long = 0
internal var virtualFileKotlinClass: VirtualFileKotlinClass? = null internal var virtualFileKotlinClass: VirtualFileKotlinClass? = null
@@ -53,7 +53,7 @@ class KotlinBinaryClassCache : Disposable {
companion object { companion object {
fun getKotlinBinaryClass(file: VirtualFile): KotlinJvmBinaryClass? { fun getKotlinBinaryClass(file: VirtualFile, fileContent: ByteArray? = null): KotlinJvmBinaryClass? {
if (file.fileType !== JavaClassFileType.INSTANCE) return null if (file.fileType !== JavaClassFileType.INSTANCE) return null
val service = ServiceManager.getService(KotlinBinaryClassCache::class.java) val service = ServiceManager.getService(KotlinBinaryClassCache::class.java)
@@ -65,7 +65,7 @@ class KotlinBinaryClassCache : Disposable {
else { else {
val aClass = ApplicationManager.getApplication().runReadAction(Computable { val aClass = ApplicationManager.getApplication().runReadAction(Computable {
//noinspection deprecation //noinspection deprecation
VirtualFileKotlinClass.create(file) VirtualFileKotlinClass.create(file, fileContent)
}) })
return requestCache.cache(file, aClass) return requestCache.cache(file, aClass)
@@ -54,12 +54,12 @@ public class VirtualFileKotlinClass private constructor(
private val perfCounter = PerformanceCounter.create("Binary class from Kotlin file") private val perfCounter = PerformanceCounter.create("Binary class from Kotlin file")
@Deprecated("Use KotlinBinaryClassCache") @Deprecated("Use KotlinBinaryClassCache")
fun create(file: VirtualFile): VirtualFileKotlinClass? { fun create(file: VirtualFile, fileContent: ByteArray?): VirtualFileKotlinClass? {
return perfCounter.time { return perfCounter.time {
assert(file.getFileType() == JavaClassFileType.INSTANCE) { "Trying to read binary data from a non-class file $file" } assert(file.getFileType() == JavaClassFileType.INSTANCE) { "Trying to read binary data from a non-class file $file" }
try { try {
val byteContent = file.contentsToByteArray(false) val byteContent = fileContent ?: file.contentsToByteArray(false)
if (!byteContent.isEmpty()) { if (!byteContent.isEmpty()) {
return@time FileBasedKotlinClass.create(byteContent) { return@time FileBasedKotlinClass.create(byteContent) {
name, header, innerClasses -> name, header, innerClasses ->
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.vfilefinder
import com.intellij.ide.highlighter.JavaClassFileType import com.intellij.ide.highlighter.JavaClassFileType
import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.indexing.* import com.intellij.util.indexing.*
import com.intellij.util.io.KeyDescriptor import com.intellij.util.io.KeyDescriptor
import org.jetbrains.kotlin.idea.decompiler.KotlinJavaScriptMetaFileType import org.jetbrains.kotlin.idea.decompiler.KotlinJavaScriptMetaFileType
@@ -27,7 +26,7 @@ import org.jetbrains.kotlin.load.kotlin.KotlinBinaryClassCache
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import java.io.DataInput import java.io.DataInput
import java.io.DataOutput import java.io.DataOutput
import java.util.Collections import java.util.*
abstract class KotlinFileIndexBase<T>(private val classOfIndex: Class<T>) : ScalarIndexExtension<FqName>() { abstract class KotlinFileIndexBase<T>(private val classOfIndex: Class<T>) : ScalarIndexExtension<FqName>() {
public val KEY: ID<FqName, Void> = ID.create(classOfIndex.canonicalName) public val KEY: ID<FqName, Void> = ID.create(classOfIndex.canonicalName)
@@ -50,10 +49,10 @@ abstract class KotlinFileIndexBase<T>(private val classOfIndex: Class<T>) : Scal
override fun getKeyDescriptor() = KEY_DESCRIPTOR override fun getKeyDescriptor() = KEY_DESCRIPTOR
protected fun indexer(f: (VirtualFile) -> FqName?): DataIndexer<FqName, Void, FileContent> = protected fun indexer(f: (FileContent) -> FqName?): DataIndexer<FqName, Void, FileContent> =
DataIndexer { DataIndexer {
try { try {
val fqName = f(it.file) val fqName = f(it)
if (fqName != null) { if (fqName != null) {
Collections.singletonMap<FqName, Void>(fqName, null) Collections.singletonMap<FqName, Void>(fqName, null)
} }
@@ -78,8 +77,8 @@ public object KotlinClassFileIndex : KotlinFileIndexBase<KotlinClassFileIndex>(K
private val VERSION = 2 private val VERSION = 2
private val INDEXER = indexer() { file -> private val INDEXER = indexer() { fileContent ->
val kotlinClass = KotlinBinaryClassCache.getKotlinBinaryClass(file) val kotlinClass = KotlinBinaryClassCache.getKotlinBinaryClass(fileContent.file, fileContent.content)
if (kotlinClass != null && kotlinClass.classHeader.isCompatibleAbiVersion) kotlinClass.classId.asSingleFqName() else null if (kotlinClass != null && kotlinClass.classHeader.isCompatibleAbiVersion) kotlinClass.classId.asSingleFqName() else null
} }
} }
@@ -94,7 +93,9 @@ public object KotlinJavaScriptMetaFileIndex : KotlinFileIndexBase<KotlinJavaScri
private val VERSION = 2 private val VERSION = 2
private val INDEXER = indexer() { file -> private val INDEXER = indexer() { fileContent ->
if (file.fileType == KotlinJavaScriptMetaFileType) JsMetaFileUtils.getClassFqName(file) else null if (fileContent.fileType == KotlinJavaScriptMetaFileType) JsMetaFileUtils.getClassFqName(fileContent.file) else null
} }
override fun dependsOnFileContent(): Boolean = false
} }