perf: cache txid() and hash() to avoid redundant serialize + SHA256#171
Open
ALiberalVoluntarist wants to merge 1 commit into
Open
perf: cache txid() and hash() to avoid redundant serialize + SHA256#171ALiberalVoluntarist wants to merge 1 commit into
ALiberalVoluntarist wants to merge 1 commit into
Conversation
txid() previously recomputed serialize() + double SHA256 on every call. In BEEF workflows with deep ancestor chains (depth=100), the same tx objects have txid() called 3-6 times across chain building, topology sorting, MP resolution, and verification — all redundant after the first computation. Add _cached_hash and _cached_txid fields with invalidation on add_input(), add_output(), sign(), and _apply_fee_amount(). Deserialized transactions (from_reader/from_hex/from_beef) are effectively immutable, so the cache persists for their entire lifetime. Benchmark results (depth=100 chain, 5 walks): Cached: 0.05 ms vs Uncached: 1.90 ms → 38x speedup Single tx 1000 calls: 73x speedup Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Test Coverage Report📊 Coverage: 84.7% View the full coverage report in the build artifacts. |
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
txid()previously recomputedserialize()+ double SHA256 on every call with no cachingtxid()called 3–6 times across chain building, topology sorting, MP resolution, and verification — all redundant after the first computation_cached_hash/_cached_txidfields toTransactionwith invalidation on mutation methods (add_input,add_output,sign,_apply_fee_amount)Benchmark results
Test plan
TestTxidCache— correctness (7 tests) + benchmarks (2 tests)hash256(serialize())add_input(),add_output()from_reader/from_hexstarts with clean cachetxid()returns same object identity (is)🤖 Generated with Claude Code