Skip to content

perf(txpool): use RLock in GetByID to allow concurrent pool reads#36

Open
simonzg wants to merge 2 commits into
mainnetfrom
perf/txpool-lock-fix
Open

perf(txpool): use RLock in GetByID to allow concurrent pool reads#36
simonzg wants to merge 2 commits into
mainnetfrom
perf/txpool-lock-fix

Conversation

@simonzg

@simonzg simonzg commented May 1, 2026

Copy link
Copy Markdown
Contributor

Why

txObjectMap.GetByID is a read-only map lookup, but it was acquiring an exclusive write lock (sync.RWMutex.Lock). This prevented any concurrent reads: Get, GetTxObj, and the Contains fast-path in add() all route through GetByID and were serialized one-at-a-time regardless of load.

On a busy node receiving many transactions simultaneously, every lookup — including the duplicate-check at the top of add() — blocked every other lookup. The sync.RWMutex is specifically designed to allow multiple concurrent readers, but only if RLock is used.

What Changed

GetByID: m.lock.Lock() / m.lock.Unlock()m.lock.RLock() / m.lock.RUnlock()

One line change. No behaviour change — txObjMap is a plain map and map reads are safe under RLock by Go's memory model.

Expected Impact

  • Concurrent transaction lookups (Get, GetTxObj, pool duplicate checks) no longer serialize behind a write lock.
  • Higher throughput during bursts of incoming transactions from multiple goroutines.
  • The Add method already uses a proper write lock and does its own duplicate check internally, so correctness is unaffected.

simonzg and others added 2 commits July 10, 2025 09:54
…ncurrent reads

GetByID was acquiring an exclusive write lock (sync.RWMutex.Lock) for a
read-only map lookup. This prevented concurrent reads and serialized every
pool lookup: Get, GetTxObj, and the Contains fast-path in add() all funnel
through GetByID and were forced to run one-at-a-time.

Change to RLock/RUnlock so parallel reads can proceed concurrently.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant