1 minor finding · 6 suggestions
Findings
[minor] · Sparse output indices defeat KeyHash() collision randomization
In a small or freshly wiped shard, a mined nonstandard transaction can create a 256-entry chain regardless of the salt; no timing impact has been measured.
Location: src/coinsstore.h:894 (head).
Details and suggested fix
KeyHash() now computes H(txid) + n, and Map selects (H(txid) + n) & (B - 1). The bucket count grows with retained entries, not output indices. Unspendable OP_RETURN outputs consume indices without entering the store, and every output of a transaction shares a shard. For example, keep spendable outputs at indices 0, 256, ..., 65280 and fill the gaps with OP_RETURN. In an otherwise empty shard, all 256 retained outputs stay in one chain through every resize up to B = 256. This roughly 650 kB transaction fits the consensus weight limit. Even one insertion probe per output implies 32,640 node comparisons, and later lookups can traverse 256 nodes under the shard mutex. The base's keyed whole-outpoint hash had no such deterministic relationship.
The output-count and weight limits bound this construction to about 256 colliding entries per transaction, and further shard growth separates them. Prefer hashing (txid, n >> 4) with the salt and then adding n & 15. That retains adjacent placement within sixteen-output groups while salting distant groups. Add a sparse-output probe-count test.
Optional improvements
[suggestion] · The undo shortcut leaves its isolation precondition unchecked
No current caller supplies undo data for a window block, but ApplyChanges() leaves its isolation precondition unchecked even though the shortcut bypasses stale-coin detection.
Location: src/coinsstore.cpp:2374 (head).
Details and suggested fix
ApplyBatch() feeds a supplied undo coin directly to SpendLocked() on a miss, bypassing ApplySpend()'s base read and shard.settles retry. If a lower block has already spent the coin and its written marker has been erased, matching height and coinbase fields do not reveal that the undo coin is stale. The shortcut would record that spend without a conflict. PublishConnectTip() supplies undo only for successful non-window blocks, while workers and continuations use the argument-free form. Since staging marks window views with SetKeepBaseReads(true), assert !m_keep_base_reads whenever undo is supplied, or use an equivalent explicit tip-only check. This preserves existing callers and rejects the invalid mode combination at the API boundary.
[suggestion] · Synchronous node-region faults can hold the shared depot mutex
When synchronous refill returns untouched memory, writing its region number can fault under the shared depot mutex and delay other appliers; this platform-dependent latency effect has not been measured.
Location: src/coinsstore.h:792 (head).
Details and suggested fix
Take() formerly made its explicit first-touch writes in the linking loop after releasing the depot mutex. The new region-number memcpy() runs before that unlock. NodeRegion() allocates synchronously below 1 GiB and whenever no prepared region is ready; HintedRegion() requests allocation and huge-page advice but does not prefault the memory. If the kernel must supply the first page here, other threads cannot Take() or Give() existing lists during that fault, potentially including huge-page reclaim or compaction. A prepared region avoids this, and the header write occurs once per 32 MiB region. Restructure synchronous refill to prepare and prefault the region outside the depot mutex before installing and numbering it under the lock. Adding a touch inside the current NodeRegion() call would still hold that mutex.
[suggestion] · The coinsahead handoff and shutdown paths lack deterministic tests
The added node_numbers_span_regions test cannot activate coinsahead by itself and has no handoff or active-worker teardown assertions, leaving those lifecycle paths without targeted regression coverage.
Location: src/coinsstore.cpp:141 (head).
Details and suggested fix
NodeRegion() gates worker creation on a fixed 1 GiB process-wide region counter. The numbering test creates 700,000 cells, well below that threshold by itself, and checks values and lists rather than worker ownership transitions. Other tests could activate the worker incidentally because the counter persists, but that does not deterministically exercise the handoff and shutdown schedules. Add a test-only activation seam and an allocation barrier. Use them to check that a ready region is consumed exactly once, synchronous fallback works while preparation is in flight, last-store destruction handles both ready memory and an allocation in progress, and a new store can restart preparation, including overlapping-store cases. The production threshold and policy can remain unchanged.
[suggestion] · CollectChunk() makes an unnecessary owning StoredCoin copy
Collecting an unspent dirty coin with a script over 35 bytes now allocates and copies an intermediate StoredCoin before constructing CoinWrite's Coin, while holding the shard mutex.
Location: src/coinsstore.h:660 (head).
Details and suggested fix
std::make_optional(entry.coin) now deduces std::optional<StoredCoin> rather than std::optional<Coin>. Its copy constructor calls CopyFrom() and SetScript(), allocating a separate heap script. Conversion to the unchanged CoinWrite::coin then copies those bytes again through ToCoin(), and the temporary owner is freed. The base copied directly into a Coin. Use std::make_optional<Coin>(entry.coin) in CollectChunk() to preserve the write's independent ownership without the intermediate allocation. This affects uncommon long scripts, such as uncompressed P2PK or bare multisig; no throughput impact has been measured.
[suggestion] · The coinsahead commit also carries an independent API-doc correction
Dropping the region-ahead optimization would also discard the correction to an existing pinned-block budget, unnecessarily coupling a documentation fix to an optional performance change.
Location: src/kernel/bitcoinkernel.h:1308 at 6842f7f81883 (commit) (commit).
Details and suggested fix
6842f7f8188304ce284352bb3047c0b09b6cc66e (the region-ahead thread) changes src/kernel/bitcoinkernel.h both to correct the notification-pinned-block budget and to document the new prepared 32 MiB region. At its direct parent, 58d2a6baefa9bddbad0454380eefe26ad3771cb5 (pending-transaction hints), src/util/byte_ledger.h already states the eighth-of-coins-cache budget with a 64 MiB minimum. The comparison base already enforces it, with the reorganisation exception. Move the budget correction into a documentation-only commit before that parent, and leave only the new 32 MiB clause in the region-thread commit. The final text stays the same, while independently evaluating or dropping the region-thread optimization no longer loses an unrelated API-doc fix.
[suggestion] · CoinsStore's class contract now precedes StoredCoinData
The store's locking, visibility and AccessCoin() lifetime contract now precedes StoredCoinData rather than CoinsStore, making the class documentation misleading.
Location: src/coinsstore.h:86 (head).
Details and suggested fix
The inserted StoredCoinData and StoredCoin definitions separate the existing “young tier of the UTXO set” comment from class CoinsStore by roughly 170 lines. That contract describes sharded mutation, write-out runs, per-block records and reference lifetime, not the byte-only helper immediately following it. Move the existing store comment to immediately before class CoinsStore. The helper definitions can remain in their required dependency order.
Design and approach
Make retention of the region-ahead thread and less-supported prefetch tuning conditional on representative-scale throughput, memory and latency comparisons with the store and window baseline. The reported 1.02x–1.09x cache-pool gain measures a different change; spend prefetch and the threshold-zero thread run reported no harness gain. Representative results should justify the added worker and prepared 32 MiB region rather than assume mainnet-scale gains.