Skip to content

kernel: Use spans instead of vectors for passing block headers to validation functions#30742

Merged
achow101 merged 3 commits intobitcoin:masterfrom
sedited:headersSpan
Sep 3, 2024
Merged

kernel: Use spans instead of vectors for passing block headers to validation functions#30742
achow101 merged 3 commits intobitcoin:masterfrom
sedited:headersSpan

Conversation

@sedited
Copy link
Copy Markdown
Contributor

@sedited sedited commented Aug 28, 2024

Makes it friendlier for potential future users of the kernel library if they do not store the headers in a std::vector, but can guarantee contiguous memory.

Take this opportunity to also change the argument of ImportBlocks previously taking a std::vector to a std::span.

@DrahtBot
Copy link
Copy Markdown
Contributor

DrahtBot commented Aug 28, 2024

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK stickies-v, maflcko, danielabrozzoni, achow101
Concept ACK theuni
Stale ACK jonatack

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Copy link
Copy Markdown
Member

@theuni theuni left a comment

Choose a reason for hiding this comment

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

Concept ACK.

Comment thread src/validation.cpp Outdated
@theuni
Copy link
Copy Markdown
Member

theuni commented Aug 28, 2024

Actually, I read this assuming we'd already migrated away from Span, which isn't the case. So probably best not to start introducing std::spans until that's happened?

@theuni
Copy link
Copy Markdown
Member

theuni commented Aug 28, 2024

Relevant: #29119

@sedited
Copy link
Copy Markdown
Contributor Author

sedited commented Aug 28, 2024

Thanks for the review @theuni!

Updated 65e4404 -> c584c1f (headersSpan_0 -> headersSpan_1, compare)

  • Addressed @theuni's comment, made the span take a const CBlockHeader, which allows dropping the annoying std::span invocations again.

Actually, I read this assuming we'd already migrated away from Span, which isn't the case. So probably best not to start introducing std::spans until that's happened?

There already are users of std::span, so I did not have the impression that I was introducing something novel here. Now that #29071 is merged I would also expect #29119 to move forward soon. But can always revert to Span if that is what reviewers prefer.

Comment thread src/validation.cpp Outdated
@theuni
Copy link
Copy Markdown
Member

theuni commented Aug 28, 2024

Now that #29071 is merged I would also expect #29119 to move forward soon. But can always revert to Span if that is what reviewers prefer.

@maflcko ?

@sedited
Copy link
Copy Markdown
Contributor Author

sedited commented Aug 28, 2024

Updated c584c1f -> 45a9005 (headersSpan_1 -> headersSpan_2, compare)

Copy link
Copy Markdown
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

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

ACK 45a9005

Comment thread src/net_processing.cpp Outdated
@sedited
Copy link
Copy Markdown
Contributor Author

sedited commented Aug 28, 2024

Thanks for the review @jonatack.

Updated 45a9005 -> 797eede (headersSpan_2 -> headersSpan_3, compare)

@jonatack
Copy link
Copy Markdown
Member

ACK 797eede

Doesn't conflict with the changes in #29119 and LGTM provided we're migrating to std::span and modulo the tidy CI iwyu checks on the updated files (the task hasn't run yet at the time of writing this).

Copy link
Copy Markdown
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

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

ACK 797eede

@maflcko
Copy link
Copy Markdown
Member

maflcko commented Aug 30, 2024

Avoids the need for copying by having to wrap the header in a std::vector when just a single block header is passed.

Can you explain this a bit more? In theory there is C++26 with https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2447r6.html , but I don't think any compiler implements it and the code here is compiled in C++20, so I haven't checked if that fixes your problem.

Currently in the code, you are even creating explicit copies via the GetBlockHeader method.

Maybe I am missing something obvious?

@sedited
Copy link
Copy Markdown
Contributor Author

sedited commented Aug 30, 2024

but I don't think any compiler implements it and the code here is compiled in C++20, so I haven't checked if that fixes your problem.

Thanks, you are right, my initial patch avoided copies where possible by explicitly creating the span, but the brace initializers that theuni suggested instead create temporary arrays that copy the headers. I should update the description, or I can revert to what I was doing before. Do you have a preference? I don't think the copies matter all that much either way. The main motiviation for me is that it makes life a bit easier for potential external callers if you use spans over vector references in the interfaces.

@maflcko
Copy link
Copy Markdown
Member

maflcko commented Aug 30, 2024

I think your initial commit also had some calls to GetBlockHeader to create a copy (explicitly), but I agree that it doesn't matter much either way.

Maybe remove it from the motivation, since it doesn't seem to be a goal, and if it was a goal, it would be hard to achieve?

Makes it friendlier for potential future users of the kernel library if
they do not store the headers in a std::vector, but can guarantee
contiguous memory.
Makes it friendlier for potential future users of the kernel library if
they do not store the headers in a std::vector, but can guarantee
contiguous memory.
@sedited
Copy link
Copy Markdown
Contributor Author

sedited commented Aug 30, 2024

797eede -> b31483e (headersSpan_3 -> headersSpan_4, compare)

  • Corrected commit message and pull request description, dropping mentions of copies as per @maflcko's suggestion.
  • Added a commit for also moving to std::span in ImportBlocks.

@maflcko
Copy link
Copy Markdown
Member

maflcko commented Aug 30, 2024

This changes vector to span, and where copies were done previously, they are still done.

ACK b31483e 🖍

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: ACK b31483e32b7d111eeef5fea8ebdc927a32c22bf4 🖍
KluDd3R0iPVwSdNpepkr2LEtfCpZ259JdLgMHassQji86noEGgOSkRAX6C0unxwVFzvv9ROsLYzZd5fV2XJGDw==

Copy link
Copy Markdown
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

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

ACK b31483e

Comment thread src/node/blockstorage.h Outdated
@sedited
Copy link
Copy Markdown
Contributor Author

sedited commented Aug 30, 2024

b31483e -> a2955f0 (headersSpan_4 -> headersSpan_5, compare)

Makes it friendlier for potential future users of the kernel library if
they do not store the headers in a std::vector, but can guarantee
contiguous memory.
@stickies-v
Copy link
Copy Markdown
Contributor

stickies-v commented Aug 30, 2024

re-ACK a2955f0 - no changes except further walking the file path of modernizing variable names.

@DrahtBot DrahtBot requested a review from maflcko August 30, 2024 10:46
@maflcko
Copy link
Copy Markdown
Member

maflcko commented Aug 30, 2024

ACK a2955f0 🕑

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: ACK a2955f09792b6232f3a45aa44a498b466279a8b7 🕑
lTCtNlp6GgzlMVUtGi0UeE37JlHEPqVxzu84w9qah6aDOvdY6VVedlJf9Dorj/Kjkm8MX2kvBnBkv+eI9aifAQ==

Copy link
Copy Markdown
Member

@danielabrozzoni danielabrozzoni left a comment

Choose a reason for hiding this comment

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

ACK a2955f0

@maflcko
Copy link
Copy Markdown
Member

maflcko commented Sep 3, 2024

CI failure looks unrelated and can be ignored: #30797

@achow101
Copy link
Copy Markdown
Member

achow101 commented Sep 3, 2024

ACK a2955f0

@achow101 achow101 merged commit d4b5553 into bitcoin:master Sep 3, 2024
@bitcoin bitcoin locked and limited conversation to collaborators Sep 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

Status: Done or Closed or Rethinking

Development

Successfully merging this pull request may close these issues.

9 participants