Skip to content

perf(proxy): avoid unnecessary alloc in Yield#7708

Merged
yongtang merged 1 commit into
coredns:masterfrom
thevilledev:fix/proxy-yield
Nov 24, 2025
Merged

perf(proxy): avoid unnecessary alloc in Yield#7708
yongtang merged 1 commit into
coredns:masterfrom
thevilledev:fix/proxy-yield

Conversation

@thevilledev

@thevilledev thevilledev commented Nov 23, 2025

Copy link
Copy Markdown
Collaborator

1. Why is this pull request needed and what does it do?

Context is the plugin/pkg/proxy helper package, namely the persistent connection pool.

Currently, Yield allocates a new timer on every call via "time.After", even if the yield channel has space. This creates unnecessary heap allocations and GC pressure under load.

This change adds a non-blocking select to try sending first, avoiding the timer allocation in the happy path. The timeout-based send is only used as a fallback when the receiver is not ready.

Bench run showing the performance difference in "best case" scenario, where a timer allocation is not needed:

goos: darwin
goarch: arm64
pkg: github.com/coredns/coredns/plugin/pkg/proxy
cpu: Apple M1 Pro
        │    before    │                after                │
        │    sec/op    │   sec/op     vs base                │
Yield-8   1140.0n ± 9%   821.1n ± 2%  -27.98% (p=0.000 n=10)

        │   before   │               after                │
        │    B/op    │    B/op     vs base                │
Yield-8   339.0 ± 4%   114.0 ± 2%  -66.37% (p=0.000 n=10)

        │   before   │                after                │
        │ allocs/op  │ allocs/op   vs base                 │
Yield-8   3.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=10)

2. Which issues (if any) are related?

None.

3. Which documentation changes (if any) need to be made?

None.

4. Does this introduce a backward incompatible change or deprecation?

No.

@thevilledev thevilledev changed the title perf(proxy): fix timer leak in Yield perf(proxy): avoid unnecessary alloc in Yield Nov 23, 2025
Comment thread plugin/pkg/proxy/persistent.go Outdated
Comment thread plugin/pkg/proxy/persistent.go Outdated
Currently, Yield allocates a new timer on every call via
"time.After", even if the yield channel has space. This creates
unnecessary heap allocations and GC pressure under load.

This change adds a non-blocking select to try sending first,
avoiding the timer allocation in the happy path. The timeout-based
send is only used as a fallback when the receiver is not ready.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
@yongtang yongtang merged commit fe7335e into coredns:master Nov 24, 2025
11 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.

3 participants