perf(router): cache-friendly static child lookup#3024
Open
vishr wants to merge 1 commit into
Open
Conversation
findStaticChild scanned []*node and dereferenced every child to read its one-byte label — a pointer chase per static step. Maintain a parallel scLabels []byte (kept in sync in addStaticChild, newNode, the insert split, and Remove) and scan that contiguous slice instead, indexing into staticChildren only on a hit. benchstat (n=6): RouterStaticRoutes-14 8.49µ -> 8.01µ -5.67% (p=0.002) RouterGitHubAPI-14 16.93µ -> 16.45µ -2.86% (p=0.002) RouterParseAPI / GooglePlusAPI: neutral (param-heavy) allocations: 0, unchanged Also fold the is-leaf recomputation (duplicated across 5 sites, with the Remove copy subtly using len()==0 vs == nil) into a single refreshLeaf() helper that uses len() so it is correct whether staticChildren is nil or an emptied-but-non-nil slice left after a removal. Split out of #3023 so the router and JSON changes can be reviewed independently (requested by @aldas). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Split out of #3023 at @aldas's request so the router and JSON changes land as independent PRs.
What
findStaticChildscanned[]*nodeand dereferenced every child just to read its one-byte label — a pointer chase per static step on the routing hot path. This adds a parallelscLabels []byte(kept in sync inaddStaticChild,newNode, the insert split, andRemove) and scans that contiguous slice instead, indexing intostaticChildrenonly on a hit.It also folds the is-leaf recomputation — previously duplicated across 5 sites, with the
Removecopy subtly usinglen()==0while the others used== nil— into a singlerefreshLeaf()helper that useslen(), so it is correct whetherstaticChildrenisnilor an emptied-but-non-nil slice left after a removal.Numbers
The companion ServeHTTP benchmark harness lands with #3023 (the JSON PR); routing benchmarks there exercise this change once both merge.
🤖 Generated with Claude Code