Cache virtualFilePath for KtFile

Because of com.intellij.openapi.vfs.local.CoreLocalVirtualFile#getPath
that has a rather slow implementation calling String::replace on each call

At the same time this method gets called very frequently when
recording lookups
This commit is contained in:
Denis Zharkov
2017-10-04 12:52:36 +03:00
parent bb6a52c0cc
commit 20334a2321
2 changed files with 14 additions and 1 deletions
@@ -32,7 +32,7 @@ class KotlinLookupLocation(val element: KtElement) : LookupLocation {
if (containingJetFile.doNotAnalyze != null) return null
return object : LocationInfo {
override val filePath = containingJetFile.virtualFile.path
override val filePath = containingJetFile.virtualFilePath
override val position: Position
get() = getLineAndColumnInPsiFile(containingJetFile, element.textRange).let { Position(it.line, it.column) }
@@ -50,6 +50,9 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) :
@Volatile
private var hasTopLeveCallables: Boolean? = null
@Volatile
private var pathCached: String? = null
val importList: KtImportList?
get() = findChildByTypeOrClass(KtStubElementTypes.IMPORT_LIST, KtImportList::class.java)
@@ -104,6 +107,15 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) :
return result
}
val virtualFilePath
get(): String {
pathCached?.let { return it }
return virtualFile.path.also {
pathCached = it
}
}
val isScriptByTree: Boolean
get() = script != null
@@ -170,6 +182,7 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) :
super<PsiFileBase>.clearCaches()
isScript = null
hasTopLeveCallables = null
pathCached = null
}
fun isScript(): Boolean = stub?.isScript() ?: isScriptByTree