Skip to content

Decode the offsets of a sparse column from a local pointer - #129768

Merged
Algunenano merged 3 commits into
ClickHouse:masterfrom
groeneai:sparse-offsets-local-pointer
Nov 11, 2026
Merged

Algunenano merged 3 commits into
ClickHouse:masterfrom
groeneai:sparse-offsets-local-pointer

Conversation

@groeneai

@groeneai groeneai commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #129686

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Reading sparse columns is faster: the offsets of their non-default values are decoded from a local pointer instead of updating the read buffer position for every byte.

Description

deserializeOffsets reads one VarUInt per non-default value, and one per granule, through readVarUInt(UInt64 &, ReadBuffer &). In the ThinLTO binary the inlined loop stores the buffer position after every byte, reloads the position and the buffer end for every VarUInt, and reloads offsets.back() for every value.

Now, while a whole VarUInt (10 bytes) is in the buffer, it is decoded from a local pointer, the position is written back only when leaving that loop, and the row of the next value is kept in a local. The last bytes of a buffer are still read through the ReadBuffer, because a VarUInt there can continue in the next one. The disassembly shows no store to the buffer in the new loop.

Measured, master 35fed92ffa43 vs this PR (Xeon 6975P-C, 16 pinned cores, ThinLTO clang-22; two perf.py runs; ClickBench with 95% CI):

  • sparse_column_filter sum(u64) ... PREWHERE u8 = 0 at 10% non-default, 1 thread: -11.2% / -12.9%.
  • sparse_column_index_sorted sum(b) ... WHERE a != 0: -5.4% / -5.4%.
  • ClickBench at max_threads = 1: WHERE MobilePhoneModel <> '' GROUP BY MobilePhoneModel -4.5% [-6.3, -2.8], GROUP BY MobilePhoneModel -2.6% [-3.6, -1.5], the same WHERE with GROUP BY MobilePhoneModel, UTMSource -2.9% [-5.5, -0.3]. At 16 threads: within noise. Controls (AVG(UserID), SearchPhrase) unchanged, results identical.

This is the form proposed in the issue. String sizes are read the same way; they are not changed here.

04147_sparse_column_coverage now also reads sparse columns written in 13-byte compressed blocks, so VarUInts are split between buffers, with max_block_size 7, 1000 and random; mutants of the new loop (bounds, row arithmetic, position stores) fail it. The *sparse* stateless tests match master.

Idea and analysis by snelheidai in #129686.


Workflow [PR]
Sync PR [sync-upstream/pr/129768]

Version info

  • Merged into: 26.11.1.1980-master (included in 26.11 and later)

groeneai and others added 3 commits November 6, 2026 14:53
deserializeOffsets read every VarUInt of the offsets stream through
readVarUInt(UInt64 &, ReadBuffer &), so the inlined loop stored the buffer
position after every byte, reloaded the position and the buffer end for every
VarUInt, and reloaded offsets.back() for every value. Decode from a local
pointer while a whole VarUInt is in the buffer, write the position back only
when leaving that path, and carry the row of the next value in a local.
The last bytes of a buffer are still read through the ReadBuffer, because a
VarUInt there can continue in the next one.

04147_sparse_column_coverage now also reads sparse columns written in
13-byte compressed blocks, so their VarUInts are split between buffers, with
reads that stop in the middle of a group.

Idea and analysis by snelheidai in ClickHouse#129686.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add in-granule gaps of 128+ rows to both sparse columns so their offsets
streams also hold 2-byte VarUInts split between 13-byte blocks, and pin
index_granularity_bytes so granules stay at 1000 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the add_group and fast-path comments to one line each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author

cc @Avogar, could you review this and add the can be tested label so CI runs? deserializeOffsets now decodes the VarUInt offsets of a sparse column from a local pointer while a whole VarUInt is in the read buffer and writes the position back once, instead of storing it after every byte; the last bytes of a buffer still go through the ReadBuffer.

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Performance change: base and fix ThinLTO binaries from one build dir, two perf.py runs per sparse_column*.xml and 5 paired ClickBench rounds. The per-byte position store is in the base binary's disassembly and absent from the new loop.
b Root cause explained? readVarUInt(UInt64 &, ReadBuffer &) advances istr.position() per byte, so the inlined offsets loop stores the position after every byte and reloads position, buffer end and offsets.back() per value.
c Fix matches root cause? Decode from a local pointer while 10 bytes are in the buffer, write the position back once, carry the next row in a local; the buffer tail still goes through the ReadBuffer.
d Test intent preserved / new tests added? 04147_sparse_column_coverage extended (additions only): 13-byte compressed blocks split VarUInts between buffers, reads with max_block_size 7 / 1000 / random. Five mutants of the new loop fail it. Existing perf tests carry the claim.
e Both directions demonstrated? Perf: base vs fix in the description. Correctness: identical output on master and on the fix; each mutant reddens the test (wrong rows, logical error or server abort).
f Fix is general across code paths? One function, one caller, used by Wide and Compact parts and the Native format. SerializationString size VarUInts are read the same way, not changed (unmeasured).
g Fix generalizes across inputs (params/datatypes/wrappers)? VarUInts of 1, 2 and 9 bytes, buffer boundaries, limits inside a group, empty stream; the offsets stream does not depend on the value type (UInt64 and String tested).
h Backward compatible? (maintainer-approved exception only) Yes: no format, setting or on-disk change.
i Invariants and contracts preserved? Same offsets, returned row count, state and final buffer position on every exit; a truncated VarUInt still throws through the ReadBuffer path.

@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dd102b0]

Summary: ✅


AI Review

Summary

This PR speeds up sparse-column offset decoding in deserializeOffsets by using a local input pointer and carrying the next row position between values. It preserves the existing encoding and adds regression coverage for split VarUInt values and partial reads. The author provides repeated before/after measurements. No actionable findings in the current diff or its callers.

Missing context / blind spots
  • ⚠️ CI is still running; the targeted stateless and performance jobs have no published results yet. No local ClickHouse binary or build is available to rerun the tests or benchmarks. Completed CI reports would close this validation gap; the reported local results are supplied by the author.
Final Verdict

Status: ✅ Approve. No code changes requested; CI validation remains pending.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements can be tested Allows running workflows for external contributors comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). labels Nov 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing dd102b0ee with master d45109e17 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

8 translation units recompiled, 7 s compile time in total, 8 of them have a recent master baseline.

Job report

@Algunenano Algunenano self-assigned this Nov 11, 2026
@Algunenano

Copy link
Copy Markdown
Member

Seems this is visible in AMD:

image

@Algunenano Algunenano left a comment

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.

Nice improvement on top of 269567a

@Algunenano
Algunenano added this pull request to the merge queue Nov 11, 2026
Merged via the queue into ClickHouse:master with commit 98d000d Nov 11, 2026
367 of 368 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 11, 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 comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). 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.

3 participants