Skip to content
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

Implement iter::size_hint and ExactSizeIterator for Witness Iter #1053

Merged
merged 1 commit into from Jun 16, 2022

Conversation

RCasatta
Copy link
Collaborator

@RCasatta RCasatta commented Jun 13, 2022

close #1050

I don't think we need to change the collect since it use the size_hint() lower bound to initially allocate

// with size_hint
// test blockdata::witness::benches::bench_big_witness_to_vec ... bench: 313 ns/iter (+/- 13)
// test blockdata::witness::benches::bench_witness_to_vec ... bench: 204 ns/iter (+/- 11)

// without
// test blockdata::witness::benches::bench_big_witness_to_vec ... bench: 489 ns/iter (+/- 28)
// test blockdata::witness::benches::bench_witness_to_vec ... bench: 221 ns/iter (+/- 102)

The reason why the small witness doesn't get big perf boost is because by default vec allocates 4 slots

@RCasatta RCasatta marked this pull request as draft June 13, 2022 13:55
@RCasatta RCasatta marked this pull request as ready for review June 13, 2022 14:24
@@ -43,7 +43,7 @@ pub struct Witness {
}

/// Support structure to allow efficient and convenient iteration over the Witness elements
pub struct Iter<'a>(::core::slice::Iter<'a, u8>);
pub struct Iter<'a>(::core::slice::Iter<'a, u8>, usize);
Copy link
Contributor

Choose a reason for hiding this comment

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

If it is already two fields, I would name them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure, I added a small test also

@RCasatta RCasatta force-pushed the witness_size_hint branch 2 times, most recently from 1149c35 to 52014d7 Compare June 13, 2022 17:28
dpc
dpc previously approved these changes Jun 13, 2022
tcharding
tcharding previously approved these changes Jun 13, 2022
Copy link
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK 52014d7

@@ -43,7 +43,10 @@ pub struct Witness {
}

/// Support structure to allow efficient and convenient iteration over the Witness elements
pub struct Iter<'a>(::core::slice::Iter<'a, u8>);
pub struct Iter<'a> {
inner: ::core::slice::Iter<'a, u8>,
Copy link
Member

Choose a reason for hiding this comment

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

nit: We don't need these leading colons anymore, since the MSRV bump.

Copy link
Member

Choose a reason for hiding this comment

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

Will hold off on merging since I'd like this to have a chance to be addressed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

apoelstra
apoelstra previously approved these changes Jun 15, 2022
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 52014d7 nice!

Kixunil
Kixunil previously approved these changes Jun 15, 2022
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.

LOL at first I thought small ones got worse, I'm actually surprised they didn't. (Would be happy to see an explanation if anyone knows.)

ACK 52014d7

// with size_hint
// test blockdata::witness::benches::bench_big_witness_to_vec              ... bench:         313 ns/iter (+/- 13)
// test blockdata::witness::benches::bench_witness_to_vec                  ... bench:         204 ns/iter (+/- 11)

// without
// test blockdata::witness::benches::bench_big_witness_to_vec              ... bench:         489 ns/iter (+/- 28)
// test blockdata::witness::benches::bench_witness_to_vec                  ... bench:         221 ns/iter (+/- 102)
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.

ACK ec8dada

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK ec8dada

@apoelstra apoelstra merged commit e7a3bdd into rust-bitcoin:master Jun 16, 2022
@RCasatta RCasatta deleted the witness_size_hint branch June 16, 2022 14:01
ChallengeDev210 pushed a commit to ChallengeDev210/rust-bitcoin that referenced this pull request Aug 1, 2022
…xactSizeIterator for Witness Iter

ec8dada Implement iter::size_hint and ExactSizeIterator for Witness Iter (Riccardo Casatta)

Pull request description:

  close rust-bitcoin/rust-bitcoin#1050

  I don't think we need to change the `collect` since it use the `size_hint()` lower bound to initially allocate

  // with size_hint
  // test blockdata::witness::benches::bench_big_witness_to_vec              ... bench:         313 ns/iter (+/- 13)
  // test blockdata::witness::benches::bench_witness_to_vec                  ... bench:         204 ns/iter (+/- 11)

  // without
  // test blockdata::witness::benches::bench_big_witness_to_vec              ... bench:         489 ns/iter (+/- 28)
  // test blockdata::witness::benches::bench_witness_to_vec                  ... bench:         221 ns/iter (+/- 102)

  The reason why the small witness doesn't get big perf boost is because by default vec allocates 4 slots

ACKs for top commit:
  Kixunil:
    ACK ec8dada
  apoelstra:
    ACK ec8dada

Tree-SHA512: dbe09ba6ebd4014fe0639412894beedab6cc7e844a5ec1697af8f0694b62ae5d423a801df1b48ac7029444c01877975e2d5168728f038fbd4f5808bda90e0f2f
@Kixunil Kixunil added the perf Performance optimizations/issues label Aug 1, 2022
@Kixunil Kixunil added this to the 0.29.0 milestone Aug 1, 2022
@Kixunil Kixunil added the minor API Change This PR should get a minor version bump label Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor API Change This PR should get a minor version bump perf Performance optimizations/issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize Witness::to_vec to use known lenght for reserving.
5 participants