Skip to content

Conversation

martinus
Copy link
Contributor

@martinus martinus commented Aug 2, 2023

This simplifies the serialization code a bit and should also make it a bit faster.

  • use fold expressions instead of recursive calls. This simplifies the code, makes it most likely faster because it reduces the number of function calls, and compiles faster because there are fewer template instantiations.

  • use if constexpr instead of unnecessarily creating a temporary object only to call the right overload. This is used for std::vector and prevector serialization.

@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 2, 2023

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

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK MarcoFalke, jonatack, sipa, john-moffett

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

Copy link
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.

This looks like a very nice cleanup (and in a widely included and large file).

Have started running a node on this branch.

Initial approach ACK 513f465

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

very nice, ACK 513f465 🙊

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: very nice, ACK 513f4659b0a2ac4e2454ad2425b0fec8d7f64f04 🙊
GEzCyPu57ooJeHDaJ2Nuwgozpyvft2GzjmZzH1LsuCmxm4NLtc+ONqj570HEbSuzwFAnZ/pZOvN9NSTjIWsxDw==

Serialize_impl(os, v, T());
void Serialize(Stream& os, const prevector<N, T>& v)
{
if constexpr (std::is_same_v<T, unsigned char>) {
Copy link
Member

@maflcko maflcko Aug 3, 2023

Choose a reason for hiding this comment

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

review note in 10105d9: A simple is_same_v works, because I presume T can never include const in a prevector, similar to how std::vector<const int> does not exist.

…eMany()

Instead of recursively calling `SerializeMany` and peeling off one
argument at a time, use a fold expression. This simplifies the code,
makes it most likely faster because it reduces the number of function
calls, and compiles faster because there are fewer template
instantiations.
…izeMany()

Instead of recursively calling `UnserializeMany` and peeling off one
argument at a time, use a fold expression. This simplifies the code,
makes it most likely faster because it reduces the number of function
calls, and compiles faster because there are fewer template
instantiations.
This gets rid of unnecessarily creating a temporary object T() to call
the right function.
This gets rid of unnecessarily creating a temporary object T() to call
the right function.
This gets rid of unnecessarily creating a temporary object T() to call
the right function.
This gets rid of unnecessarily creating a temporary object T() to call
the right function.
@fanquake fanquake requested a review from john-moffett August 3, 2023 08:35
@martinus martinus force-pushed the 2023-08-simplify-serialization branch from 513f465 to f054bd0 Compare August 3, 2023 08:46
@martinus
Copy link
Contributor Author

martinus commented Aug 3, 2023

Rebased to f054bd0 to clang-format the changes, and adds back the && in UnserializeMany.

@maflcko
Copy link
Member

maflcko commented Aug 3, 2023

only change is to add a missing &. lgtm, re-ACK f054bd0 📦

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: only change is to add a missing `&`. lgtm, re-ACK f054bd072afb72d8dae7adc521ce15c13b236700 📦
qVTNWRmP4fK55EiOWjNDYq7LnIwUAwWHe96FjQdkKJV0imPWgVxuLQVRwAbyPEiKRxWUArJKVfajpftB3UDzCw==

@DrahtBot DrahtBot requested a review from jonatack August 3, 2023 12:01
@jonatack
Copy link
Member

jonatack commented Aug 3, 2023

ACK f054bd0

@DrahtBot DrahtBot removed the request for review from jonatack August 3, 2023 17:47
@sipa
Copy link
Member

sipa commented Aug 3, 2023

utACK f054bd0

Copy link
Contributor

@john-moffett john-moffett left a comment

Choose a reason for hiding this comment

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

ACK f054bd0

@fanquake fanquake merged commit f138422 into bitcoin:master Aug 4, 2023
@martinus martinus deleted the 2023-08-simplify-serialization branch August 4, 2023 15:38
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 9, 2023
f054bd0 refactor: use "if constexpr" in std::vector's Unserialize() (Martin Leitner-Ankerl)
088caa6 refactor: use "if constexpr" in std::vector's Serialize() (Martin Leitner-Ankerl)
0fafaca refactor: use "if constexpr" in prevector's Unserialize() (Martin Leitner-Ankerl)
c8839ec refactor: use "if constexpr" in prevector's Serialize() (Martin Leitner-Ankerl)
1403d18 refactor: use fold expressions instead of recursive calls in UnserializeMany() (Martin Leitner-Ankerl)
bd08a00 refactor: use fold expressions instead of recursive calls in SerializeMany() (Martin Leitner-Ankerl)

Pull request description:

  This simplifies the serialization code a bit and should also make it a bit faster.

  * use fold expressions instead of recursive calls. This simplifies the code, makes it most likely faster because it reduces the number of function calls, and compiles faster because there are fewer template instantiations.

  * use `if constexpr` instead of unnecessarily creating a temporary object only to call the right overload. This is used for `std::vector` and `prevector` serialization.

ACKs for top commit:
  MarcoFalke:
    only change is to add a missing `&`. lgtm, re-ACK f054bd0 📦
  jonatack:
    ACK f054bd0
  sipa:
    utACK f054bd0
  john-moffett:
    ACK f054bd0

Tree-SHA512: 0417bf2d6be486c581732297945449211fc3481bac82964e27628b38ef55a47dfa58d730148aeaf1b19fa8eb1076489cc646ceebb178162a9afa59034601501d
@bitcoin bitcoin locked and limited conversation to collaborators Aug 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants