⚡ perf(req): skip ip.String() alloc in IsProxyTrusted for CIDR-only trust configs - #4500
Conversation
…st configs IsProxyTrusted looked up config.TrustProxyConfig.ips[ip.String()] even when the exact-IP map is empty, so ip.String() heap-allocated on every call for the common CIDR-only / range-based trust setup. Behind a load balancer, IP()/Host()/Scheme() reach this per request. Guard the lookup with len(ips) > 0. Adds a WithProxyCheckCIDR benchmark exercising the empty-ips range path. Before: 8 B/op, 1 allocs/op After: 0 B/op, 0 allocs/op Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
WalkthroughThe ChangesIsProxyTrusted optimization
Estimated code review effort: 1 (Trivial) | ~5 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Optimizes DefaultReq.IsProxyTrusted() to avoid an unnecessary ip.String() allocation when TrustProxyConfig is configured with CIDR/range entries only (empty exact-IP map), improving per-request performance in common proxy/LB deployments.
Changes:
- Guard exact-IP map lookup behind
len(config.TrustProxyConfig.ips) > 0to skipip.String()when it would be wasted work. - Add a new benchmark subcase (
WithProxyCheckCIDR) to exercise the CIDR-only path and validate the allocation reduction.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| req.go | Skips ip.String() allocation when TrustProxyConfig.ips is empty, preserving behavior while improving hot-path performance. |
| ctx_test.go | Adds a benchmark scenario to cover the CIDR-only trust configuration path and measure alloc improvements. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4500 +/- ##
=======================================
Coverage 92.98% 92.98%
=======================================
Files 139 139
Lines 13931 13932 +1
=======================================
+ Hits 12954 12955 +1
Misses 607 607
Partials 370 370
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
IsProxyTrustedlooked upconfig.TrustProxyConfig.ips[ip.String()]even when the exact-IP map is empty, soip.String()heap-allocated on every call for the common CIDR-only / range-based trust setup. Behind a load balancer,IP(),Host(), andScheme()reach this per request. Guard the lookup withlen(ips) > 0.Adds a
WithProxyCheckCIDRbenchmark exercising the empty-ips range path.Benchmark
Benchmark_Ctx_IsProxyTrusted/WithProxyCheckCIDR(CIDR-only trust, remote IP reaches the range check):Behavior is unchanged: with a non-empty exact-IP map the lookup runs exactly as before (existing
WithProxyCheckcovers that path).🤖 Generated with Claude Code