perf(lookup): short-circuit 1-byte keys and inline route.match#102
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:A param followed by a static branching point, e.g. /users/{id}/repos and /users/{id}/projects, compress to:
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:noinlinematchSlow.Differential Benchmark