Skip to content

Implement Clone for SighashCache #4075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

BEULAHEVANJALIN
Copy link

Fixes #4068


Summary

  • Implemented Clone for SighashCache
  • Benchmarked before and after adding Clone
  • Added specific benchmarks to measure cloning performance

Benchmark Results

Benchmark Before After Change
SighashCache Creation 30.9 - 35.6 ns 28.1 - 28.3 ns ~9% Faster
Access SighashCache 297.4 - 301.4 ps 297.6 - 299.5 ps No Significant Change
Clone SighashCache (New Test) 28.9 - 29.1 ns Efficient Cloning

Key Notes

  • SighashCache creation improved by ~9%
  • No regression in access performance (~300 ps)
  • Cloning is efficient (~29 ns), supporting multi-threaded use cases
  • All tests pass; benchmarks confirm no degradation

Please verify correctness and confirm alignment with project goals.
Open to any feedback or further refinements.

@github-actions github-actions bot added the C-bitcoin PRs modifying the bitcoin crate label Feb 18, 2025
@coveralls
Copy link

coveralls commented Feb 18, 2025

Pull Request Test Coverage Report for Build 13416368245

Details

  • 50 of 72 (69.44%) changed or added relevant lines in 2 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.04%) to 82.806%

Changes Missing Coverage Covered Lines Changed/Added Lines %
bitcoin/src/crypto/sighash.rs 45 47 95.74%
bitcoin/src/psbt/mod.rs 5 25 20.0%
Files with Coverage Reduction New Missed Lines %
bitcoin/src/psbt/mod.rs 3 80.44%
Totals Coverage Status
Change from base Build 13413734123: 0.04%
Covered Lines: 21243
Relevant Lines: 25654

💛 - Coveralls

@apoelstra
Copy link
Member

Appears to be built on an outdated version of #4071. Please rebase on master (which should fix CI).

@tcharding
Copy link
Member

tcharding commented Feb 19, 2025

Thanks for the contribution!

  • Appreciate your effort with testing; I don't think we need to test clone though since its derived. Same with test_sighash_cache_clone_performance.
  • You have some formatting changes mixed in (we use nightly to run the formatter, and formatting is not enforced by CI)
  • We don't commit the Cargo.lock file - not sure how you got that in there since its in gitignore
  • Finally can you squash commits down into discrete changes please

@tcharding
Copy link
Member

tcharding commented Feb 19, 2025

I"m getting merge commits on the tip of this now mate. You want to squash the whole thing down into a single patch and force push. Holla if you can't work it out - here to help.

EDIT: Ignore that, the 'Benchmark SighashCache before adding Clone' as a separate patch is a good idea. Then perhaps the rest as a single patch.

@BEULAHEVANJALIN
Copy link
Author

Thanks for the feedback @tcharding!
Apologies for the merge commits and any confusion on my part. I now understand the approach:

  • Keep 'Benchmark SighashCache before adding Clone' as a separate patch.
  • Squash the rest into a single commit.
  • Force push the updated branch.

I'll get this sorted and push the changes soon. Let me know if anything else needs adjusting. Thanks for your patience!

Implement Clone for SighashCache

Benchmark SighashCache after adding Clone

Trigger CI rebuild

Update Cargo.lock with the latest dependencies

Update dependencies and fix deprecated Txid usage

Benchmark SighashCache before adding Clone

Squashed changes:
- Implement Clone for SighashCache
- Benchmark SighashCache after adding Clone
- Remove unnecessary CI rebuild commit
- Remove Cargo.lock (not committed)
- Update dependencies and fix deprecated Txid usage
- General cleanup
@BEULAHEVANJALIN
Copy link
Author

@tcharding @apoelstra I have squashed all my commits (except the first one) as mentioned and rebased the branch on the latest master. But still some tests are failing.

Could you help me understand if these failures are related to my changes or something upstream? Let me know if I need to make further adjustments. Apologies for any inconvenience caused, and thanks for your time!

@yancyribbens
Copy link
Contributor

@BEULAHEVANJALIN I think it would simplify the process if you added Criterion in a different PR and focus on closing #4068

@@ -40,6 +40,7 @@ base64 = { version = "0.22.0", optional = true }
# `bitcoinconsensus` version includes metadata which indicates the version of Core. Use `cargo tree` to see it.
bitcoinconsensus = { version = "0.106.0", default-features = false, optional = true }
serde = { version = "1.0.103", default-features = false, features = [ "derive", "alloc" ], optional = true }
criterion = "0.5.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a dev dependency. see bellow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! Lemme try.

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected this PR to be a single line change - adding Clone to the list of derives. The only other useful change in the PR I see is removing &mut but that's not really related.

black_box(cloned_cache);
})
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire benchmark is meaningless. The first one benchmarks setting a few registers to zero/pointer, the second benchmarks doing nothing (passing a reference is not accessing; and even if it was, there's no point), the third one benchmarks cloning.

It's comparing apples to oranges. We don't need such a test. We never benchmark clone impls and I see no reason to believe we need to benchmark them here.

#[derive(Debug)]
pub struct SighashCache<T: Borrow<Transaction>> {
#[derive(Debug, Clone)]
pub struct SighashCache<T: Borrow<Transaction> + Clone> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That additional Clone bound shouldn't be needed and it's harmful. The derive macro inserts its own and forcing it prevents &mut Transaction from being used which specifically goes against the intent of BorrowMut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bitcoin PRs modifying the bitcoin crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SighashCache is missing Clone
6 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy