]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Cleanup deflate_quick variable reuse and call signatures
authorNathan Moin Vaziri <nathan@nathanm.com>
Tue, 12 May 2026 06:24:09 +0000 (23:24 -0700)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 17 Jun 2026 09:40:48 +0000 (11:40 +0200)
Drop the lc local: the four-byte window read is hoisted above the
match-search branch, and the literal-emit takes the low byte of
str_val directly so the short-lookahead arm no longer needs its own
load.  Pass strstart into quick_start_block and quick_end_block so
they no longer reach into s for block_start.  In the early-finish
arm, pass 1 to quick_start_block since last is known to be 1.

deflate_quick.c

index e6f67cdec2e2e72b861cab2707e7e964bbbc6ec0..16666247135a9c3fc4702f65f474ff8f1a4f686b 100644 (file)
 extern const ct_data static_ltree[L_CODES+2];
 extern const ct_data static_dtree[D_CODES];
 
-Z_FORCEINLINE static void quick_start_block(deflate_state *s, int last) {
+Z_FORCEINLINE static void quick_start_block(deflate_state *s, uint32_t strstart, int last) {
     zng_tr_emit_tree(s, STATIC_TREES, last);
     s->block_open = 1 + last;
-    s->block_start = (int)s->strstart;
+    s->block_start = (int)strstart;
 }
 
-Z_FORCEINLINE static int quick_end_block(deflate_state *s, int last) {
+Z_FORCEINLINE static int quick_end_block(deflate_state *s, uint32_t strstart, int last) {
     if (s->block_open) {
         zng_tr_emit_end_block(s, static_ltree, last);
         s->block_open = 0;
-        s->block_start = (int)s->strstart;
+        s->block_start = (int)strstart;
         PREFIX(flush_pending)(s->strm);
         return (s->strm->avail_out == 0);
     }
     return 0;
 }
 
-Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
+Z_FORCEINLINE static block_state deflate_quick_impl(deflate_state *s, int flush,
+                                                   uint32_t strstart, uint32_t lookahead) {
     unsigned char *window;
     unsigned last = (flush == Z_FINISH) ? 1 : 0;
-    unsigned int lookahead = s->lookahead;
-    unsigned int strstart = s->strstart;
 
     if (UNLIKELY(last && s->block_open != 2)) {
         /* Emit end of previous block */
-        if (quick_end_block(s, 0))
+        if (quick_end_block(s, strstart, 0))
             return need_more;
         /* Emit start of last block */
-        quick_start_block(s, last);
+        quick_start_block(s, strstart, last);
     } else if (UNLIKELY(s->block_open == 0 && lookahead > 0)) {
         /* Start new block only when we have lookahead data, so that if no
            input data is given an empty block will not be written */
-        quick_start_block(s, last);
+        quick_start_block(s, strstart, last);
     }
 
     window = s->window;
 
     for (;;) {
-        uint8_t lc;
-
         if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
             PREFIX(flush_pending)(s->strm);
             if (s->strm->avail_out == 0) {
@@ -91,23 +88,23 @@ Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
             if (UNLIKELY(s->block_open == 0)) {
                 /* Start new block when we have lookahead data, so that if no
                    input data is given an empty block will not be written */
-                quick_start_block(s, last);
+                quick_start_block(s, strstart, last);
             }
         }
 
+        uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + strstart));
+
         if (LIKELY(lookahead >= WANT_MIN_MATCH)) {
-            uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + strstart));
             uint32_t hash_head = quick_insert_value(s, strstart, str_val);
             int64_t dist = (int64_t)strstart - hash_head;
-            lc = (uint8_t)str_val;
 
             if (dist <= MAX_DIST(s) && dist > 0) {
                 const uint8_t *match_start = window + hash_head;
                 uint32_t match_val = Z_U32_FROM_LE(zng_memread_4(match_start));
 
                 if (str_val == match_val) {
-                    const uint8_t *str_start = window + strstart;
-                    uint32_t match_len = FUNCTABLE_CALL(compare256)(str_start+2, match_start+2) + 2;
+                    const uint8_t *scan_start = window + strstart;
+                    uint32_t match_len = FUNCTABLE_CALL(compare256)(scan_start+2, match_start+2) + 2;
 
                     if (match_len >= WANT_MIN_MATCH) {
                         if (UNLIKELY(match_len > lookahead))
@@ -124,10 +121,9 @@ Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
                     }
                 }
             }
-        } else {
-            lc = window[strstart];
         }
-        zng_tr_emit_lit(s, static_ltree, lc);
+
+        zng_tr_emit_lit(s, static_ltree, (uint8_t)str_val);
         strstart++;
         lookahead--;
     }
@@ -136,12 +132,16 @@ Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
     s->strstart = strstart;
     s->insert = strstart < (STD_MIN_MATCH - 1) ? strstart : (STD_MIN_MATCH - 1);
     if (UNLIKELY(last)) {
-        if (quick_end_block(s, 1))
+        if (quick_end_block(s, strstart, 1))
             return finish_started;
         return finish_done;
     }
 
-    if (quick_end_block(s, 0))
+    if (quick_end_block(s, strstart, 0))
         return need_more;
     return block_done;
 }
+
+Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
+    return deflate_quick_impl(s, flush, s->strstart, s->lookahead);
+}