]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Add likely/unlikely hinting to all deflate algorithms.
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Wed, 1 Jul 2020 13:06:37 +0000 (15:06 +0200)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Fri, 3 Jul 2020 13:33:43 +0000 (15:33 +0200)
deflate_fast.c
deflate_medium.c
deflate_quick.c
deflate_slow.c

index 7f73da355afc6536a605ccfe19d78593715a2863..f2a4a59928457b00c859584954fd61fac63daace 100644 (file)
@@ -29,10 +29,10 @@ ZLIB_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
          */
         if (s->lookahead < MIN_LOOKAHEAD) {
             fill_window(s);
-            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
                 return need_more;
             }
-            if (s->lookahead == 0)
+            if (UNLIKELY(s->lookahead == 0))
                 break; /* flush the current block */
         }
 
@@ -88,15 +88,15 @@ ZLIB_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
             s->lookahead--;
             s->strstart++;
         }
-        if (bflush)
+        if (UNLIKELY(bflush))
             FLUSH_BLOCK(s, 0);
     }
     s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
-    if (flush == Z_FINISH) {
+    if (UNLIKELY(flush == Z_FINISH)) {
         FLUSH_BLOCK(s, 1);
         return finish_done;
     }
-    if (s->sym_next)
+    if (UNLIKELY(s->sym_next))
         FLUSH_BLOCK(s, 0);
     return block_done;
 }
index 4e3f1d890fcfccc72d05f410fd9a7185e65f2d1b..db155311423853167e216d67fc9219dc0f69cfdb 100644 (file)
@@ -47,10 +47,10 @@ static void insert_match(deflate_state *s, struct match match) {
         return;
 
     /* matches that are not long enough we need to emit as literals */
-    if (match.match_length < MIN_MATCH) {
+    if (LIKELY(match.match_length < MIN_MATCH)) {
         match.strstart++;
         match.match_length--;
-        if (match.match_length > 0) {
+        if (UNLIKELY(match.match_length > 0)) {
             if (match.strstart >= match.orgstart) {
                 if (match.strstart + match.match_length - 1 >= match.orgstart) {
                     functable.insert_string(s, match.strstart, match.match_length);
@@ -130,13 +130,13 @@ static void fizzle_matches(deflate_state *s, struct match *current, struct match
     orig = s->window + n.strstart - 1;
 
     while (*match == *orig) {
-        if (c.match_length < 1)
+        if (UNLIKELY(c.match_length < 1))
             break;
-        if (n.strstart <= limit)
+        if (UNLIKELY(n.strstart <= limit))
             break;
-        if (n.match_length >= 256)
+        if (UNLIKELY(n.match_length >= 256))
             break;
-        if (n.match_start <= 1)
+        if (UNLIKELY(n.match_start <= 1))
             break;
 
         n.strstart--;
@@ -182,7 +182,7 @@ ZLIB_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
                 return need_more;
             }
-            if (s->lookahead == 0)
+            if (UNLIKELY(s->lookahead == 0))
                 break; /* flush the current block */
             next_match.match_length = 0;
         }
@@ -215,9 +215,9 @@ ZLIB_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
                  */
                 current_match.match_length = functable.longest_match(s, hash_head);
                 current_match.match_start = s->match_start;
-                if (current_match.match_length < MIN_MATCH)
+                if (UNLIKELY(current_match.match_length < MIN_MATCH))
                     current_match.match_length = 1;
-                if (current_match.match_start >= current_match.strstart) {
+                if (UNLIKELY(current_match.match_start >= current_match.strstart)) {
                     /* this can happen due to some restarts */
                     current_match.match_length = 1;
                 }
@@ -231,7 +231,7 @@ ZLIB_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
         insert_match(s, current_match);
 
         /* now, look ahead one */
-        if (s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD)) {
+        if (LIKELY(s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD))) {
             s->strstart = current_match.strstart + current_match.match_length;
             hash_head = functable.quick_insert_string(s, s->strstart);
 
@@ -248,7 +248,7 @@ ZLIB_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
                  */
                 next_match.match_length = functable.longest_match(s, hash_head);
                 next_match.match_start = s->match_start;
-                if (next_match.match_start >= next_match.strstart) {
+                if (UNLIKELY(next_match.match_start >= next_match.strstart)) {
                     /* this can happen due to some restarts */
                     next_match.match_length = 1;
                 }
@@ -273,7 +273,7 @@ ZLIB_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
         /* move the "cursor" forward */
         s->strstart += current_match.match_length;
 
-        if (bflush)
+        if (UNLIKELY(bflush))
             FLUSH_BLOCK(s, 0);
     }
     s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
@@ -281,7 +281,7 @@ ZLIB_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
         FLUSH_BLOCK(s, 1);
         return finish_done;
     }
-    if (s->sym_next)
+    if (UNLIKELY(s->sym_next))
         FLUSH_BLOCK(s, 0);
 
     return block_done;
index faa90b2783e6ed4a1473f725abdc0fa3ada3fd51..4c775300d72b2bc7ccc725cf74938ea8c6164c4f 100644 (file)
@@ -47,41 +47,41 @@ ZLIB_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
 
 
     last = (flush == Z_FINISH) ? 1 : 0;
-    if (last && s->block_open != 2) {
+    if (UNLIKELY(last && s->block_open != 2)) {
         /* Emit end of previous block */
         QUICK_END_BLOCK(s, 0);
         /* Emit start of last block */
         QUICK_START_BLOCK(s, last);
-    } else if (s->block_open == 0 && s->lookahead > 0) {
+    } else if (UNLIKELY(s->block_open == 0 && s->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);
     }
 
     do {
-        if (s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size) {
+        if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
             flush_pending(s->strm);
             if (s->strm->avail_out == 0 && flush != Z_FINISH) {
                 return need_more;
             }
         }
 
-        if (s->lookahead < MIN_LOOKAHEAD) {
+        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
             fill_window(s);
-            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
                 return need_more;
             }
-            if (s->lookahead == 0)
+            if (UNLIKELY(s->lookahead == 0))
                 break;
 
-            if (s->block_open == 0) {
+            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);
             }
         }
 
-        if (s->lookahead >= MIN_MATCH) {
+        if (LIKELY(s->lookahead >= MIN_MATCH)) {
             hash_head = functable.quick_insert_string(s, s->strstart);
             dist = s->strstart - hash_head;
 
@@ -89,7 +89,7 @@ ZLIB_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
                 match_len = functable.compare258(s->window + s->strstart, s->window + hash_head);
 
                 if (match_len >= MIN_MATCH) {
-                    if (match_len > s->lookahead)
+                    if (UNLIKELY(match_len > s->lookahead))
                         match_len = s->lookahead;
 
                     check_match(s, s->strstart, hash_head, match_len);
@@ -109,7 +109,7 @@ ZLIB_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
 
     s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
 
-    if (last) {
+    if (UNLIKELY(last)) {
         if (s->strm->avail_out == 0)
             return s->strm->avail_in == 0 ? finish_started : need_more;
 
index 0e28e3503bf3ab7c46c1be42964f690f406c3c09..b79bc7eaff4998b380216acdc03ec1a3757e9f21 100644 (file)
@@ -28,10 +28,10 @@ ZLIB_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
          */
         if (s->lookahead < MIN_LOOKAHEAD) {
             fill_window(s);
-            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
                 return need_more;
             }
-            if (s->lookahead == 0)
+            if (UNLIKELY(s->lookahead == 0))
                 break; /* flush the current block */
         }
 
@@ -39,7 +39,7 @@ ZLIB_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
          * dictionary, and set hash_head to the head of the hash chain:
          */
         hash_head = NIL;
-        if (s->lookahead >= MIN_MATCH) {
+        if (LIKELY(s->lookahead >= MIN_MATCH)) {
             hash_head = functable.quick_insert_string(s, s->strstart);
         }
 
@@ -93,7 +93,7 @@ ZLIB_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
             s->match_available = 0;
             s->strstart += mov_fwd + 1;
 
-            if (bflush)
+            if (UNLIKELY(bflush))
                 FLUSH_BLOCK(s, 0);
 
         } else if (s->match_available) {
@@ -102,12 +102,12 @@ ZLIB_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
              * is longer, truncate the previous match to a single literal.
              */
             bflush = zng_tr_tally_lit(s, s->window[s->strstart-1]);
-            if (bflush)
+            if (UNLIKELY(bflush))
                 FLUSH_BLOCK_ONLY(s, 0);
             s->prev_length = match_len;
             s->strstart++;
             s->lookahead--;
-            if (s->strm->avail_out == 0)
+            if (UNLIKELY(s->strm->avail_out == 0))
                 return need_more;
         } else {
             /* There is no previous match to compare with, wait for
@@ -120,16 +120,16 @@ ZLIB_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
         }
     }
     Assert(flush != Z_NO_FLUSH, "no flush?");
-    if (s->match_available) {
+    if (UNLIKELY(s->match_available)) {
         (void) zng_tr_tally_lit(s, s->window[s->strstart-1]);
         s->match_available = 0;
     }
     s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
-    if (flush == Z_FINISH) {
+    if (UNLIKELY(flush == Z_FINISH)) {
         FLUSH_BLOCK(s, 1);
         return finish_done;
     }
-    if (s->sym_next)
+    if (UNLIKELY(s->sym_next))
         FLUSH_BLOCK(s, 0);
     return block_done;
 }