@@ -1689,11 +1689,8 @@ do { \
16891689/*
16901690 * Choose the best type of block to use (dynamic Huffman, static Huffman, or
16911691 * uncompressed), then output it.
1692- *
1693- * Returns the number of bits the block header used (or would have used, if the
1694- * block was output as a dynamic Huffman block).
16951692 */
1696- static u32
1693+ static void
16971694deflate_flush_block (struct libdeflate_compressor * c ,
16981695 struct deflate_output_bitstream * os ,
16991696 const u8 * block_begin , u32 block_length ,
@@ -1714,7 +1711,6 @@ deflate_flush_block(struct libdeflate_compressor *c,
17141711 unsigned bitcount = os -> bitcount ;
17151712 u8 * out_next = os -> next ;
17161713 u8 * const out_end = os -> end ;
1717- u32 hdr_bits ;
17181714 /* The cost for each block type, in bits */
17191715 u32 dynamic_cost = 0 ;
17201716 u32 static_cost = 0 ;
@@ -1733,14 +1729,13 @@ deflate_flush_block(struct libdeflate_compressor *c,
17331729 deflate_precompute_huffman_header (c );
17341730
17351731 /* Account for the cost of encoding dynamic Huffman codes. */
1736- hdr_bits = 5 + 5 + 4 + (3 * c -> o .precode .num_explicit_lens );
1732+ dynamic_cost + = 5 + 5 + 4 + (3 * c -> o .precode .num_explicit_lens );
17371733 for (sym = 0 ; sym < DEFLATE_NUM_PRECODE_SYMS ; sym ++ ) {
17381734 u32 extra = deflate_extra_precode_bits [sym ];
17391735
1740- hdr_bits += c -> o .precode .freqs [sym ] *
1741- (extra + c -> o .precode .lens [sym ]);
1736+ dynamic_cost += c -> o .precode .freqs [sym ] *
1737+ (extra + c -> o .precode .lens [sym ]);
17421738 }
1743- dynamic_cost = hdr_bits ;
17441739
17451740 /* Account for the cost of encoding literals. */
17461741 for (sym = 0 ; sym < 144 ; sym ++ ) {
@@ -2009,7 +2004,6 @@ deflate_flush_block(struct libdeflate_compressor *c,
20092004 os -> bitbuf = bitbuf ;
20102005 os -> bitcount = bitcount ;
20112006 os -> next = out_next ;
2012- return hdr_bits ;
20132007}
20142008
20152009static void
@@ -2113,11 +2107,8 @@ merge_new_observations(struct block_split_stats *stats)
21132107 stats -> num_new_observations = 0 ;
21142108}
21152109
2116- #define DEFAULT_PREV_HDR_BITS 512
2117-
21182110static bool
2119- do_end_block_check (struct block_split_stats * stats , u32 block_length ,
2120- u32 prev_hdr_bits )
2111+ do_end_block_check (struct block_split_stats * stats , u32 block_length )
21212112{
21222113 if (stats -> num_observations > 0 ) {
21232114 /*
@@ -2148,23 +2139,14 @@ do_end_block_check(struct block_split_stats *stats, u32 block_length,
21482139
21492140 num_items = stats -> num_observations +
21502141 stats -> num_new_observations ;
2151-
21522142 /*
2153- * Heuristic: end the block when the sum of absolute differences
2154- * of probabilities becomes at least x/512, where x is roughly
2155- * 184-220, increasing as the estimated cost of the block header
2156- * increases (so that slightly longer blocks are used when block
2157- * headers are expensive). To estimate the cost of the block
2158- * header, we just use the cost of the previous block's header.
2159- *
2160- * As above, the probability is multiplied by both
2161- * num_new_observations and num_observations. Be careful to
2162- * avoid integer overflow.
2143+ * Heuristic: the cutoff is when the sum of absolute differences
2144+ * of probabilities becomes at least 200/512. As above, the
2145+ * probability is multiplied by both num_new_observations and
2146+ * num_observations. Be careful to avoid integer overflow.
21632147 */
2164- cutoff = stats -> num_new_observations *
2165- (184 + (prev_hdr_bits / 32 )) / 512 *
2148+ cutoff = stats -> num_new_observations * 200 / 512 *
21662149 stats -> num_observations ;
2167-
21682150 /*
21692151 * Very short blocks have a lot of overhead for the Huffman
21702152 * codes, so only use them if it clearly seems worthwhile.
@@ -2201,8 +2183,7 @@ should_end_block(struct block_split_stats *stats,
22012183 if (!ready_to_check_block (stats , in_block_begin , in_next , in_end ))
22022184 return false;
22032185
2204- return do_end_block_check (stats , in_next - in_block_begin ,
2205- DEFAULT_PREV_HDR_BITS );
2186+ return do_end_block_check (stats , in_next - in_block_begin );
22062187}
22072188
22082189/******************************************************************************/
@@ -3380,17 +3361,14 @@ deflate_find_min_cost_path(struct libdeflate_compressor *c,
33803361 *
33813362 * As an alternate strategy, also consider using only literals. The boolean
33823363 * returned in *used_only_literals indicates whether that strategy was best.
3383- *
3384- * *hdr_bits is set to the number of bits the block header used (or would have
3385- * used, if the block was output as a dynamic Huffman block).
33863364 */
33873365static void
33883366deflate_optimize_and_flush_block (struct libdeflate_compressor * c ,
33893367 struct deflate_output_bitstream * os ,
33903368 const u8 * block_begin , u32 block_length ,
33913369 const struct lz_match * cache_ptr ,
33923370 bool is_first_block , bool is_final_block ,
3393- bool * used_only_literals , u32 * hdr_bits )
3371+ bool * used_only_literals )
33943372{
33953373 unsigned num_passes_remaining = c -> p .n .max_optim_passes ;
33963374 u32 best_true_cost = UINT32_MAX ;
@@ -3474,8 +3452,8 @@ deflate_optimize_and_flush_block(struct libdeflate_compressor *c,
34743452 deflate_find_min_cost_path (c , block_length , cache_ptr );
34753453 deflate_set_costs_from_codes (c , & c -> codes .lens );
34763454 }
3477- * hdr_bits = deflate_flush_block (c , os , block_begin , block_length , seq ,
3478- is_final_block );
3455+ deflate_flush_block (c , os , block_begin , block_length , seq ,
3456+ is_final_block );
34793457}
34803458
34813459static void
@@ -3554,7 +3532,6 @@ deflate_compress_near_optimal(struct libdeflate_compressor * restrict c,
35543532 struct lz_match * cache_ptr = c -> p .n .match_cache ;
35553533 u32 next_hashes [2 ] = {0 , 0 };
35563534 bool prev_block_used_only_literals = false;
3557- u32 prev_hdr_bits = DEFAULT_PREV_HDR_BITS ;
35583535
35593536 bt_matchfinder_init (& c -> p .n .bt_mf );
35603537 deflate_near_optimal_init_stats (c );
@@ -3716,8 +3693,7 @@ deflate_compress_near_optimal(struct libdeflate_compressor * restrict c,
37163693 continue ;
37173694 /* Check if it would be worthwhile to end the block. */
37183695 if (do_end_block_check (& c -> split_stats ,
3719- in_next - in_block_begin ,
3720- prev_hdr_bits )) {
3696+ in_next - in_block_begin )) {
37213697 change_detected = true;
37223698 break ;
37233699 }
@@ -3764,8 +3740,7 @@ deflate_compress_near_optimal(struct libdeflate_compressor * restrict c,
37643740 c , os , in_block_begin ,
37653741 block_length , cache_ptr ,
37663742 is_first , is_final ,
3767- & prev_block_used_only_literals ,
3768- & prev_hdr_bits );
3743+ & prev_block_used_only_literals );
37693744 memmove (c -> p .n .match_cache , cache_ptr ,
37703745 cache_len_rewound * sizeof (* cache_ptr ));
37713746 cache_ptr = & c -> p .n .match_cache [cache_len_rewound ];
@@ -3791,8 +3766,7 @@ deflate_compress_near_optimal(struct libdeflate_compressor * restrict c,
37913766 c , os , in_block_begin ,
37923767 block_length , cache_ptr ,
37933768 is_first , is_final ,
3794- & prev_block_used_only_literals ,
3795- & prev_hdr_bits );
3769+ & prev_block_used_only_literals );
37963770 cache_ptr = & c -> p .n .match_cache [0 ];
37973771 deflate_near_optimal_save_stats (c );
37983772 deflate_near_optimal_init_stats (c );
0 commit comments