Skip to content

Commit ae70342

Browse files
author
Usama Arif
committed
compress: Elide long-offset path on 64-bit
ZSTD_seqToCodes() only enables longOffsets on 32-bit builds because the 64-bit bitstream accumulator can hold every supported offset. However, ZSTD_encodeSequences_body() still tests the runtime flag for the first sequence and every sequence in its hot loop. Make the architecture condition explicit at both branches so 64-bit compilers can discard the unreachable path. Add an assertion documenting the invariant while preserving the existing 32-bit behavior. In five alternating GCC 11.5 fullbench runs encoding sequences for an 8 MiB, 50%-compressible level-3 sample, throughput increased from 1201.6 to 1213.4 MB/s on average (+1.0%) with identical 2,706,887-byte output. The fullbench text section also shrank by 512 bytes. Signed-off-by: Usama Arif <usama.arif@linux.dev>
1 parent d9c0c7e commit ae70342

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

‎lib/compress/zstd_compress_sequences.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ ZSTD_encodeSequences_body(
300300
FSE_CState_t stateOffsetBits;
301301
FSE_CState_t stateLitLength;
302302

303+
assert(MEM_32bits() || !longOffsets);
303304
RETURN_ERROR_IF(
304305
ERR_isError(BIT_initCStream(&blockStream, dst, dstCapacity)),
305306
dstSize_tooSmall, "not enough space remaining");
@@ -315,7 +316,7 @@ ZSTD_encodeSequences_body(
315316
if (MEM_32bits()) BIT_flushBits(&blockStream);
316317
BIT_addBits(&blockStream, sequences[nbSeq-1].mlBase, ML_bits[mlCodeTable[nbSeq-1]]);
317318
if (MEM_32bits()) BIT_flushBits(&blockStream);
318-
if (longOffsets) {
319+
if (MEM_32bits() && longOffsets) {
319320
U32 const ofBits = ofCodeTable[nbSeq-1];
320321
unsigned const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1);
321322
if (extraBits) {
@@ -353,7 +354,7 @@ ZSTD_encodeSequences_body(
353354
if (MEM_32bits() && ((llBits+mlBits)>24)) BIT_flushBits(&blockStream);
354355
BIT_addBits(&blockStream, sequences[n].mlBase, mlBits);
355356
if (MEM_32bits() || (ofBits+mlBits+llBits > 56)) BIT_flushBits(&blockStream);
356-
if (longOffsets) {
357+
if (MEM_32bits() && longOffsets) {
357358
unsigned const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1);
358359
if (extraBits) {
359360
BIT_addBits(&blockStream, sequences[n].offBase, extraBits);

0 commit comments

Comments
 (0)