Skip to content

Vectorize small-offset overlapping copies on x86_64 with SSSE3 pshufb - #4

Merged
thevar1able merged 4 commits into
clickhousefrom
x86-ssse3-overlap-copy
Jul 22, 2026
Merged

thevar1able merged 4 commits into
clickhousefrom
x86-ssse3-overlap-copy

Conversation

@thevar1able

Copy link
Copy Markdown
Member

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 pshufb

For small match offsets (overlap period < 16 bytes), ZSTD_wildcopy falls back to an 8-byte-per-iteration COPY8 loop. 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. pshufb is the direct analog of NEON vqtbl1q_u8, using the same index tables:

  • init[diff][j] = j % diff builds the period-diff pattern from a 16-byte load;
  • adv[diff][j] = (16 + j) % diff shifts the pattern forward by 16 bytes each iteration, which composes correctly because pattern[k] = src[k % diff].

The path is gated behind compile-time SSSE3 detection (ZSTD_ARCH_X86_SSSE3, from __SSSE3__). The scalar COPY8 path 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 ClickBench hits columns (1M rows, 64 KiB blocks, best of 15):

column type baseline MB/s patched MB/s speedup
UserID Int64 7373 8818 1.20x
AdvEngineID Int16 11037 12402 1.12x
ClientIP Int32 4001 4641 1.16x
RegionID Int32 3591 4080 1.14x
URL / Title String 3052 / 4644 3080 / 4689 ~1.00x (unchanged)
WatchID Int64 (incompressible) 84246 83910 ~1.00x (unchanged)

The 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 existing WILDCOPY_OVERLENGTH (32) slack, asserted with ZSTD_STATIC_ASSERT.

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]>
@Algunenano

Copy link
Copy Markdown
Member

Looks good. It's pretty much the same principle we have for copyOverlap<16> in our LZ4 code.

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)

…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]>
@thevar1able
thevar1able merged commit 5b3e9e8 into clickhouse Jul 22, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants