From cd0142c3ce25ec65a3345399c5eb0cd70ddae80d Mon Sep 17 00:00:00 2001 From: Konstantin Ulitin Date: Wed, 25 Apr 2018 20:35:21 +0300 Subject: [PATCH] JS debugger: lazy source resolver --- .../src/debugger/sourcemap/SourceResolver.kt | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/platform/script-debugger/backend/src/debugger/sourcemap/SourceResolver.kt b/platform/script-debugger/backend/src/debugger/sourcemap/SourceResolver.kt index f04f67c6ff24..85e1b28dfb9b 100644 --- a/platform/script-debugger/backend/src/debugger/sourcemap/SourceResolver.kt +++ b/platform/script-debugger/backend/src/debugger/sourcemap/SourceResolver.kt @@ -6,7 +6,6 @@ import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.vfs.StandardFileSystems import com.intellij.openapi.vfs.VfsUtilCore import com.intellij.openapi.vfs.VirtualFile -import com.intellij.util.ArrayUtil import com.intellij.util.Url import com.intellij.util.Urls import com.intellij.util.containers.ObjectIntHashMap @@ -14,18 +13,6 @@ import com.intellij.util.containers.isNullOrEmpty import com.intellij.util.io.URLUtil import java.io.File -inline fun SourceResolver(rawSources: List, sourceContents: List?, urlCanonicalizer: (String) -> Url): SourceResolver { - return SourceResolver(rawSources, Array(rawSources.size) { urlCanonicalizer(rawSources[it]) }, sourceContents) -} - -fun SourceResolver(rawSources: List, - trimFileScheme: Boolean, - baseUrl: Url?, - sourceContents: List?, - baseUrlIsFile: Boolean = true): SourceResolver { - return SourceResolver(rawSources, sourceContents) { canonicalizeUrl(it, baseUrl, trimFileScheme, baseUrlIsFile) } -} - interface SourceFileResolver { /** * Return -1 if no match @@ -34,19 +21,33 @@ interface SourceFileResolver { fun resolve(rawSources: List): Int = -1 } -class SourceResolver(private val rawSources: List, val canonicalizedUrls: Array, private val sourceContents: List?) { +class SourceResolver(private val rawSources: List, + private val sourceContents: List?, + urlCanonicalizer: (String, String?) -> Url) { companion object { fun isAbsolute(path: String) = path.startsWith('/') || (SystemInfo.isWindows && (path.length > 2 && path[1] == ':')) } - private val canonicalizedUrlToSourceIndex: ObjectIntHashMap = if (SystemInfo.isFileSystemCaseSensitive) ObjectIntHashMap(rawSources.size) else ObjectIntHashMap(rawSources.size, Urls.caseInsensitiveUrlHashingStrategy) - - init { - for (i in rawSources.indices) { - canonicalizedUrlToSourceIndex.put(canonicalizedUrls[i], i) + val canonicalizedUrls: Array by lazy { Array(rawSources.size) { urlCanonicalizer(rawSources[it], sourceContents?.get(it)) } } + private val canonicalizedUrlToSourceIndex: ObjectIntHashMap by lazy { + ( + if (SystemInfo.isFileSystemCaseSensitive) ObjectIntHashMap(rawSources.size) + else ObjectIntHashMap(rawSources.size, Urls.caseInsensitiveUrlHashingStrategy) + ).also { + for (i in rawSources.indices) { + it.put(canonicalizedUrls[i], i) + } } } + constructor(rawSources: List, + trimFileScheme: Boolean, + baseUrl: Url?, + sourceContents: List?, + baseUrlIsFile: Boolean = true) + : this(rawSources, sourceContents, { sourceUrl, _ -> canonicalizeUrl(sourceUrl, baseUrl, trimFileScheme, baseUrlIsFile) }) + + fun getSource(entry: MappingEntry): Url? { val index = entry.source return if (index < 0) null else canonicalizedUrls[index] @@ -68,7 +69,7 @@ class SourceResolver(private val rawSources: List, val canonicalizedUrls return if (sourceIndex < 0 || sourceIndex >= sourceContents!!.size) null else sourceContents[sourceIndex] } - fun getSourceIndex(url: Url) = ArrayUtil.indexOf(canonicalizedUrls, url) + fun getSourceIndex(url: Url) = canonicalizedUrlToSourceIndex[url] fun getRawSource(entry: MappingEntry): String? { val index = entry.source