Repository navigation
Vectorize small-offset overlapping copies on x86_64 with SSSE3 pshufb - #4
Merged
Merged
Conversation
This ports the AArch64 NEON `tbl` optimization for `ZSTD_wildcopy`'s small-offset path to x86_64 using SSSE3 `pshufb`. For small match offsets (overlap period < 16 bytes), `ZSTD_wildcopy` falls back to an 8-byte-per-iteration `COPY8` loop. This dominates decompression of integer and low-cardinality columns, whose matches frequently have offsets of 4 or 8 bytes (e.g. repeated fixed-width values). Build the repeating pattern once in an SSE register and store 16 bytes per iteration, advancing the pattern with a single `_mm_shuffle_epi8` (`pshufb`) table lookup and no load inside the loop. `pshufb` is the direct analog of NEON `vqtbl1q_u8` and reuses the same index-table scheme: init[diff][j] = j % diff : build the pattern from a 16-byte load adv[diff][j] = (16 + j) % diff : shift the pattern forward by 16 bytes The path is gated behind compile-time SSSE3 detection (`ZSTD_ARCH_X86_SSSE3`, from `__SSSE3__`); the scalar `COPY8` path is kept for all other targets, and the non-overlapping (offset >= 16) path is unchanged. Each iteration stores 16 bytes and stops once `op >= oend`, overrunning the logical end by at most 15 bytes, which always fits in the `WILDCOPY_OVERLENGTH` (32) slack callers guarantee; this is asserted with `ZSTD_STATIC_ASSERT`. On an AMD Ryzen 9 9950X (x86-64-v3), ZSTD level 1 decompression of ClickBench `hits` columns (1M rows, 64 KiB blocks, best of 15) speeds up: column baseline MB/s patched MB/s speedup UserID 7373.3 8818.0 1.20x AdvEngineID 11036.8 12401.9 1.12x ClientIP 4000.8 4640.8 1.16x RegionID 3591.1 4080.2 1.14x URL 3052.1 3079.7 1.01x Title 4643.5 4688.6 1.01x WatchID 84246.0 83910.3 1.00x The string columns (`URL`, `Title`) and the incompressible `WatchID` column are unchanged, as expected: their decompression does not exercise the small-offset path. Verified byte-exact across block sizes 4 KiB-1 MiB and clean under ASan and UBSan. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This was referenced Jun 22, 2026
Member
|
Looks good. It's pretty much the same principle we have for We should validate performance in CI (the cloud run, since that uses ZSTD by default I think) and send the patch upstream (likely with #3) |
Algunenano
approved these changes
Jun 22, 2026
…ap-copy Resolve the conflict with the AArch64 NEON variant of the same optimization (#3, merged): the NEON and SSSE3 paths now share the comment and the `init`/`adv` index tables, with an inner `#if` selecting the intrinsics.
The SSSE3/NEON short-offset path in ZSTD_wildcopy did not pass the x86 `make test` target, which builds with -Werror on top of DEBUGFLAGS: - `ZSTD_copy8` lost its only reference in most translation units (the COPY8 fallback loop is preprocessed out), tripping -Wunused-function. Mark it UNUSED_ATTR like the default-norm tables above it. - `ZSTD_STATIC_ASSERT` expanded to a statement before the `init`/`adv` table declarations, tripping -Wdeclaration-after-statement. Move it after the declarations in each arch branch. - Casting `BYTE*` directly to `__m128i*` tripped -Wcast-align. Go through `void*`, matching how ZSTD_copy16 receives `void*` operands for the same unaligned load/store intrinsics. Verified with clang 21 and gcc: `make -C lib libzstd.a` with MOREFLAGS="-Werror -mssse3" and without -mssse3 build clean, and `make check MOREFLAGS="-Werror -mssse3"` passes. Co-Authored-By: Claude Fable 5 <[email protected]>
thevar1able
added a commit
to ClickHouse/ClickHouse
that referenced
this pull request
Jul 20, 2026
…n `make test` The previous submodule pointer failed the dependency's x86 `make test` target (which adds `-Werror`): `ZSTD_copy8` became unused in SSSE3 translation units, the `ZSTD_STATIC_ASSERT` statement before the new `static const` tables tripped `-Wdeclaration-after-statement`, and the direct `BYTE*` -> `__m128i*` casts tripped `-Wcast-align`. Fixed in ClickHouse/zstd#4: `ZSTD_copy8` is now marked `UNUSED_ATTR`, the static assert moved after the declarations, and the intrinsic casts go through `void*`. Verified with clang 21 and gcc that `make -C lib libzstd.a` with `MOREFLAGS="-Werror -mssse3"` and without `-mssse3` builds clean, and `make check MOREFLAGS="-Werror -mssse3"` passes. Co-Authored-By: Claude Fable 5 <[email protected]>
…lap copy The short-offset path in ZSTD_wildcopy built the initial pattern register with a 16-byte load at `ip`, but only the first `diff` (8..15) bytes of that load are initialized - the tail overlaps the not-yet-written part of `dst`. The `tbl`/`pshufb` lookup never selects those lanes, so the output bytes are correct, but MemorySanitizer propagates the poisoned shadow through the shuffle and into every stored byte, and any instrumented reader of the decompressed data then reports use-of-uninitialized-value (observed in ClickHouse CI as a false positive in SZ3's Huffman decoder, which reads a zstd-decompressed buffer). Seed the first 16 output bytes with two COPY8 calls instead - exactly the first two iterations of the scalar fallback loop, reading only bytes that are already written - and load the pattern register from the freshly written `dst` prefix. This also makes the `init` shuffle table unnecessary, so drop it. The steady-state loop is unchanged; on an AMD Ryzen 9 9950X, decompression of 128 MiB of period-12 data is slightly faster (1225 -> 1309 MB/s) since the setup no longer needs the initial shuffle. Verified under MSan (clang -fsanitize=memory) with round trips over pattern periods 1..20: previously reported use-of-uninitialized-value, now clean and byte-exact. `make -C lib libzstd.a` with MOREFLAGS="-Werror [-mssse3]" builds clean with clang 21 and gcc, and `make check MOREFLAGS="-Werror -mssse3"` passes. Co-Authored-By: Claude Fable 5 <[email protected]>
pull Bot
pushed a commit
to SINHASantos/ClickHouse
that referenced
this pull request
Jul 23, 2026
Bump the `contrib/zstd` submodule to include an SSSE3 `pshufb` pattern-fill for small-offset overlapping copies in `ZSTD_wildcopy` - the x86_64 counterpart of the AArch64 NEON change from ClickHouse#108049. This replaces the 8-byte-per-iteration `COPY8` loop (16 bytes per iteration, no load in the loop) that otherwise dominates ZSTD decompression of integer and low-cardinality columns. On an AMD Ryzen 9 9950X (x86-64-v3), ZSTD level 1 decompression of ClickBench `hits` columns speeds up: UserID 1.20x, AdvEngineID 1.12x, ClientIP 1.16x, RegionID 1.14x; string and incompressible columns are unchanged. Verified byte-exact and clean under ASan and UBSan. The `contrib/zstd` change: ClickHouse/zstd#4 Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
Bump the `contrib/zstd` submodule to include an SSSE3 `pshufb` pattern-fill for small-offset overlapping copies in `ZSTD_wildcopy` - the x86_64 counterpart of the AArch64 NEON change from ClickHouse#108049. This replaces the 8-byte-per-iteration `COPY8` loop (16 bytes per iteration, no load in the loop) that otherwise dominates ZSTD decompression of integer and low-cardinality columns. On an AMD Ryzen 9 9950X (x86-64-v3), ZSTD level 1 decompression of ClickBench `hits` columns speeds up: UserID 1.20x, AdvEngineID 1.12x, ClientIP 1.16x, RegionID 1.14x; string and incompressible columns are unchanged. Verified byte-exact and clean under ASan and UBSan. The `contrib/zstd` change: ClickHouse/zstd#4 Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
…n `make test` The previous submodule pointer failed the dependency's x86 `make test` target (which adds `-Werror`): `ZSTD_copy8` became unused in SSSE3 translation units, the `ZSTD_STATIC_ASSERT` statement before the new `static const` tables tripped `-Wdeclaration-after-statement`, and the direct `BYTE*` -> `__m128i*` casts tripped `-Wcast-align`. Fixed in ClickHouse/zstd#4: `ZSTD_copy8` is now marked `UNUSED_ATTR`, the static assert moved after the declarations, and the intrinsic casts go through `void*`. Verified with clang 21 and gcc that `make -C lib libzstd.a` with `MOREFLAGS="-Werror -mssse3"` and without `-mssse3` builds clean, and `make check MOREFLAGS="-Werror -mssse3"` passes. Co-Authored-By: Claude Fable 5 <[email protected]>
actueleai
pushed a commit
to actueleai/zstd
that referenced
this pull request
Nov 24, 2026
Rebase of ClickHouse#3 and ClickHouse#4 (branch clickhouse, 5b3e9e8) onto upstream dev 49cf517. The unwind-information patches of the old fork are upstream already (80cac40, a480191). (cherry picked from commit edda504)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The x86_64 counterpart of #3 (which does the same for AArch64 with NEON
tbl). Independent of that PR; the two touch the same lines and whichever merges second will need a trivial conflict resolution.Vectorize small-offset overlapping copies on x86_64 with SSSE3
pshufbFor small match offsets (overlap period < 16 bytes),
ZSTD_wildcopyfalls back to an 8-byte-per-iterationCOPY8loop. This loop dominates decompression of integer and low-cardinality data, whose matches frequently have offsets of 4 or 8 bytes (e.g. runs of repeated fixed-width values).This builds the repeating pattern once in an SSE register and stores 16 bytes per iteration, advancing the pattern with a single
_mm_shuffle_epi8(pshufb) table lookup and no load inside the loop.pshufbis the direct analog of NEONvqtbl1q_u8, using the same index tables:init[diff][j] = j % diffbuilds the period-diffpattern from a 16-byte load;adv[diff][j] = (16 + j) % diffshifts the pattern forward by 16 bytes each iteration, which composes correctly becausepattern[k] = src[k % diff].The path is gated behind compile-time SSSE3 detection (
ZSTD_ARCH_X86_SSSE3, from__SSSE3__). The scalarCOPY8path is kept unchanged for all other targets, and the non-overlapping (offset >= 16) path is untouched.Results
On an AMD Ryzen 9 9950X (
x86-64-v3), ZSTD level 1 decompression of ClickBenchhitscolumns (1M rows, 64 KiB blocks, best of 15):UserIDAdvEngineIDClientIPRegionIDURL/TitleWatchIDThe string and incompressible columns are unchanged, as expected: their decompression does not exercise the small-offset path.
Correctness
Round-trip decompression is byte-exact against the original input for all tested columns at block sizes from 4 KB to 1 MB, and clean under AddressSanitizer and UndefinedBehaviorSanitizer. Each iteration stores 16 bytes and stops once
op >= oend, overrunning the logical end by at most 15 bytes — well within the existingWILDCOPY_OVERLENGTH(32) slack, asserted withZSTD_STATIC_ASSERT.