@@ -14,7 +14,9 @@ import com.intellij.psi.PsiFile
1414import com.intellij.util.gist.GistManager
1515import com.intellij.util.io.DataExternalizer
1616import kotlinx.coroutines.TimeoutCancellationException
17+ import kotlinx.coroutines.async
1718import kotlinx.coroutines.runBlocking
19+ import kotlinx.coroutines.withContext
1820import kotlinx.coroutines.withTimeout
1921import kotlinx.coroutines.yield
2022import org.jetbrains.annotations.VisibleForTesting
@@ -39,6 +41,7 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.model.Supplemental
3941import java.io.DataInput
4042import java.io.DataOutput
4143import java.util.Collections
44+ import kotlin.coroutines.coroutineContext
4245
4346private val contentRootPathProvider = CopyContentRootPathProvider ()
4447
@@ -140,7 +143,7 @@ class DefaultCodeWhispererFileContextProvider(private val project: Project) : Fi
140143 return @withTimeout supplementalContext?.let {
141144 if (it.contents.isNotEmpty()) {
142145 val logStr = buildString {
143- append(" Successfully fetched supplemental context with strategy ${it.strategy} . " )
146+ append(" Successfully fetched supplemental context with strategy ${it.strategy} with ${it.latency} ms " )
144147 it.contents.forEachIndexed { index, chunk ->
145148 append(
146149 """
@@ -180,7 +183,6 @@ class DefaultCodeWhispererFileContextProvider(private val project: Project) : Fi
180183 }
181184
182185 override suspend fun extractCodeChunksFromFiles (psiFile : PsiFile , fileProducers : List <suspend (PsiFile ) -> List <VirtualFile >>): List <Chunk > {
183- val parseFilesStart = System .currentTimeMillis()
184186 val hasUsed = Collections .synchronizedSet(mutableSetOf<VirtualFile >())
185187 val chunks = mutableListOf<Chunk >()
186188
@@ -196,13 +198,11 @@ class DefaultCodeWhispererFileContextProvider(private val project: Project) : Fi
196198 chunks.addAll(file.toCodeChunk(relativePath))
197199 hasUsed.add(file)
198200 if (chunks.size > CodeWhispererConstants .CrossFile .CHUNK_SIZE ) {
199- LOG .debug { " finish fetching ${CodeWhispererConstants .CrossFile .CHUNK_SIZE } chunks in ${System .currentTimeMillis() - parseFilesStart} ms" }
200201 return chunks.take(CodeWhispererConstants .CrossFile .CHUNK_SIZE )
201202 }
202203 }
203204 }
204205
205- LOG .debug { " finish fetching ${CodeWhispererConstants .CrossFile .CHUNK_SIZE } chunks in ${System .currentTimeMillis() - parseFilesStart} ms" }
206206 return chunks.take(CodeWhispererConstants .CrossFile .CHUNK_SIZE )
207207 }
208208
@@ -215,18 +215,42 @@ class DefaultCodeWhispererFileContextProvider(private val project: Project) : Fi
215215
216216 val query = generateQuery(targetContext)
217217
218- val projectContext = if (CodeWhispererFeatureConfigService .getInstance().getInlineCompletion()) {
219- fetchProjectContext(query, psiFile, targetContext)
218+ val projectContextDeferred = if (CodeWhispererFeatureConfigService .getInstance().getInlineCompletion()) {
219+ withContext(coroutineContext) {
220+ async {
221+ val t0 = System .currentTimeMillis()
222+ val r = fetchProjectContext(query, psiFile, targetContext)
223+ val t1 = System .currentTimeMillis()
224+ LOG .debug { " fetchProjectContext cost ${t1 - t0} ms" }
225+
226+ r
227+ }
228+ }
220229 } else {
221230 null
222231 }
223232
224- val openTabsContext = fetchOpentabsContext(query, psiFile, targetContext)
233+ val openTabsContextDeferred = withContext(coroutineContext) {
234+ async {
235+ val t0 = System .currentTimeMillis()
236+ val r = fetchOpentabsContext(query, psiFile, targetContext)
237+ val t1 = System .currentTimeMillis()
238+ LOG .debug { " fetchOpenTabsContext cost ${t1 - t0} ms" }
225239
226- return if (projectContext == null || projectContext.contents.isEmpty()) {
227- openTabsContext
240+ r
241+ }
242+ }
243+
244+ if (projectContextDeferred == null ) {
245+ return openTabsContextDeferred.await()
228246 } else {
229- projectContext
247+ val projectContext = projectContextDeferred.await()
248+ return if (projectContext.contents.isNotEmpty()) {
249+ projectContext
250+ } else {
251+ val openTabsContext = openTabsContextDeferred.await()
252+ openTabsContext
253+ }
230254 }
231255 }
232256
@@ -275,7 +299,6 @@ class DefaultCodeWhispererFileContextProvider(private val project: Project) : Fi
275299 // step 2: bm25 calculation
276300 val timeBeforeBm25 = System .currentTimeMillis()
277301 val top3Chunks: List <BM25Result > = BM250kapi (first60Chunks.map { it.content }).topN(query)
278- LOG .info { " Time ellapsed for BM25 algorithm: ${System .currentTimeMillis() - timeBeforeBm25} ms" }
279302
280303 yield ()
281304
0 commit comments