Skip to content

netlink: reduce allocations in marshalMessages - #311

Merged
nickgarlis merged 1 commit into
mdlayher:mainfrom
nickgarlis:reduce-allocs
Jul 20, 2026
Merged

netlink: reduce allocations in marshalMessages#311
nickgarlis merged 1 commit into
mdlayher:mainfrom
nickgarlis:reduce-allocs

Conversation

@nickgarlis

Copy link
Copy Markdown
Collaborator

Reduce allocations in marshalMessages by summing their sizes and then allocating a single buffer to write directly to via a new marshalInto method.

                       │  before       │              after                   │
                       │    sec/op     │    sec/op     vs base                │
MarshalMessages/1-12      60.35µ ±  4%   33.61µ ±  3%  -44.32% (p=0.000 n=10)
MarshalMessages/8-12     1537.5µ ±  4%   330.9µ ± 11%  -78.47% (p=0.000 n=10)
MarshalMessages/64-12    15.403m ±  5%   1.294m ±  3%  -91.60% (p=0.000 n=10)
MarshalMessages/512-12    74.14m ± 17%   11.84m ±  4%  -84.03% (p=0.000 n=10)
geomean                   3.208m         642.5µ        -79.98%

                       │  before       │              after                   │
                       │     B/op      │     B/op      vs base                │
MarshalMessages/1-12     144.00Ki ± 0%   72.00Ki ± 0%  -50.00% (p=0.000 n=10)
MarshalMessages/8-12     2760.0Ki ± 0%   520.0Ki ± 0%  -81.16% (p=0.000 n=10)
MarshalMessages/64-12    25.719Mi ± 0%   4.008Mi ± 0%  -84.42% (p=0.000 n=10)
MarshalMessages/512-12   200.43Mi ± 0%   32.01Mi ± 0%  -84.03% (p=0.000 n=10)
geomean                   6.648Mi        1.463Mi       -78.00%

                       │  before      │             after                  │
                       │  allocs/op   │ allocs/op   vs base                │
MarshalMessages/1-12       2.000 ± 0%   1.000 ± 0%  -50.00% (p=0.000 n=10)
MarshalMessages/8-12      15.000 ± 0%   1.000 ± 0%  -93.33% (p=0.000 n=10)
MarshalMessages/64-12     80.000 ± 0%   1.000 ± 0%  -98.75% (p=0.000 n=10)
MarshalMessages/512-12   537.000 ± 0%   1.000 ± 0%  -99.81% (p=0.000 n=10)
geomean                    33.69        1.000       -97.03%

See #254

Reduce allocations in marshalMessages by summing their sizes
and then allocating a single buffer to write directly to via
a new marshalInto method.

                       │  before       │              after                   │
                       │    sec/op     │    sec/op     vs base                │
MarshalMessages/1-12      60.35µ ±  4%   33.61µ ±  3%  -44.32% (p=0.000 n=10)
MarshalMessages/8-12     1537.5µ ±  4%   330.9µ ± 11%  -78.47% (p=0.000 n=10)
MarshalMessages/64-12    15.403m ±  5%   1.294m ±  3%  -91.60% (p=0.000 n=10)
MarshalMessages/512-12    74.14m ± 17%   11.84m ±  4%  -84.03% (p=0.000 n=10)
geomean                   3.208m         642.5µ        -79.98%

                       │  before       │              after                   │
                       │     B/op      │     B/op      vs base                │
MarshalMessages/1-12     144.00Ki ± 0%   72.00Ki ± 0%  -50.00% (p=0.000 n=10)
MarshalMessages/8-12     2760.0Ki ± 0%   520.0Ki ± 0%  -81.16% (p=0.000 n=10)
MarshalMessages/64-12    25.719Mi ± 0%   4.008Mi ± 0%  -84.42% (p=0.000 n=10)
MarshalMessages/512-12   200.43Mi ± 0%   32.01Mi ± 0%  -84.03% (p=0.000 n=10)
geomean                   6.648Mi        1.463Mi       -78.00%

                       │  before      │             after                  │
                       │  allocs/op   │ allocs/op   vs base                │
MarshalMessages/1-12       2.000 ± 0%   1.000 ± 0%  -50.00% (p=0.000 n=10)
MarshalMessages/8-12      15.000 ± 0%   1.000 ± 0%  -93.33% (p=0.000 n=10)
MarshalMessages/64-12     80.000 ± 0%   1.000 ± 0%  -98.75% (p=0.000 n=10)
MarshalMessages/512-12   537.000 ± 0%   1.000 ± 0%  -99.81% (p=0.000 n=10)
geomean                    33.69        1.000       -97.03%

See mdlayher#254
@nickgarlis
nickgarlis requested review from SuperQ and mdlayher July 19, 2026 09:08

@SuperQ SuperQ left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, otherwise LGTM.

Comment thread message.go
@nickgarlis
nickgarlis merged commit 712005d into mdlayher:main Jul 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants