Consolidate all uses of the fast range mapping technique in util/fastrange.h #23994

pull sipa wants to merge 3 commits into bitcoin:master from sipa:202201_fastrange changing 7 files +71 −58
  1. sipa commented at 4:55 PM on January 6, 2022: member

    Several places in the codebase use the fast range mapping technique described in https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/, some for 32-bit ranges, some for 64-bit ones.

    Move all of these to util/fastrange.h, and give them a consistent name.

  2. [moveonly] Move MapIntoRange() to separate util/fastrange.h c6d15c45d9
  3. scripted-diff: rename MapIntoRange to FastRange64
    -BEGIN VERIFY SCRIPT-
    sed -i -e 's/MapIntoRange/FastRange64/' src/blockfilter.cpp src/test/fuzz/golomb_rice.cpp src/util/fastrange.h
    -END VERIFY SCRIPT-
    96ecd6fa3e
  4. DrahtBot added the label Build system on Jan 6, 2022
  5. DrahtBot added the label Utils/log/libs on Jan 6, 2022
  6. Sjors approved
  7. Sjors commented at 11:07 AM on January 7, 2022: member

    ACK b6e8ebc361682ba90cfcdd3af0fd39da85d1ca2a

    nit: funcion -> function in last commit message

  8. in src/util/fastrange.h:14 in b6e8ebc361 outdated
       9 | +
      10 | +// See: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
      11 | +
      12 | +/** Map a value x that is uniformly distributed in the range [0, 2^64) to a
      13 | + *  value uniformly distributed in [0, n) by returning the upper 64 bits of
      14 | + * x * n. */
    


    shaavan commented at 3:04 PM on January 7, 2022:

    A minor nit. Feel free to ignore.

    How about putting the reference link after the explanation?

    /** Map a value x that is uniformly distributed in the range [0, 2^64) to a
     *  value uniformly distributed in [0, n) by returning the upper 64 bits of
     * x * n. */
     
     // See: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
    

    As this convention is followed throughout the codebase and seems more logical.


    sipa commented at 3:10 PM on January 7, 2022:

    That's not a valid doxygen comment.

    I factored out the URL above, because it's more a comment for the entire file, than just that individual function.


    sipa commented at 6:37 PM on January 7, 2022:

    I've rewritten the comments in the last commit.

  9. shaavan approved
  10. shaavan commented at 3:09 PM on January 7, 2022: contributor

    ACK b6e8ebc361682ba90cfcdd3af0fd39da85d1ca2a

    This PR:

    • Renames MapIntoRange -> FastRange64. Moves it to util/fastrange.
    • Renames FastMod -> FastRange32. Moves it to util/fastrange.
    • Uses FastRange32 consistently throughout the codebase.

    I like the refactoring as this makes the functions more consistent in their naming scheme and where they are defined. I checked these functions are being appropriately used wherever possible.

    I had one suggestion, though.

  11. sipa force-pushed on Jan 7, 2022
  12. Add FastRange32 function and use it throughout the codebase efab28b06b
  13. sipa force-pushed on Jan 7, 2022
  14. Sjors commented at 7:19 PM on January 7, 2022: member

    ACK efab28b06bfaa50c41337e84136cb58437e7ba00

  15. shaavan approved
  16. shaavan commented at 11:15 AM on January 8, 2022: contributor

    reACK efab28b06bfaa50c41337e84136cb58437e7ba00

    Changes since my last review:

    • Rewritten documentation in util/fastrange.h.
    • Reordered function definition in util/fastrange.h.

    The new documentation looks a lot better. It’s able to remain concise while also clearly explaining the following code. Also, now the placement of links is a lot more meaningful.

    I prefer the new ordering of functions as it feels more logical, and also it follows lexicographic ordering, so that is a plus. FastRange32 followed by FastRange64.

  17. DrahtBot commented at 6:58 AM on January 10, 2022: member

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #23810 (refactor: destroy all C-style casts; use modern C++ casts, enforce via -Wold-style-cast by PastaPastaPasta)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  18. MarcoFalke removed the label Build system on Jan 10, 2022
  19. MarcoFalke added the label Refactoring on Jan 10, 2022
  20. MarcoFalke approved
  21. MarcoFalke commented at 9:53 AM on January 10, 2022: member

    review ACK efab28b06bfaa50c41337e84136cb58437e7ba00 🍸

    <details><summary>Show signature</summary>

    Signature:

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA512
    
    review ACK efab28b06bfaa50c41337e84136cb58437e7ba00 🍸
    -----BEGIN PGP SIGNATURE-----
    
    iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
    pUhOvAwAjcxhK6fe5biykR3KX9cq8SXp3ipAgZVHUdSrzB/iWtZBBFWOEIgMKSqB
    ohj9G24GyaS4Do6O7AKdclWFzcjOh+3XxEJZvKKYvMF5fiu1Xh3FyNvbArS4MBdl
    0WkGfc3IMMAFLowO7YdKt11EJQipd93ujnP/6Zc+SopytTWGz4j6VPDMWiRKAvXi
    sxk7nGbCgdotTKUo4jFx6CVFvUBuZNYDKO4pzzpJETrRX+VAkD7Fb7JQSQOoy2mP
    HSvVXhMz3DcGEIEkoae2mes5+JsxH7sbD71V1GIsIdpuiTyA4tVyBZp5bqyoaFRA
    HqE1j87MkUXLtre8pkRECONriruOOHQLTnfwdlMC5WH4AwPE+CvrhTae8d/iIe4b
    xNRtVf+SV36WU72JwejRnL2+0YwmE8TkSudm8ctG5c9pWttOSVUyFoLhdCiXRz+U
    Of34WyEpvZO637cqD8no2zbXM3sBjacU+qV0LvYY3p/Kko4qZDDQDUH4A1ip2KI+
    QXlBjryM
    =2LES
    -----END PGP SIGNATURE-----
    

    </details>

  22. MarcoFalke merged this on Jan 10, 2022
  23. MarcoFalke closed this on Jan 10, 2022

  24. sidhujag referenced this in commit a3cd5163eb on Jan 10, 2022
  25. DrahtBot locked this on Jan 10, 2023

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: 2026-04-13 15:14 UTC

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