Skip to content

Improvements to Witness type#4

Closed
dr-orlovsky wants to merge 8 commits intoRCasatta:witness_with_blockfrom
BP-WG:scripts/witness
Closed

Improvements to Witness type#4
dr-orlovsky wants to merge 8 commits intoRCasatta:witness_with_blockfrom
BP-WG:scripts/witness

Conversation

@dr-orlovsky
Copy link
Copy Markdown

No description provided.

RCasatta and others added 8 commits October 5, 2021 15:22
Witness struct is in place of the Vec<Vec<u8>> we have before this commit.
Implementation of Default, Iterator and others allows to have similar behaviour but using a single Vec prevent many allocations during deserialization which in turns results in better performance, even 20% better perfomance on recent block.
}

/// Gets nth element from the witness stack. Does not allocate
pub fn get(&self, index: usize) -> Option<&[u8]> {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

If we really need Index (I want to see what you and other think of the comment rust-bitcoin#672 (comment)) I think we can just use something like self.iter().nth(index) (also the iterator is not allocating)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Why not to keep the whole map of indexes as a part of the struct itself? This will make indexing performance much better.

Indexes are needed for circumstances like "get the previous to last item in witness stack" useful in Taproot witness parsing (for instance "if there is more than two stack elems after annex removal, get the second to the last as a script")


impl ToHex for Witness {
fn to_hex(&self) -> String {
let mut vec = Vec::with_capacity(self.content.len());
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

self.content.len() should be self.serialized_len()

@RCasatta RCasatta force-pushed the witness_with_block branch 2 times, most recently from be017cf to 5df226f Compare December 1, 2021 11:56
@RCasatta RCasatta force-pushed the witness_with_block branch 3 times, most recently from ba224cf to 106acdc Compare December 28, 2021 09:12
RCasatta pushed a commit that referenced this pull request Feb 21, 2025
f3c80ea Use concrete type for all_zeros call (Tobin C. Harding)

Pull request description:

  Currently we use the `Hash` trait in a bunch of places to call `all_zeros`. We are attempting to improve the `hashes` API and this usage is both unnecessary and also hindering that effort.

  Use the concrete type (e.g. `BlockHash`) instead of calling through the trait method.

  Refactor only, no logic changes.

ACKs for top commit:
  apoelstra:
    ACK f3c80ea I contend that this meets one-ACK carve-out #4 "code moves that do not change the API"

Tree-SHA512: a8d7ba48cf6816b722d626ed0a9a7ccfeee2dff19ef689c78661e9afff1f9053a53752562c70c201e33f8e979a2ea9d14660b36d3f732c0f37c327a062514919
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.

2 participants