Optimize event verification and database performance#124
Merged
greenart7c3 merged 1 commit intomasterfrom Apr 24, 2026
Merged
Conversation
- Enable WAL journal mode and PRAGMA synchronous=NORMAL / temp_store=MEMORY on both AppDatabase and HistoryDatabase so readers don't block writers and each commit issues fewer fsyncs. - Verify event signature before any DB read in verifyEvent() so invalid events are rejected without paying for 3-6 SQLite reads. Reorder the pure (non-DB) checks to run first. - Drop the unconditional deletetags() call at the top of insertEventWithTags(). It was wasted I/O for every new event and orphaned the existing row's tags when the INSERT hit an IGNORE conflict. - Collapse delete(ids, pubkey) from a per-id loop into a single DELETE ... WHERE id IN (:ids), relying on the existing FK ON DELETE CASCADE on TagEntity.pkEvent for tag cleanup.
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
This PR reorders event verification logic to fail fast on invalid events before database access, and optimizes database configuration for improved performance.
Key Changes
Event Verification Order (CustomWebSocketServer.kt)
event.verify()) before any database queriesEvent Deletion Optimization (EventDao.kt)
delete()method to use a single SQL query withON DELETE CASCADEinstead of iterating through IDsdeleteById()helper methoddeletetags()call ininsertOrUpdateEvent()- tags are now handled transactionally via foreign key constraintsDatabase Performance Tuning (AppDatabase.kt)
PRAGMA synchronous=NORMALfor balanced durability/performancePRAGMA temp_store=MEMORYto use memory for temporary tablesImplementation Details
https://claude.ai/code/session_01GXehQxQhBepAvtfbNfTHpN