Skip to content

reverseproxy: preallocate upstream slices with known sizes - #7926

Merged
steadytao merged 2 commits into
caddyserver:masterfrom
jvoisin:ups
Aug 10, 2026
Merged

steadytao merged 2 commits into
caddyserver:masterfrom
jvoisin:ups

Conversation

@jvoisin

@jvoisin jvoisin commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Preallocate the sources slice in MultiUpstreams.Provision and the netAddrs slice in UpstreamResolver.ParseAddresses now that their final lengths are known, avoiding the intermediate append regrowth allocations. For a 4-address resolver this reduces ParseAddresses from 11 to 9 allocs/op and 528 to 384 B/op; the benefit scales with the number of sources/addresses.

                             |  before  |            after            |
                             |  sec/op  |  sec/op     vs base         |

UpstreamResolverParseAddresses-8 | 2.289µ |1.946µ ~ (p=0.165) noisy |

B/op: 528 → 384 -27.27% (p=0.000)
allocs/op: 11 → 9 -18.18% (p=0.000)

Adds BenchmarkUpstreamResolverParseAddresses as per policy.

Assistance Disclosure

"No AI was used."

jvoisin and others added 2 commits August 7, 2026 13:37
Preallocate the sources slice in MultiUpstreams.Provision and the
netAddrs slice in UpstreamResolver.ParseAddresses now that their final
lengths are known, avoiding the intermediate append regrowth
allocations. For a 4-address resolver this reduces ParseAddresses from
11 to 9 allocs/op and 528 to 384 B/op; the benefit scales with the
number of sources/addresses.

                                 |  before  |            after            |
                                 |  sec/op  |  sec/op     vs base         |
UpstreamResolverParseAddresses-8 | 2.289µ   |1.946µ     ~ (p=0.165) noisy |

B/op:      528 → 384   -27.27%  (p=0.000)
allocs/op:  11 →   9   -18.18%  (p=0.000)

Adds BenchmarkUpstreamResolverParseAddresses as per policy.

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thank you again.

@steadytao
steadytao merged commit 22e976e into caddyserver:master Aug 10, 2026
26 checks passed
@steadytao steadytao added this to the v2.11.5 milestone Aug 10, 2026
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