net: reduce ASMap lookup and address grouping costs #36994

Open
l0rinc wants to merge 6 commits from l0rinc/asmap-performance into master AGit
l0rinc commented 2026-11-27 10:07:21 +00:00 (Migrated from github.com)

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).

**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) ```text 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).
DrahtBot commented 2026-11-27 10:07:32 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36994 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36994.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36994#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) 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](https://github.com/bitcoin/bitcoin/pull/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) * [#36434](https://github.com/bitcoin/bitcoin/pull/36434) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36434.svg"></sub> (http: support unix sockets by pinheadmz) * [#36033](https://github.com/bitcoin/bitcoin/pull/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-->
First-time contributor

Review failed because required verification did not complete.

Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.

Review coverage was incomplete.


Reviewed: 4131eaa53f53 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:806938d40e279b6d701b2e78128bb1a09b55193a0b697245a1dce6241d3081c1:3607c760b63c01fb7f1f349adfc3fd0614b148a7df02e42f2f0edc2d448925a1 --> <!-- ralph-base:0629982adad837abb5521f07aa7f269abdc610f6 --> <!-- ralph-head:4131eaa53f530d2a94e3bb98cf2c2962ef869d4b --> <!-- ralph-case:c2540c2b193328d6ef4c6c39ec76c84976054e22b6f820b4ca20b45cb310aedf --> Review failed because required verification did not complete. Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments. Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`4131eaa53f53`](<https://git.fish.foo/bitcoin/bitcoin/commit/4131eaa53f530d2a94e3bb98cf2c2962ef869d4b>) · [Review report](<https://review.fish.foo/report/36994-4131eaa53f530d2a94e3bb98cf2c2962ef869d4b-3249-2.html>) · [Previous reports](<https://review.fish.foo/traces/36994/>) [permalink](<https://review.fish.foo/report/36994-4131eaa53f530d2a94e3bb98cf2c2962ef869d4b-3249-2.html>)
l0rinc commented 2026-12-04 14:41:07 +00:00 (Migrated from github.com)

@fjahr / @hodlinator, you may be interested in this since it optimizes the byte-based ASMap decoder.
It also removes redundant address classification in the GetMappedAS/GetGroup path, avoids the temporary lookup allocation, and reserves the small group vector. The full series made AddrManAdd 19.2% faster on a Rpi5 (and I'd argue the code is also simpler).

@fjahr / @hodlinator, you may be interested in this since it optimizes the byte-based ASMap decoder. It also removes redundant address classification in the `GetMappedAS`/`GetGroup` path, avoids the temporary lookup allocation, and reserves the small group vector. The full series made `AddrManAdd` 19.2% faster on a Rpi5 (and I'd argue the code is also simpler).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36994
No description provided.