Skip to content

Optimize radix route fallback matching#4654

Open
heliang-whut wants to merge 2 commits into
gin-gonic:masterfrom
heliang-whut:optimize-radix-route-fallback
Open

Optimize radix route fallback matching#4654
heliang-whut wants to merge 2 commits into
gin-gonic:masterfrom
heliang-whut:optimize-radix-route-fallback

Conversation

@heliang-whut
Copy link
Copy Markdown

@heliang-whut heliang-whut commented May 16, 2026

Summary

This PR optimizes Radix tree route matching when a node has both static children and a wildcard fallback. The router now records the fallback node directly and skips the already-failed static branch when rolling back, avoiding the per-request node snapshot allocation. It also reuses the pre-trimmed path string instead of rebuilding it with prefix + path.

Benchmark report

Environment:

  • OS/arch: darwin/arm64
  • CPU: Apple M4 Max
  • Command: go test -run=^$ -bench='Benchmark(StaticRouteWithParamFallback|OneRoute|ManyRoutes|404Many|Github)$' -benchmem -count=10
  • Baseline: origin/master at 5f4f964, with the new benchmark added for comparison
Benchmark Baseline ns/op This PR ns/op Change Baseline allocs This PR allocs
BenchmarkStaticRouteWithParamFallback 53.719 22.409 -58.28% 2 allocs / 128 B 0 allocs / 0 B
BenchmarkOneRoute 18.939 19.160 +1.17% 0 allocs / 0 B 0 allocs / 0 B
Benchmark404Many 34.153 34.589 +1.28% 0 allocs / 0 B 0 allocs / 0 B
BenchmarkGithub 580.950 585.870 +0.85% 15 allocs / 833 B 15 allocs / 833 B

The main optimized case removes all matching allocations and improves latency by about 58%. Existing broad router benchmarks remain allocation-neutral; timing changes are within about 1.3% in this local run.

Pull Request Checklist

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (local go test ./... passes; CI will run on GitHub).
  • Tests are added or modified as needed to cover code changes.
  • This PR does not introduce a new feature requiring docs/doc.md updates.

Verification

  • go test -run='TestTree|TestRouter|TestRoute|TestGithub' -timeout=60s ./...
  • go test ./...

@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.36%. Comparing base (3dc1cd6) to head (6d97ab9).
⚠️ Report is 276 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4654      +/-   ##
==========================================
+ Coverage   99.21%   99.36%   +0.14%     
==========================================
  Files          42       48       +6     
  Lines        3182     3137      -45     
==========================================
- Hits         3157     3117      -40     
+ Misses         17       13       -4     
+ Partials        8        7       -1     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 99.35% <100.00%> (?)
-tags go_json 99.22% <100.00%> (?)
-tags nomsgpack 99.28% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.25 99.29% <100.00%> (?)
go-1.26 99.36% <100.00%> (?)
macos-latest 99.29% <100.00%> (+0.08%) ⬆️
ubuntu-latest 99.36% <100.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant