Repository navigation
Conversation
GET is by far the most common request method. routeMethods.find matched it through a string switch over every method. Return the GET handler first when one is registered; other methods, and GET with only an Any handler, still go through the switch unchanged.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused optimization preserves existing routing semantics and introduces no unresolved issues.
Review effort: Balanced
Findings: None
What changed in this PR
Optimizes common GET route lookup without changing fallback behavior.
Changes:
- Returns registered GET handlers before the method switch.
- Preserves existing Any and missing-handler fallbacks.
| File | Description |
|---|---|
router.go |
Adds the GET fast path in routeMethods.find. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Closing. Measured against a placebo build with the same code layout, the GET early return gains only about 2% on router benchmarks (about 0.5 ns per GET request). That is below the bar we set for performance changes, and it adds a special case to the method lookup. The raw -3.7% to -3.9% figures in the description were mostly code-layout effects, as explained there. Other router ideas measured and rejected in the same exercise: comparing whole node prefixes at once (misses got 4–13% slower), and |
GET is by far the most common request method.
routeMethods.findresolved it through the methodswitch(which Go 1.27 compiles to a jump table on string length followed by byte comparisons). This returns the GET handler first when one is registered.Behavior is unchanged:
findover 5.2M combinations of node handlers, methods (including custom,RouteAny,RouteNotFound),fallbackToAnyandautoHandleHEAD, and found 0 differences. An end-to-end dump of status, Allow header, RouteInfo and path values across 44 paths × 18 methods was byte-identical to master.Benchmarks:
-cpu=1, 17 interleaved samples).*Missesbenchmarks are layout only: 404 lookups never callrouteMethods.find.findcall in an isolated micro-benchmark. No router-level benchmark shows it, including static routes with only POST/PUT/HEAD and GitHub's mixed-method routes.go test -race ./...,go vet,staticcheckandgolintpass.Other options tried and rejected, measured against master without a placebo:
bytes.IndexBytefor static child lookup: slower than the short label scan.