net: reduce ASMap lookup and address grouping costs #36994

pull l0rinc wants to merge 6 commits into bitcoin:master from l0rinc:l0rinc/asmap-performance changing 4 files +74 −39
  1. l0rinc commented at 10:07 AM on November 27, 2026: contributor

    Problem: Addrman groups peer addresses by network prefix or (when ASMap finds a match) by autonomous system. Without ASMap, grouping classifies the address twice. With ASMap, an IP lookup allocates a temporary key and the decoder calculates a shift distance for each mantissa bit. Building the group can also take several allocations for just a few bytes.

    Fix: When ASMap is disabled, skip the extra address classification in GetMappedAS. Build ASMap lookup keys on the stack and decode integers by shifting each bit into the mantissa. Reserve space for the address group before appending its bytes.

    Time to group one IPv4 address: (Raspberry Pi 5, ns per call, lower is better)

                                     With ASMap                    Without ASMap
    master      baseline             ███████████████████▉ 1,982    █████▏ 518
    80a39cd417  Decode integers      ██████████████████▎  1,830    █████▎ 524
    adc7012941  Skip empty map       ██████████████████▎  1,824    ███▉   390
    bbe7b52765  Build lookup key     █████████████████▌   1,744    ████   394
    13e0803197  Reserve group space  ████████████████▍    1,640    ███▏   307
    

    Across the full series, AddrManAdd is 19.2% faster (59.1 to 49.6 ms).

  2. bench: measure ASMap validation and address groups
    Time validation of the embedded ASMap. Measure `GetGroup` for the same IPv4 address with and without a map.
    The existing lookup benchmarks cover `GetMappedAS` without constructing a group.
    867b2a5bcb
  3. DrahtBot added the label P2P on Nov 27, 2026
  4. DrahtBot commented at 10:07 AM on November 27, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36675 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36675.svg"></sub> (bugfix: send shortened IPv6 addresses to SOCKS5 proxies in full by instagibbs)
    • #36033 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36033.svg"></sub> ([wip,nomerge,rfc] build: Require C++23 compiler by maflcko)

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  5. test: cover linked IPv4 lookup and ASN byte order
    Use a single-prefix map with a four-byte ASN to cover the last ASN encoding class.
    Truncating the offset in that class must invalidate the map.
    Native IPv4 and its RFC 6052, RFC 6145, 6to4 and Teredo forms must resolve to the same ASN.
    3feadee569
  6. util: simplify ASMap integer decoding
    Skip classes with a set continuation bit, then shift in the bits of the offset within the selected class.
    Keep returning `INVALID` for an empty class list before reading the selected class size.
    
    On a Raspberry Pi 5 (GCC 13.3, Release), ASMapCheckStandard went from 42.65 to 39.29 ms (8.6% faster) and ASMapGetMappedASMulti went from 2266 to 2081 ns/address (8.9% faster).
    bc2caf6b86
  7. net: return early from GetMappedAS without ASMap
    Return before computing the network class when there is no map to search.
    
    On a Raspberry Pi 5 (GCC 13.3, Release), ASMapGetGroupNoMap went from 523.82 to 389.93 ns/group (34.3% faster) and AddrManAdd went from 59.40 to 50.57 ms (17.5% faster). The grouping benchmark uses one IPv4 address.
    6e05f35aaf
  8. net: avoid temporary allocations in ASMap lookups
    Build the lookup key on the stack. Use the computed network class to select the IPv4 encoding, and copy a native IPv6 address directly into the key.
    
    On a Raspberry Pi 5 (GCC 13.3, Release), ASMapGetMappedASMulti went from 2091 to 1934 ns/address (8.1% faster).
    24be384013
  9. net: reserve space for the address group
    Reserve room for the group tag and up to `ADDR_INTERNAL_SIZE` address bytes. This also covers the four-byte ASN of a mapped group.
    
    On a Raspberry Pi 5 (GCC 13.3, Release), ASMapGetGroupMapped went from 1744.1 to 1639.8 ns/group (6.4% faster) and ASMapGetGroupNoMap went from 394.11 to 307.28 ns/group (28.3% faster). Both grouping benchmarks use one IPv4 address.
    4131eaa53f
  10. l0rinc force-pushed on Nov 27, 2026

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-11-29 05:51 UTC

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