]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
fix a minor inefficiency in compress_superblock 3668/head
authorYann Collet <cyan@fb.com>
Mon, 5 Jun 2023 16:51:52 +0000 (09:51 -0700)
committerYann Collet <cyan@fb.com>
Mon, 5 Jun 2023 16:51:52 +0000 (09:51 -0700)
and in `decodecorpus`:
the specific case `nbSeq=127` can be represented using the 1-byte format.
Note that both the 1-byte and the 2-bytes formats are valid to represent this case,
so there was no "error", produced data remains valid,
it's just that the 1-byte format is more efficient.

fix #3667

Credit to @ip7z for finding this issue.

doc/zstd_compression_format.md
lib/compress/zstd_compress_superblock.c
tests/decodecorpus.c

index 3843bf3905513b57da62efdc4c46a543f287620e..2a69c4c30ae95364a6bfea815e8db862af99501e 100644 (file)
@@ -655,8 +655,9 @@ Let's call its first byte `byte0`.
             Decompressed content is defined entirely as Literals Section content.
             The FSE tables used in `Repeat_Mode` aren't updated.
 - `if (byte0 < 128)` : `Number_of_Sequences = byte0` . Uses 1 byte.
-- `if (byte0 < 255)` : `Number_of_Sequences = ((byte0-128) << 8) + byte1` . Uses 2 bytes.
-- `if (byte0 == 255)`: `Number_of_Sequences = byte1 + (byte2<<8) + 0x7F00` . Uses 3 bytes.
+- `if (byte0 < 255)` : `Number_of_Sequences = ((byte0 - 0x80) << 8) + byte1`. Uses 2 bytes.
+            Note that the 2 bytes format fully overlaps the 1 byte format.
+- `if (byte0 == 255)`: `Number_of_Sequences = byte1 + (byte2<<8) + 0x7F00`. Uses 3 bytes.
 
 __Symbol compression modes__
 
index 638c4acbe708aa48437a1092616fe9fb6b4b59ba..dacaf85dbc2510a6fac2d475c6d15fd906b98c4b 100644 (file)
@@ -180,7 +180,7 @@ ZSTD_compressSubBlock_sequences(const ZSTD_fseCTables_t* fseTables,
     /* Sequences Header */
     RETURN_ERROR_IF((oend-op) < 3 /*max nbSeq Size*/ + 1 /*seqHead*/,
                     dstSize_tooSmall, "");
-    if (nbSeq < 0x7F)
+    if (nbSeq < 128)
         *op++ = (BYTE)nbSeq;
     else if (nbSeq < LONGNBSEQ)
         op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2;
index e48eccd6df74ff9de5a5fc2a77a41bdd4d2057f7..a440ae38af274bf392fdbec593ace5b870ac55ef 100644 (file)
@@ -825,7 +825,7 @@ static size_t writeSequences(U32* seed, frame_t* frame, seqStore_t* seqStorePtr,
 
     /* Sequences Header */
     if ((oend-op) < 3 /*max nbSeq Size*/ + 1 /*seqHead */) return ERROR(dstSize_tooSmall);
-    if (nbSeq < 0x7F) *op++ = (BYTE)nbSeq;
+    if (nbSeq < 128) *op++ = (BYTE)nbSeq;
     else if (nbSeq < LONGNBSEQ) op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2;
     else op[0]=0xFF, MEM_writeLE16(op+1, (U16)(nbSeq - LONGNBSEQ)), op+=3;