Skip to content

net: reduce ASMap lookup and address grouping costs - #36994

Open
l0rinc wants to merge 6 commits into
bitcoin:masterfrom
l0rinc:l0rinc/asmap-performance
Open

l0rinc wants to merge 6 commits into
bitcoin:masterfrom
l0rinc:l0rinc/asmap-performance

Conversation

@l0rinc

@l0rinc l0rinc commented Nov 27, 2026

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

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.
@DrahtBot DrahtBot added the P2P label Nov 27, 2026
@DrahtBot

DrahtBot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36675 (bugfix: send shortened IPv6 addresses to SOCKS5 proxies in full by instagibbs)
  • #36434 (http: support unix sockets by pinheadmz)
  • #36033 ([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.

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.
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).
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.
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).
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants