Repository navigation
SparseGrams tokenizer perf improvement - #110541
Conversation
The sparse-grams tokenizer feeds text (`text(tokenizer = sparseGrams(...))`) and bloom-filter (`sparse_grams(...)`) secondary indexes. Building those indexes tokenizes every value, and the tokenizer was delivered one token at a time through `ITokenizer::nextInString`: the whole-string entry points (`forEachToken`, and therefore `forEachTokenToBloomFilter` / `tokenizeToArray`) looped over that pull primitive, paying a per-token `std::tie` data-change check plus offset arithmetic ~2N times for a string of N symbols. That per-token plumbing, not the token selection itself, dominated the build. Add a batch `SparseGramsTokenizer::forEachTokenImpl` that generates the whole string once and hands each token straight to the callback, and route the `SparseGrams` case of `forEachToken` to it. Same tokens in the same order, so index contents and the `tokens` function output are unchanged. Also make `SparseGramsImpl::getNextIndices` iterative and return by out-parameter instead of constructing an `std::optional` per token, and drop a write-only `position` field from the convex-hull element. Measured on bloom-filter index build over 120k random 200-byte values: `sparse_grams(3, 8)` ~1.25x, `sparse_grams(3, 100)` ~1.12x; the tokenizer in isolation (`tokens`) ~1.15x. Output verified byte-identical to before across the `sparseGrams`/`sparseGramsUTF8`/`sparseGramsHashes` functions, the `tokens` function, and text/bloom index query results. Adds tests/performance/sparse_grams_index_build.xml covering both index builds and the isolated tokenizer. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The previous commit copied `src/Interpreters/ITokenizer.h` from a working tree based on another feature branch, pulling in unrelated changes (`tokenizeToArray`, the `IColumn_fwd` include, and `ITokenizer` constructor/`type` tweaks) that are not part of this change. Restore the file to `master` plus only the batch `SparseGramsTokenizer::forEachTokenImpl` addition and its dispatch routing. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [85d241d] Summary: ✅
AI ReviewSummaryThis PR routes Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 31/31 (100.00%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR only refactors the sparseGrams tokenizer (sparseGramsImpl.h streaming interface and ITokenizer.h) plus adds a tokenizer perf test — a full-text/bloom-index code path that no ClickBench query exercises. Every flagged query, including the deterministically-flagged Q30 (+18.3%), runs entirely off that path, so the uniform small slowdowns are run-to-run/co-location variance, not a regression from this change. Q30 is downgraded to not_sure and the remaining envelope-suppressed rows are left non-directional. No action needed for the ClickBench results; the sparse-grams tokenizer change looks performance-neutral on this suite. clickbenchFlagged queries (6 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
|
Ergus
left a comment
There was a problem hiding this comment.
I agree here with the Bot about the perf test.
The code looks correct so far and the de-recursion is always better to read :). However, I won't really expect any measurable performance difference from it.
Let's see the performance results before approving
| /// bypassing the per-token `nextInString` pull (its `std::tie` data-change check and offset | ||
| /// arithmetic on every token). Emits the same tokens in the same order as `nextInString`. | ||
| template <Fn<bool(const char *, size_t)> Callback> | ||
| void forEachTokenImpl(const char * __restrict data, size_t length, Callback && callback) const |
There was a problem hiding this comment.
I think this is the only change that I think could produce some real improvement.
There was a problem hiding this comment.
It's seems consistently show around 10% time improvement, but i cannot guarantee that's all of changes are required for that, indeed.
Reduced the number of rows inserted into sg_src from 5 million to 1 million for performance testing.
| } | ||
|
|
||
| std::optional<SubString> getNextIndices() | ||
| /// Drain the current batch of answers, refilling it via `consume` until there is a result or the |
There was a problem hiding this comment.
@UnamedRus The irrelevant slop in the comment will need to go first:
Iterative (not recursive) and returns by out-parameter rather than an `std::optional`; both are on the hot path, called ~2N times for a string of N symbols.
There was a problem hiding this comment.
Done, slightly changed, but can't think of something significantly better worder.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 39/39 (100.00%) · Uncovered code |
|
Sure, i increased perf test size because original scale didn't show perf improvement confidentially. |
| <create_query>CREATE TABLE sg_text_3_8 (s String, INDEX idx s TYPE text(tokenizer = sparseGrams(3, 8)) GRANULARITY 1) ENGINE = MergeTree ORDER BY tuple()</create_query> | ||
| <create_query>SYSTEM STOP MERGES</create_query> | ||
|
|
||
| <fill_query>INSERT INTO sg_src SELECT randomPrintableASCII(200) FROM numbers(1_500_000)</fill_query> |
There was a problem hiding this comment.
@UnamedRus This commit made the perf test bigger, not smaller. Like to reduce from 1.5 mio to let's say 150k?
There was a problem hiding this comment.
It's got somewhat messy,
Initially it was 1 Mil row test, which show different barely visible, (may be few dozen less stack traces frames in tokenizer code)
Then i bumped it to 5Mil, which showed that reliable enough.
But, as you noticed it made test run longer, multiple seconds per each query.
So, i decreased it, but to slightly bigger than it was initially (1M -> 1.5M)
Last (fast) run, where difference is barely noticeable at 1.5M rows
https://performance.ci.clickhouse.com/runs/pr-110541-85d241dbd9915a2a30a739c595bf79ce1e5638f7/tests/sparse_grams_index_build?query_index=1
There was a problem hiding this comment.
Actually, may be it's me, who is wrong. (new perf dashboard is bit silly in showing which metric change is actually useful/important)

https://s3.amazonaws.com/clickhouse-test-reports/PRs/110541/85d241dbd9915a2a30a739c595bf79ce1e5638f7/performance_comparison_amd_release_master_head_2_6/report.html
This is last run, we have 4 queries in test, and it's 1.3-1.8s total time per query (so user time probably 16x of that) , is that acceptable or, should i tune it down?
There was a problem hiding this comment.
It's fine - I think we (ClickHouse) will need to do a sweep of the performance test results and trim runtimes of all overlong tests.
…x_build_test_fix Follow-up for ClickHouse#110541 (reduce size of sparse grams build test)

Speed up sparse grams tokenizer a bit (should be around 10-20%)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Optimized building of text and bloom filter based skip indexes with sparseGrams tokenizer
Version info
26.8.1.1090(included in26.8and later)