Skip to content

net: precompute rfc6724policyTable in addrselect #54032

@Thorleon

Description

@Thorleon

As net package has one of the biggest init time in standard library, I have tried to improve performance by doing two things in net/addrselect.go:

  1. Precompute slice with RFC rules. Currently the rules are computed and sorted in init() function. We could save the time and allocations by using prepopulated values in sorted manner. The rules haven't changed since 2015. To be extra safe we could move order validation as test case. It should slightly speed up startup of each binary with "net" package and go dns resolver. It also saves 38 allocations, ~50% of allocations in init phase of net module.
  2. Replace internal net.IP usage with netip.Addr in sortByRFC6724 function. It results in ~40% performance improvement on samples from tests.

The only risk is the difference between net.IP and netip.Addr behaviour.

Init benchmark:

name    old time/op    new time/op    delta
Init-8    2.14µs ± 0%    0.12µs ± 0%   ~     (p=1.000 n=1+1)

name    old alloc/op   new alloc/op   delta
Init-8    1.05kB ± 0%    0.38kB ± 0%   ~     (p=1.000 n=1+1)

name    old allocs/op  new allocs/op  delta
Init-8      39.0 ± 0%       1.0 ± 0%   ~     (p=1.000 n=1+1)

Whole sortByRFC6724 function benchmark:

name               old time/op  new time/op  delta
SortByRFC6724/0-8   479ns ± 0%   290ns ± 0%   ~     (p=1.000 n=1+1)
SortByRFC6724/1-8   482ns ± 0%   291ns ± 0%   ~     (p=1.000 n=1+1)
SortByRFC6724/2-8   489ns ± 0%   297ns ± 0%   ~     (p=1.000 n=1+1)
SortByRFC6724/3-8   580ns ± 0%   367ns ± 0%   ~     (p=1.000 n=1+1)
SortByRFC6724/4-8   934ns ± 0%   574ns ± 0%   ~     (p=1.000 n=1+1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions