Skip to content

perf(lookup): short-circuit 1-byte keys and inline route.match#102

Merged
tigerwill90 merged 1 commit into
masterfrom
perf/lookup-fast-path
May 4, 2026
Merged

perf(lookup): short-circuit 1-byte keys and inline route.match#102
tigerwill90 merged 1 commit into
masterfrom
perf/lookup-fast-path

Conversation

@tigerwill90
Copy link
Copy Markdown
Collaborator

This PR make two lookup-path optimizations giving 10 to 15 % performance improvement with no regression elsewhere.

After the static binary search lands on a child, the label match already proves search[0] == child.key[0]. So when the child key is exactly one byte, the runtime.memequal call (search[:keyLen] == child.key) is redundant and can be skipped. This sounds niche, but the radix tree forces "/" separators into their own nodes wherever they sit between two non-static neighbours, or between a param and a branching point. Both patterns are common in API. Two consecutive params, e.g. /repos/{owner}/{repo}, compress to:

root
└── "/repos/"
    └── "?" (param owner)
        └── "/"   ← isolated between two params
            └── "?" (param repo)

A param followed by a static branching point, e.g. /users/{id}/repos and /users/{id}/projects, compress to:

root
└── "/users/"
    └── "?" (param)
        └── "/"   ← isolated, cannot merge with either side
            ├── "projects"
            └── "repos"

In both cases the "/" cannot be merged into the param node above, and cannot merge into the static children below (either because the next node is a param, or because there are multiple static children). In the GitHub API fixture 47% of tree nodes have a 1-byte key, mostly those isolated "/" nodes, which is what makes the fast path worth it.

The second change inlines (*Route).match. We cache methods[0] in a new methodFast field for the common case (single method, no matchers), the rest moves to a //go:noinline matchSlow.

Differential Benchmark

goos: darwin
goarch: arm64
pkg: github.com/fox-toolkit/fox
cpu: Apple M4 Max
                        │ /tmp/baseline_all.txt │       /tmp/all_truly_final.txt       │
                        │        sec/op         │   sec/op     vs base                 │
StaticAll                          8.811µ ± ∞ ¹   7.861µ ± 1%  -10.78% (p=0.004 n=5+6)
GithubAll                          15.13µ ± ∞ ¹   13.18µ ± 1%  -12.84% (p=0.004 n=5+6)
StaticHostnameAll                  8.855µ ± ∞ ¹   8.372µ ± 1%   -5.45% (p=0.004 n=5+6)
GithubParamsAll                    70.02n ± ∞ ¹   59.50n ± 1%  -15.02% (p=0.004 n=5+6)
GithubParamsHostnameAll            61.43n ± ∞ ¹   58.12n ± 1%   -5.38% (p=0.004 n=5+6)
InfixCatchAll                      209.1n ± ∞ ¹   204.9n ± 0%   -2.01% (p=0.004 n=5+6)
LongParam                          26.72n ± ∞ ¹   25.52n ± 1%   -4.49% (p=0.004 n=5+6)
OverlappingRoute                   68.25n ± ∞ ¹   64.39n ± 1%   -5.66% (p=0.004 n=5+6)
WithIgnoreTrailingSlash            37.54n ± ∞ ¹   34.66n ± 1%   -7.69% (p=0.004 n=5+6)
CatchAll                           27.06n ± ∞ ¹   26.02n ± 1%   -3.82% (p=0.004 n=5+6)
VeryLongPattern                    82.53n ± ∞ ¹   81.18n ± 1%   -1.64% (p=0.004 n=5+6)
geomean                            241.4n         224.8n        -6.89%

@tigerwill90 tigerwill90 requested a review from pawndev May 3, 2026 14:22
@tigerwill90 tigerwill90 self-assigned this May 3, 2026
@tigerwill90 tigerwill90 merged commit 0515e52 into master May 4, 2026
3 of 4 checks passed
@tigerwill90 tigerwill90 deleted the perf/lookup-fast-path branch May 4, 2026 21:17
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