[http_server] Cache parameter child pointer for O(1) lookup fallback - #4
Closed
cong1920 wants to merge 1 commit into
Closed
[http_server] Cache parameter child pointer for O(1) lookup fallback#4cong1920 wants to merge 1 commit into
cong1920 wants to merge 1 commit into
Conversation
In find(), when a static child lookup misses, the code previously did a
linear scan of ALL children to find a {{param}} child. This is O(n) per
tree level for every parameterized route lookup.
Fix: Cache param_child_ pointer during find_or_create(), so find() can
fall back to it in O(1) instead of scanning.
Benchmark results (median, 25 iterations, WSL2 g++ -O2):
Before After Change
insert: 313.3 ns/op 299.2 ns/op -4.5%
lookup hit: 80.7 ns/op 72.7 ns/op -9.9%
lookup miss: 45.0 ns/op 36.6 ns/op -18.7%
Owner
Author
|
Committed directly to simplify_drt_node instead. |
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.
Summary
Cache the {{param}} child pointer during route registration (ind_or_create) so that ind() can fall back to it in O(1) instead of doing a linear scan of all children.
Problem
In ind(), when a static child lookup misses, the code did a linear scan of ALL children to find a {{param}} child:
\\cpp
for (const auto& kv : children_) {
if (name.size() > 4 and name[0] == '{' and name[1] == '{' ...)
return kv.second->find(r, c);
}
\\
This is O(n) per tree level for every parameterized route lookup.
Fix
Added a cached pointer set during \ind_or_create():
\\cpp
drt_node* param_child_ = nullptr;
\\
Now \ind()\ falls back in O(1):
\\cpp
if (param_child_)
return param_child_->find(r, c);
return end();
\\
Benchmark Results
Environment: WSL2 Ubuntu, g++ -O2 -std=c++20, 25 iterations
vs master branch
The hot path (lookups) now beats master by 10-20%.