From: Hans Kristian Rosbach Date: Wed, 1 Jul 2020 13:06:37 +0000 (+0200) Subject: Add likely/unlikely hinting to all deflate algorithms. X-Git-Tag: 1.9.9-b1~149 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47b1208c443e86928ad64175d0b2654c107c202c;p=thirdparty%2Fzlib-ng.git Add likely/unlikely hinting to all deflate algorithms. --- diff --git a/deflate_fast.c b/deflate_fast.c index 7f73da35..f2a4a599 100644 --- a/deflate_fast.c +++ b/deflate_fast.c @@ -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; } diff --git a/deflate_medium.c b/deflate_medium.c index 4e3f1d89..db155311 100644 --- a/deflate_medium.c +++ b/deflate_medium.c @@ -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; diff --git a/deflate_quick.c b/deflate_quick.c index faa90b27..4c775300 100644 --- a/deflate_quick.c +++ b/deflate_quick.c @@ -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; diff --git a/deflate_slow.c b/deflate_slow.c index 0e28e350..b79bc7ea 100644 --- a/deflate_slow.c +++ b/deflate_slow.c @@ -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; }