Optimize event batch import with parallel verification and transaction batching#123
Merged
greenart7c3 merged 3 commits intomasterfrom May 4, 2026
Merged
Conversation
Route the download-your-events flow through a new bulk-import path that streams events directly into Room instead of accumulating them twice in memory and paying per-event overhead designed for live WebSocket clients. - EventDownloader: drop the in-memory receivedEvents list and ConcurrentSet buffer, stream each EOSE batch into a suspend sink, fan out relays concurrently under a Semaphore(3), ensure isImportingEvents is reset in a finally block, and remove hot-path per-frame logging. - CustomWebSocketServer: add innerProcessEventBatch that verifies signatures in parallel on Dispatchers.Default, batches duplicate and kind-5 deletion checks with chunked IN (...) queries, collapses in-batch replaceables to the newest per (pubkey, kind[, d-tag]), and writes everything in one Room transaction. Extract applyDeleteTagsOwnedBy so both paths share the same delete-tag logic. - EventDao: add existingIds, getDeletedEventsByIds, deleteTagsForIds, multi-row insertEvents, and insertEventsWithTagsBatch. https://claude.ai/code/session_01VP6tzr2EBN7dY4R9BTwLXr
The relay's WebSocket thread kept appending to the batch buffer while innerProcessEventBatch was iterating it (distinctBy/toList), producing a ConcurrentModificationException. Guard the buffer with a lock and atomically swap it for a fresh one before handing the previous batch to the sink. https://claude.ai/code/session_01VP6tzr2EBN7dY4R9BTwLXr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactored the event download and import pipeline to significantly improve performance and memory efficiency when importing large event archives. The changes introduce parallel signature verification, batch database operations, and a new streaming sink pattern for bounded memory usage.
Key Changes
EventDownloader.kt
fetchAllEventsByUserPaginated()to accept asinkcallback for processing completed batches, eliminating the need to accumulate all events in memory before writingdownloadAllEventsByUser()to fetch from multiple relays concurrently using coroutineasync/awaitAll()with a semaphore limiting concurrency to 3 relaysConcurrentSet<Event>with a simpleArrayListbuffer and changed event counting from storing full events to tracking received countfinishedRelaystracking mapsCustomWebSocketServer.kt
innerProcessEventBatch()optimized for bulk imports with:Dispatchers.Defaultwith semaphore-controlled concurrencyIN (...)queriespolicyAllows()helper for consistent event filteringdeleteEvent()to use newapplyDeleteTagsOwnedBy()helper for reuse in batch processingEventDao.kt
insertEvents()and new batch helperinsertEventsWithTagsBatch()for efficient multi-event insertsexistingIds()query to check which events already existgetDeletedEventsByIds()to identify events deleted by kind-5 deletion eventsinsertEventsWithTagsBatch()transaction handles tag cleanup and event/tag insertion atomicallyNotable Implementation Details
innerProcessEvent) unchangedhttps://claude.ai/code/session_01VP6tzr2EBN7dY4R9BTwLXr