perf(sp): eliminate per-tx DB iterator reads in tweaks.subscribe - #3
Open
sethforprivacy wants to merge 3 commits into
Open
perf(sp): eliminate per-tx DB iterator reads in tweaks.subscribe#3sethforprivacy wants to merge 3 commits into
sethforprivacy wants to merge 3 commits into
Conversation
Author
|
CI triage:
🤖 Generated with Claude Code |
Author
|
The pre-existing 🤖 Generated with Claude Code |
The scan hot loop resolved the tweak spend-cache height with a fresh RocksDB iterator scan once per transaction row, which dominates scan latency on dense blocks (thousands of iterator creations per block). - Resolve cache state once per block height with a memoized HashMap entry. - Switch the cache-height lookup from iter_scan to a direct point read. - Extract P2TR xonly keys straight from the script bytes (OP_1 <32 bytes>) instead of building + splitting the full script-to-asm string per vout. - Iterate stored vout data by reference; only clone when a stale spend cache requires a lookup_spend refresh (rare, self-healing path). Output wire format is byte-for-byte unchanged.
- Iterate the tweaks range scan straight off the snapshot-consistent RocksDB iterator instead of collecting the entire requested range into a Vec before streaming. A dense historical-mode request can hold hundreds of thousands of fully-deserialized rows in memory and delays the first streamed block until the whole range has been read. Arc-cloning the query handle keeps `self` free for send_values(). - Borrow TweakData in place: get_tweak_data() deep-cloned every vout script and spend record once per transaction row. Wire output is unchanged: same rows, same order, same JSON. The RocksDB iterator is snapshot-consistent, so the mid-scan spend-cache writebacks observe the same view the collected Vec did. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
p2tr_pubkey_hex took &bitcoin::Script, but under the liquid feature script_pubkey is elements::Script, breaking the test-liquid CI build. Take the raw script bytes instead — identical logic for both types. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
sethforprivacy
force-pushed
the
sp-scan-fast-path
branch
from
August 15, 2026 19:58
381c927 to
735e156
Compare
KarimMokhtar
approved these changes
Aug 15, 2026
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.
Problem
Cake Wallet's on-device ("hardcore") Silent Payments sync is dominated by server-side per-candidate-transaction work in
blockchain.tweaks.subscribe. For every tweak row streamed, the hot loop:get_tweak_cached_height), while the main range scan runs withfill_cache(false)so these random reads never warm the block cache. A full-history scan streams ~4.03M candidate txs (~115.6M inhistoricalmode); at 1–3 ms per cold random SST read this alone accounts for hours of scan time;script_pubkey.to_asm()string per vout just to recover the taproot key;tweak.vout_dataper tx even when the spend cache was current.Live measurements against
electrs.cakewallet.com(cake-update-v1 @ cf9e03b): ~3–4 ms marginal cost per candidate tx cold vs ~0.2 ms warm (~20×); 0.44 s/block over a 300-block cold dense window, which projects to a 5–17 h serial full-history sync.Fix
HashMapentry instead of once per transaction (~60× fewer reads on dense blocks).iter_scan(...).next()inget_tweak_cached_heightwith a direct pointget()— same fixed-length 5-byte key the write path (store_tweak_cache_height→put_sync) uses, so lookups are exact-match equivalent.OP_1 <32 bytes>=0x51 0x20 …), falling back to the previous asm-split path for anything non-P2TR.vout_databy reference; clone only on the stale-spend-cache path.The
row_height < last_blockchain_height - 5guard is rewritten asrow_height + 5 < last_blockchain_height— equivalent for all real tips, but no longer underflowsu32on very short (regtest) chains.Second commit: streaming + last per-tx clone
An independent optimization pass added commit 2:
collect()ing the entire requested range into aVecbefore streaming — a dense historical-mode request otherwise holds hundreds of thousands of fully-deserialized rows in memory per connection and delays the first streamed block until the whole range has been read. The query handle isArc-cloned so the iterator doesn't borrowself.get_tweak_data()cloned every vout script and spend record once per transaction row; the tweak data is now borrowed in place.Wire compatibility
Output is byte-for-byte unchanged, chain-validated twice on regtest:
aae16bf): identical normalizedtweaks.subscribepayloads for all SP transactions, including a genuine BIP-352 payment (built with rust-silentpayments send math) that both the productionsp_scannerpath and the session scanner found identically.Notes
cargo build --release --features silent-payments. Reminder: the tweaks RPC only exists when thesilent-paymentsfeature is enabled — worth pinning in the deployment CI/Dockerfile.sp-scan-fast-path-v1is the commit-1 change rebased ontocake-update-v1(basecf9e03b, the binary currently serving production) for a drop-in redeploy of the live box if we want the win before the v2 index cutover; commit 2 can be cherry-picked there too if needed.countcap (1000) so clients can amortize the ~0.3 s fixed per-request overhead over larger ranges.🤖 Generated with Claude Code