Repository navigation
Fix quadratic insert into Poco::ListMap for repeated keys - #114410
Conversation
NameValueCollection (used by HTMLForm and MessageHeader) is a Poco::ListMap over std::list. insert() keeps equal keys in one contiguous block, but found the end of that block by scanning from the front, so inserting n values under the same key cost O(n^2) compares. A request with ~10k repeated query parameters (e.g. `role=`) spent a few hundred milliseconds in HTMLForm before the query even started. Equal keys always form a single block, so the end of the block is one past the last matching entry: search from the back instead. Appending under the most recently added key (the repeated-parameter case) becomes O(1); in general the cost is the distance from the back of the list to that key's block. An absent key still costs one full scan as before, and iteration order / find() semantics are unchanged. Add a unit test for the block-contiguity semantics and the repeated-key case.
366f7e1 to
8d49872
Compare
|
Workflow [PR], commit [8d49872] Summary: ✅
AI ReviewSummaryThis PR changes Final Verdict✅ No blocking issues found. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 46/46 (100.00%) · Uncovered code |
| /// end, same as before. | ||
| typename Container::reverse_iterator it = _list.rbegin(); | ||
| typename Container::reverse_iterator itEnd = _list.rend(); | ||
| for (; it != itEnd; ++it) |
There was a problem hiding this comment.
I'm OK with the current change. And will merge it when CI is done.
However I kindly propose to consider the usage of additional method here:
Iterator findLast(const KeyType & key)
/// Finds the last occurrence of the key and
/// returns iterator pointing to the found entry
/// or iterator pointing to the end if entry is
/// not found.
{
typename Container::reverse_iterator it = _list.rbegin();
typename Container::reverse_iterator itEnd = _list.rend();
for (; it != itEnd; ++it)
{
if (isEqual(it->first, key))
{
typename Container::iterator found = it.base();
return --found;
}
}
return _list.end();
}
That will move the logic about finding right element away from insert.
Iterator it = findLast(val.first);
if (it != _list.end())
++it;
return _list.insert(it, val);
|
📊 Cloud Performance Report ✅ AI verdict: This change only rewrites Poco::ListMap::insert so repeated HTTP query parameters (and headers) append in near-constant time instead of scanning the whole collection — it does not touch the server's query-execution hot path. Q17 was flagged as a -12.9% improvement, but since the query never exercises the changed code, that delta is run-to-run variance and has been downgraded to not_sure. Q18 was correctly left as no_change (its small shift stays inside master's normal variance band). Net: no plausible per-query performance effect from this PR on ClickBench. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
Backport #114410 to 26.6: Fix quadratic insert into Poco::ListMap for repeated keys
Backport #114410 to 26.3: Fix quadratic insert into Poco::ListMap for repeated keys
Backport #114410 to 26.7: Fix quadratic insert into Poco::ListMap for repeated keys
…istMap for repeated keys
…istMap for repeated keys
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Parsing an HTTP request that carries many query parameters or headers with the same name (for example thousands of repeated
role=parameters) no longer takes quadratic time.Details
HTMLFormandMessageHeaderkeep their entries inPoco::Net::NameValueCollection, which is aPoco::ListMapoverstd::list.ListMap::insertkeeps all entries with an equal key in one contiguous block, but it located the end of that block by scanning from the front of the list, so inserting n values under one key cost O(n²) case-insensitive comparisons.This is visible when a client selects a large set of roles per request via
?role=a&role=b&…: with ~10kroleparameters roughly 0.2–0.3 s per request was spent insideHTMLFormbefore the query started (server-sidequery_duration_msstays flat while client-observed latency grows super-linearly).Because equal keys always form a single block (
insertandoperator[]are the only ways to add entries), the end of the block is simply one past the last matching entry, so search from the back instead. Appending under the most recently added key — the repeated-parameter case — becomes O(1); in general an insert now costs the distance from the back of the list to that key's block rather than the distance from the front. A key that is not present still costs one full scan, exactly as before; iteration order andfind()(first of block) are unchanged.Micro-benchmark of
Poco::ListMap<std::string, std::string>::insert, n values under the same key (clang,-O2):A unit test is added for the block-contiguity /
find/erase/operator[]semantics and for the repeated-key ordering; it passes both before and after this change (no timing assertions).Inserting many distinct keys is still O(n²) overall since each miss scans the list; that path is already bounded by
http_max_fieldsand is left as is here.Version info
26.8.1.1279(included in26.8and later)26.6.9.14