Skip to content

Optimize event verification and database performance#124

Merged
greenart7c3 merged 1 commit intomasterfrom
claude/improve-event-write-performance-Y1Jvf
Apr 24, 2026
Merged

Optimize event verification and database performance#124
greenart7c3 merged 1 commit intomasterfrom
claude/improve-event-write-performance-Y1Jvf

Conversation

@greenart7c3
Copy link
Copy Markdown
Owner

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)

  • Moved protected event authentication check to execute earlier in the verification pipeline
  • Moved signature verification (event.verify()) before any database queries
  • This ensures invalid events are rejected without incurring database read costs
  • Maintains the same verification logic, just reordered for efficiency

Event Deletion Optimization (EventDao.kt)

  • Simplified delete() method to use a single SQL query with ON DELETE CASCADE instead of iterating through IDs
  • Removed redundant deleteById() helper method
  • Removed unnecessary deletetags() call in insertOrUpdateEvent() - tags are now handled transactionally via foreign key constraints
  • Added clarifying comments about cascade deletion behavior

Database Performance Tuning (AppDatabase.kt)

  • Enabled Write-Ahead Logging (WAL) journal mode for both AppDatabase and HistoryDatabase
  • Added PRAGMA synchronous=NORMAL for balanced durability/performance
  • Added PRAGMA temp_store=MEMORY to use memory for temporary tables
  • These pragmas improve concurrent access patterns and reduce I/O overhead

Implementation Details

  • The verification reordering maintains security guarantees while improving performance
  • Database cascade deletion leverages Room's default FK behavior, reducing code complexity
  • WAL mode enables better concurrent read/write access patterns typical in server workloads

https://claude.ai/code/session_01GXehQxQhBepAvtfbNfTHpN

- 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.
@greenart7c3 greenart7c3 merged commit aaab47a into master Apr 24, 2026
1 check passed
@greenart7c3 greenart7c3 deleted the claude/improve-event-write-performance-Y1Jvf branch April 24, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants