Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions libraries/http_server/http_server/dynamic_routing_table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ template <typename V> struct drt_node {
std::string_view k = r.substr(s, c - s);

if (children_.find(k) == children_.end()) {
children_[k] = pool_.allocate(pool_);
auto* child = pool_.allocate(pool_);
children_[k] = child;
// Cache the parameter child pointer for O(1) lookup in find()
if (k.size() > 4 and k[0] == '{' and k[1] == '{' and
k[k.size() - 2] == '}' and k[k.size() - 1] == '}')
param_child_ = child;
}
return children_[k]->find_or_create(r, c);
}
Expand Down Expand Up @@ -100,20 +105,15 @@ template <typename V> struct drt_node {
return it2;
}

{
// if one child is a url param {{param_name}}, choose it
for (const auto& kv : children_) {
auto name = kv.first;
if (name.size() > 4 and name[0] == '{' and name[1] == '{' and
name[name.size() - 2] == '}' and name[name.size() - 1] == '}')
return kv.second->find(r, c);
}
return end();
}
// O(1) fallback to cached parameter child instead of O(n) linear scan
if (param_child_)
return param_child_->find(r, c);
return end();
}

V v_;
std::unordered_map<std::string_view, drt_node*> children_;
drt_node* param_child_ = nullptr; // cached {{param}} child for O(1) lookup
drt_node_pool<drt_node>& pool_;
};

Expand Down
Loading