Skip to content

Fix quadratic insert into Poco::ListMap for repeated keys - #114410

Merged
CheSema merged 1 commit into
ClickHouse:masterfrom
sanjams2:http-params-quadratic-insert
Aug 12, 2026
Merged

CheSema merged 1 commit into
ClickHouse:masterfrom
sanjams2:http-params-quadratic-insert

Conversation

@sanjams2

@sanjams2 sanjams2 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Performance Improvement

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

HTMLForm and MessageHeader keep their entries in Poco::Net::NameValueCollection, which is a Poco::ListMap over std::list. ListMap::insert keeps 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 ~10k role parameters roughly 0.2–0.3 s per request was spent inside HTMLForm before the query started (server-side query_duration_ms stays flat while client-observed latency grows super-linearly).

Because equal keys always form a single block (insert and operator[] 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 and find() (first of block) are unchanged.

Micro-benchmark of Poco::ListMap<std::string, std::string>::insert, n values under the same key (clang, -O2):

n before after
1 000 2.4 ms 0.09 ms
10 000 226 ms 0.85 ms
30 000 2.2 s 3.3 ms

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_fields and is left as is here.

Version info

  • Merged into: 26.8.1.1279 (included in 26.8 and later)
  • Backported to: 26.6.9.14

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.
@sanjams2
sanjams2 force-pushed the http-params-quadratic-insert branch from 366f7e1 to 8d49872 Compare August 12, 2026 02:16
@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Aug 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8d49872]

Summary: ✅


AI Review

Summary

This PR changes Poco::ListMap::insert to scan from the back so repeated same-key inserts in NameValueCollection avoid the quadratic behavior described in the PR, and adds unit coverage for the block-contiguity and ordering invariants that the optimization relies on. I did not find a correctness, compatibility, or test-wiring issue in the current diff, and the green CI run is consistent with that assessment.

Final Verdict

✅ No blocking issues found.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.00% 79.00% +0.00%

Changed lines: Changed C/C++ lines covered: 46/46 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Aug 12, 2026
@CheSema CheSema self-assigned this Aug 12, 2026
/// end, same as before.
typename Container::reverse_iterator it = _list.rbegin();
typename Container::reverse_iterator itEnd = _list.rend();
for (; it != itEnd; ++it)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

@clickhouse-gh

clickhouse-gh Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 37 queries analysed

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.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 17 not_sure 556 484 -12.9% <0.0001 This PR only optimizes Poco::ListMap::insert for repeated HTTP query parameters; ClickBench Q17 is a plain SELECT that never exercises that path, so the -12.9% is run-to-run variance, not a PR effect.

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
  • StressHouse run: 0c3e890c-90ec-4d8a-b321-de12f7b9857d
  • MIRAI run: 112253f4-904d-4e41-a5be-68e00e244abf
  • PR check IDs:
    • clickbench_934665_1786556881
    • clickbench_934671_1786556881
    • clickbench_934687_1786556881
    • tpch_adapted_1_official_934696_1786556881
    • tpch_adapted_1_official_934722_1786556881
    • tpch_adapted_1_official_934736_1786556881

@CheSema
CheSema added this pull request to the merge queue Aug 12, 2026
Merged via the queue into ClickHouse:master with commit 405e218 Aug 12, 2026
180 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 12, 2026
@rschu1ze rschu1ze added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 17, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 17, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 17, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 17, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 17, 2026
Backport #114410 to 26.6: Fix quadratic insert into Poco::ListMap for repeated keys
PedroTadim added a commit that referenced this pull request Sep 18, 2026
Backport #114410 to 26.3: Fix quadratic insert into Poco::ListMap for repeated keys
PedroTadim added a commit that referenced this pull request Sep 18, 2026
Backport #114410 to 26.7: Fix quadratic insert into Poco::ListMap for repeated keys
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Sep 18, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants