Skip to content

Commit 71451b9

Browse files
committed
calc openTabsContext & projectContext concurrently and patch aws#4978
1 parent 5ab3d11 commit 71451b9

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererFileContextProvider.kt

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import com.intellij.psi.PsiFile
1414
import com.intellij.util.gist.GistManager
1515
import com.intellij.util.io.DataExternalizer
1616
import kotlinx.coroutines.TimeoutCancellationException
17+
import kotlinx.coroutines.async
1718
import kotlinx.coroutines.runBlocking
19+
import kotlinx.coroutines.withContext
1820
import kotlinx.coroutines.withTimeout
1921
import kotlinx.coroutines.yield
2022
import org.jetbrains.annotations.VisibleForTesting
@@ -39,6 +41,7 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.model.Supplemental
3941
import java.io.DataInput
4042
import java.io.DataOutput
4143
import java.util.Collections
44+
import kotlin.coroutines.coroutineContext
4245

4346
private 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

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextController.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import kotlinx.coroutines.CoroutineScope
1717
import kotlinx.coroutines.launch
1818
import software.aws.toolkits.core.utils.getLogger
1919
import software.aws.toolkits.core.utils.warn
20-
import software.aws.toolkits.jetbrains.settings.CodeWhispererSettings
2120

2221
@Service(Service.Level.PROJECT)
2322
class ProjectContextController(private val project: Project, private val cs: CoroutineScope) : Disposable {
@@ -26,9 +25,7 @@ class ProjectContextController(private val project: Project, private val cs: Cor
2625

2726
init {
2827
cs.launch {
29-
if (CodeWhispererSettings.getInstance().isProjectContextEnabled()) {
30-
encoderServer.downloadArtifactsAndStartServer()
31-
}
28+
encoderServer.downloadArtifactsAndStartServer()
3229
}
3330

3431
project.messageBus.connect(this).subscribe(

0 commit comments

Comments
 (0)