Extracted the `Gson()` instantiation in `ChatViewModel.kt` to a companion object to avoid creating a new `Gson` instance on every `ToolStart` and `ToolComplete` event handler call. This change reduces garbage collection pressure by caching reflection setup, type adapters, and factories. Impact: - Reduces memory allocations and CPU cycles during multi-step tool chains and streaming. - Safely maintains existing parsing behavior while addressing the `PERF-03` review finding. Closes #271
This commit is contained in:
1 file changed
+6
-2
@@ -460,7 +460,7 @@ class ChatViewModel(
|
||||
var orphanToPersist: ChatMessage? = null
|
||||
val sessionId = _uiState.value.currentSessionId
|
||||
|
||||
val contentJson = event.data?.let { Gson().toJson(it) } ?: ""
|
||||
val contentJson = event.data?.let { gson.toJson(it) } ?: ""
|
||||
val toolMessage =
|
||||
ChatMessage(
|
||||
role = MessageRole.TOOL,
|
||||
@@ -509,7 +509,7 @@ class ChatViewModel(
|
||||
it.toolStatus == ToolStatus.RUNNING
|
||||
}
|
||||
if (toolIdx >= 0) {
|
||||
val contentJson = event.data?.let { Gson().toJson(it) } ?: ""
|
||||
val contentJson = event.data?.let { gson.toJson(it) } ?: ""
|
||||
val updated =
|
||||
messages[toolIdx].copy(
|
||||
toolStatus = ToolStatus.COMPLETED,
|
||||
@@ -1219,4 +1219,8 @@ class ChatViewModel(
|
||||
pendingCleanupJob?.cancel()
|
||||
wsClient.disconnect()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val gson = Gson()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user