Skip to content

Commit 5b63ff6

Browse files
committed
refactor(coroutines): remove unnecessary instance creation on class loading for IDEA 242
Previously, the TokenizerImpl instance and the coroutine scope were being created when the corresponding companion objects were accessed. This could potentially lead to unnecessary resource usage. The change ensures that the TokenizerImpl instance and coroutine scope are only initialized when they are actually needed, improving the application's performance and resource management. Specifically: - TokenizerImpl is now lazily initialized only when requested. - LLMCoroutineScope's coroutine scope is initialized at the point of instance creation rather than on class loading. - The AutoDevInputSection now uses a try-catch block to handle the possibility of TokenizerImpl not being available, with appropriate error logging.
1 parent 853dd9d commit 5b63ff6

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInputSection.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
6464
private val logger = logger<AutoDevInputSection>()
6565

6666
val editorListeners = EventDispatcher.create(AutoDevInputListener::class.java)
67-
private var tokenizer: Tokenizer? = null
67+
private var tokenizer: Tokenizer? = try {
68+
lazy { TokenizerImpl.INSTANCE }.value
69+
} catch (e: Exception) {
70+
logger.error("TokenizerImpl.INSTANCE is not available", e)
71+
null
72+
}
73+
6874
var text: String
6975
get() {
7076
return input.text
@@ -168,8 +174,6 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
168174
this@AutoDevInputSection.initEditor()
169175
}
170176
})
171-
172-
tokenizer = TokenizerImpl.INSTANCE
173177
}
174178

175179
fun showStopButton() {

src/main/kotlin/cc/unitmesh/devti/llms/tokenizer/TokenizerImpl.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class TokenizerImpl : Tokenizer {
2323
}
2424

2525
companion object {
26-
val INSTANCE =
27-
ApplicationManager.getApplication().getService(TokenizerImpl::class.java)
26+
val INSTANCE = ApplicationManager.getApplication().getService(TokenizerImpl::class.java)
2827
}
2928
}

src/main/kotlin/cc/unitmesh/devti/util/LLMCoroutineScope.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import kotlinx.coroutines.CoroutineScope
99
import kotlinx.coroutines.SupervisorJob
1010

1111
@Service(Service.Level.PROJECT)
12-
class LLMCoroutineScope(val coroutineScope: CoroutineScope = CoroutineScope(SupervisorJob() + coroutineExceptionHandler)){
12+
class LLMCoroutineScope {
13+
val coroutineScope: CoroutineScope = CoroutineScope(SupervisorJob() + coroutineExceptionHandler)
14+
1315
companion object {
1416
val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
1517
Logger.getInstance(LLMCoroutineScope::class.java).error(throwable)

0 commit comments

Comments
 (0)