perf(chat): optimize O(N^2) sync merge loop with O(1) hash map lookups (#798)

Replaces the `indexOfFirst` linear scan over the unmerged incoming messages with a `HashMap` mapping message IDs to their index. This reduces the time complexity of the server message reconciliation loop from O(N^2) to O(N). Additionally, `removeAt()` calls on the unmerged list (which carry an O(N) penalty due to array shifting) were replaced by setting matched indices in a `MutableList<ChatMessage?>` to `null`.
This commit is contained in:
M57 authored and GitHub committed 2026-08-04 18:13:38 +03:00
1 parent 6d8cf09d9e
commit 0724d0104d
2 files changed
+22 -10

No files matched your search

@@ -93,6 +93,7 @@ class ChatViewModelTest {
mockConnectionStatus.value = ConnectionStatus.DISCONNECTED
every { AuthManager.getToken() } returns "test-token"
every { AuthManager.getBaseUrl() } returns "http://test.local/"
every { AuthManager.getSelectedProfileId() } returns null
every { AuthManager.isTypingEffectEnabled() } returns true
every { AuthManager.getTypingEffectDelayMs() } returns 30
@@ -1187,7 +1188,6 @@ class ChatViewModelTest {
// unless another test class happened to init it earlier in the JVM
// (the order-dependent flakiness). Stub it for determinism — same
// pattern as E2eIntegrationTest / ApiClientTest.
every { AuthManager.getBaseUrl() } returns "http://test.local/"
if (success) {
coEvery {
mockApi.getSessions(any(), any(), any())
@@ -1518,7 +1518,6 @@ class ChatViewModelTest {
// on the REST-success path (mockkObject spy fall-through → real
// uninitialized AuthManager throws). Stub it — same pattern as
// stubSession456Rests / E2eIntegrationTest.
every { AuthManager.getBaseUrl() } returns "http://test.local/"
// Stub the transcript fetch explicitly (relaxed mocks return null
// and muddy the retry path) and capture which session id it is