From 05a62c5892b4f30f5bb8592cebe47ecfd09f5989 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Mon, 30 Nov 2015 16:16:54 +0100 Subject: [PATCH] avoid unnecessary reads of file content during indexing --- .../load/kotlin/KotlinBinaryClassCache.kt | 6 +++--- .../load/kotlin/VirtualFileKotlinClass.kt | 4 ++-- .../kotlin/idea/vfilefinder/fileIndexes.kt | 17 +++++++++-------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinBinaryClassCache.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinBinaryClassCache.kt index 08381337f59..81e5d8ddbcd 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinBinaryClassCache.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinBinaryClassCache.kt @@ -25,7 +25,7 @@ import com.intellij.openapi.vfs.VirtualFile class KotlinBinaryClassCache : Disposable { private class RequestCache { - internal lateinit var virtualFile: VirtualFile + internal var virtualFile: VirtualFile? = null internal var modificationStamp: Long = 0 internal var virtualFileKotlinClass: VirtualFileKotlinClass? = null @@ -53,7 +53,7 @@ class KotlinBinaryClassCache : Disposable { companion object { - fun getKotlinBinaryClass(file: VirtualFile): KotlinJvmBinaryClass? { + fun getKotlinBinaryClass(file: VirtualFile, fileContent: ByteArray? = null): KotlinJvmBinaryClass? { if (file.fileType !== JavaClassFileType.INSTANCE) return null val service = ServiceManager.getService(KotlinBinaryClassCache::class.java) @@ -65,7 +65,7 @@ class KotlinBinaryClassCache : Disposable { else { val aClass = ApplicationManager.getApplication().runReadAction(Computable { //noinspection deprecation - VirtualFileKotlinClass.create(file) + VirtualFileKotlinClass.create(file, fileContent) }) return requestCache.cache(file, aClass) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/VirtualFileKotlinClass.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/VirtualFileKotlinClass.kt index 1e360bc04d3..f6d223241ac 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/VirtualFileKotlinClass.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/VirtualFileKotlinClass.kt @@ -54,12 +54,12 @@ public class VirtualFileKotlinClass private constructor( private val perfCounter = PerformanceCounter.create("Binary class from Kotlin file") @Deprecated("Use KotlinBinaryClassCache") - fun create(file: VirtualFile): VirtualFileKotlinClass? { + fun create(file: VirtualFile, fileContent: ByteArray?): VirtualFileKotlinClass? { return perfCounter.time { assert(file.getFileType() == JavaClassFileType.INSTANCE) { "Trying to read binary data from a non-class file $file" } try { - val byteContent = file.contentsToByteArray(false) + val byteContent = fileContent ?: file.contentsToByteArray(false) if (!byteContent.isEmpty()) { return@time FileBasedKotlinClass.create(byteContent) { name, header, innerClasses -> diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileIndexes.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileIndexes.kt index 8610bb4667c..506027a41bd 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileIndexes.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileIndexes.kt @@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.vfilefinder import com.intellij.ide.highlighter.JavaClassFileType import com.intellij.openapi.diagnostic.Logger -import com.intellij.openapi.vfs.VirtualFile import com.intellij.util.indexing.* import com.intellij.util.io.KeyDescriptor 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 java.io.DataInput import java.io.DataOutput -import java.util.Collections +import java.util.* abstract class KotlinFileIndexBase(private val classOfIndex: Class) : ScalarIndexExtension() { public val KEY: ID = ID.create(classOfIndex.canonicalName) @@ -50,10 +49,10 @@ abstract class KotlinFileIndexBase(private val classOfIndex: Class) : Scal override fun getKeyDescriptor() = KEY_DESCRIPTOR - protected fun indexer(f: (VirtualFile) -> FqName?): DataIndexer = + protected fun indexer(f: (FileContent) -> FqName?): DataIndexer = DataIndexer { try { - val fqName = f(it.file) + val fqName = f(it) if (fqName != null) { Collections.singletonMap(fqName, null) } @@ -78,8 +77,8 @@ public object KotlinClassFileIndex : KotlinFileIndexBase(K private val VERSION = 2 - private val INDEXER = indexer() { file -> - val kotlinClass = KotlinBinaryClassCache.getKotlinBinaryClass(file) + private val INDEXER = indexer() { fileContent -> + val kotlinClass = KotlinBinaryClassCache.getKotlinBinaryClass(fileContent.file, fileContent.content) if (kotlinClass != null && kotlinClass.classHeader.isCompatibleAbiVersion) kotlinClass.classId.asSingleFqName() else null } } @@ -94,7 +93,9 @@ public object KotlinJavaScriptMetaFileIndex : KotlinFileIndexBase - if (file.fileType == KotlinJavaScriptMetaFileType) JsMetaFileUtils.getClassFqName(file) else null + private val INDEXER = indexer() { fileContent -> + if (fileContent.fileType == KotlinJavaScriptMetaFileType) JsMetaFileUtils.getClassFqName(fileContent.file) else null } + + override fun dependsOnFileContent(): Boolean = false }