refactor: serialization simplifications #28203

pull martinus wants to merge 6 commits into bitcoin:master from martinus:2023-08-simplify-serialization changing 1 files +63 −124
  1. martinus commented at 6:20 pm on August 2, 2023: contributor

    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.

  2. DrahtBot commented at 6:20 pm on August 2, 2023: contributor

    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.

  3. DrahtBot added the label Refactoring on Aug 2, 2023
  4. in src/serialize.h:994 in 513f4659b0 outdated
    1007-}
    1008-
    1009-template<typename Stream>
    1010-inline void UnserializeMany(Stream& s)
    1011+template<typename Stream, typename... Args>
    1012+void SerializeMany(Stream& s, const Args&... args)
    


    jonatack commented at 8:44 pm on August 2, 2023:
    0637f53afbd9f1357ce92270f4d13a5f891d1657 question: you’ve followed the existing code, but I wonder if there is any reason why SerializeMany isn’t inline like the neighboring methods.

    martinus commented at 5:58 am on August 3, 2023:
    inline as a way to nudge the compiler into optimizing things is nowadays pretty useless, and as far as I know compilers simply ignore it anyways and do their own thing. Inlining only makes sense to mark implementations in a header so you won’t get duplicate symbols at link time, but for templates that’s not necessary because they are inline by default. So I’d say its best to remove all inline from any template function as it won’t make any difference.
  5. jonatack commented at 8:58 pm on August 2, 2023: member

    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 513f4659b0a2ac4e2454ad2425b0fec8d7f64f04

  6. in src/serialize.h:1042 in 0637f53afb outdated
    1044-{
    1045-}
    1046-
    1047-template<typename Stream, typename Arg, typename... Args>
    1048-void SerializeMany(Stream& s, const Arg& arg, const Args&... args)
    1049+template<typename Stream, typename... Args>
    


    maflcko commented at 7:21 am on August 3, 2023:
    0637f53afbd9f1357ce92270f4d13a5f891d1657: forgot clang-format on touched code?

    martinus commented at 8:27 am on August 3, 2023:
    yes, I’ll clang-format the changes
  7. in src/serialize.h:1049 in 48adef0e41 outdated
    1051-}
    1052-
    1053-template<typename Stream, typename Arg, typename... Args>
    1054-inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args)
    1055+template<typename Stream, typename... Args>
    1056+inline void UnserializeMany(Stream& s, Args&... args)
    


    maflcko commented at 7:24 am on August 3, 2023:

    48adef0e41700e4b5d0072c71ffaa2387cdeccbb: Any reason to change this to disallow temporaries (&&)?

    Also this commit can be squashed in the previous one?

    Also can remove inline. (template enforces inline already)


    martinus commented at 8:27 am on August 3, 2023:

    Ah, actually I didn’t think about temporaries at all… I changed it to be more in line with Serialize which uses const Args&... args, so Args&... args felt more appropriate.

    I’ll change it back to &&

  8. in src/serialize.h:748 in 10105d97f4 outdated
    767-inline void Serialize(Stream& os, const prevector<N, T>& v)
    768-{
    769-    Serialize_impl(os, v, T());
    770+void Serialize(Stream& os, const prevector<N, T>& v)
    771+{
    772+    if constexpr (std::is_same_v<T, unsigned char>) {
    


    maflcko commented at 7:31 am on August 3, 2023:
    review note in 10105d97f412cc54ba4031c56538f7e0c611251f: 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.
  9. maflcko approved
  10. maflcko commented at 7:35 am on August 3, 2023: member

    very nice, ACK 513f4659b0a2ac4e2454ad2425b0fec8d7f64f04 🙊

    Signature:

    0untrusted 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}"
    1RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
    2trusted comment: very nice, ACK 513f4659b0a2ac4e2454ad2425b0fec8d7f64f04 🙊
    3GEzCyPu57ooJeHDaJ2Nuwgozpyvft2GzjmZzH1LsuCmxm4NLtc+ONqj570HEbSuzwFAnZ/pZOvN9NSTjIWsxDw==
    
  11. refactor: use fold expressions instead of recursive calls in SerializeMany()
    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.
    bd08a008b4
  12. refactor: use fold expressions instead of recursive calls in UnserializeMany()
    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.
    1403d181c1
  13. refactor: use "if constexpr" in prevector's Serialize()
    This gets rid of unnecessarily creating a temporary object T() to call
    the right function.
    c8839ec5cd
  14. refactor: use "if constexpr" in prevector's Unserialize()
    This gets rid of unnecessarily creating a temporary object T() to call
    the right function.
    0fafaca4d3
  15. refactor: use "if constexpr" in std::vector's Serialize()
    This gets rid of unnecessarily creating a temporary object T() to call
    the right function.
    088caa68fb
  16. refactor: use "if constexpr" in std::vector's Unserialize()
    This gets rid of unnecessarily creating a temporary object T() to call
    the right function.
    f054bd072a
  17. fanquake requested review from john-moffett on Aug 3, 2023
  18. martinus force-pushed on Aug 3, 2023
  19. martinus commented at 8:47 am on August 3, 2023: contributor
    Rebased to f054bd0 to clang-format the changes, and adds back the && in UnserializeMany.
  20. maflcko commented at 12:01 pm on August 3, 2023: member

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

    Signature:

    0untrusted 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}"
    1RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
    2trusted comment: only change is to add a missing `&`. lgtm, re-ACK f054bd072afb72d8dae7adc521ce15c13b236700 📦
    3qVTNWRmP4fK55EiOWjNDYq7LnIwUAwWHe96FjQdkKJV0imPWgVxuLQVRwAbyPEiKRxWUArJKVfajpftB3UDzCw==
    
  21. DrahtBot requested review from jonatack on Aug 3, 2023
  22. jonatack commented at 5:47 pm on August 3, 2023: member
    ACK f054bd072afb72d8dae7adc521ce15c13b236700
  23. DrahtBot removed review request from jonatack on Aug 3, 2023
  24. sipa commented at 8:07 pm on August 3, 2023: member
    utACK f054bd072afb72d8dae7adc521ce15c13b236700
  25. john-moffett approved
  26. john-moffett commented at 8:12 pm on August 3, 2023: contributor
    ACK f054bd072afb72d8dae7adc521ce15c13b236700
  27. fanquake merged this on Aug 4, 2023
  28. fanquake closed this on Aug 4, 2023

  29. martinus deleted the branch on Aug 4, 2023
  30. sidhujag referenced this in commit 75c1b9d802 on Aug 9, 2023
  31. bitcoin locked this on Aug 3, 2024

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-09-28 22:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me