From 8b0c3204a067e8874b0ea4649d072f3673f636c1 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 27 Oct 2026 02:13:21 +0000 Subject: [PATCH 1/6] std/brotli: add large-window encoding at qualities 3 to 9 Port the composite hashers H35, H55 and H65 (hash_composite_inc.h) and the rolling hashers HROLLING_FAST and HROLLING (hash_rolling_inc.h), which ChooseHasher picks for lgwin above 24, and drop the "#unsupported encoder quality" gate for the large-window quirk at qualities 3 to 9. The workbuf's hasher region now includes the rolling hasher's 64 MiB table when a composite hasher can be chosen. Extend the encoder parity harness with a large-window grid (lgwin 10 to 30 at qualities 2 to 9) and a -lw option that runs only that grid. --- release/c/wuffs-unsupported-snapshot.c | 269 ++++++++++++++++++++- script/manual-test-brotli-encoder-parity.c | 108 ++++++++- std/brotli/encode_brotli.wuffs | 17 +- std/brotli/encode_data.wuffs | 20 +- std/brotli/encode_hasher.wuffs | 45 +++- std/brotli/encode_hasher_composite.wuffs | 208 ++++++++++++++++ 6 files changed, 643 insertions(+), 24 deletions(-) create mode 100644 std/brotli/encode_hasher_composite.wuffs diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index e5b35dc4..c78ddcfb 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -10025,6 +10025,12 @@ struct wuffs_brotli__encoder__struct { uint64_t f_dict_num_lookups; uint64_t f_dict_num_matches; uint64_t f_fc_max_hops; + uint32_t f_hr_jump; + bool f_hr_fresh; + uint32_t f_hr_state; + uint32_t f_hr_factor_remove; + uint64_t f_hr_next_ix; + uint64_t f_hr_off; uint64_t f_sr_len; uint64_t f_sr_distance; uint64_t f_sr_score; @@ -68546,6 +68552,16 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = { #define WUFFS_BROTLI__SCORE_BASE 1920u +#define WUFFS_BROTLI__ROLLING_CHUNKLEN 32u + +#define WUFFS_BROTLI__ROLLING_NUMBUCKETS 16777216u + +#define WUFFS_BROTLI__ROLLING_MASK 1073741823u + +#define WUFFS_BROTLI__ROLLING_HASH_MUL_32 69069u + +#define WUFFS_BROTLI__ROLLING_INVALID_POS 4294967295u + #define WUFFS_BROTLI__MAX_NUMBER_OF_HISTOGRAMS 256u #define WUFFS_BROTLI__INVALID_INDEX 4294967295u @@ -70596,6 +70612,42 @@ wuffs_brotli__encoder__find_longest_match_forgetful( uint64_t a_dictionary_distance, uint64_t a_max_distance); +WUFFS_BASE__GENERATED_C_CODE +static uint64_t +wuffs_brotli__encoder__hasher_rolling_alloc_size( + const wuffs_brotli__encoder* self); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_brotli__encoder__hasher_rolling_initialize( + wuffs_brotli__encoder* self, + wuffs_base__slice_u8 a_wb); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_brotli__encoder__hasher_rolling_prepare( + wuffs_brotli__encoder* self, + wuffs_base__slice_u8 a_wb, + uint64_t a_input_size, + uint64_t a_data_off); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_brotli__encoder__hasher_rolling_stitch_to_previous_block( + wuffs_brotli__encoder* self, + wuffs_base__slice_u8 a_wb, + uint64_t a_num_bytes, + uint64_t a_position); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_brotli__encoder__hasher_rolling_find_longest_match( + wuffs_brotli__encoder* self, + wuffs_base__slice_u8 a_wb, + uint64_t a_cur_ix, + uint64_t a_max_length, + uint64_t a_max_backward); + WUFFS_BASE__GENERATED_C_CODE static wuffs_base__empty_struct wuffs_brotli__bit_cost__histogram_clear( @@ -78813,7 +78865,7 @@ wuffs_brotli__encoder__workbuf_len( v_lgwin = wuffs_brotli__encoder__sanitized_lgwin(self, v_q); if (v_q <= 1u) { v_n = ((((uint64_t)(2u)) << v_lgwin) + 503u + 16u); - } else if ((v_q <= 9u) && ((v_q == 2u) || (self->private_impl.f_p_large_window == 0u))) { + } else if (v_q <= 9u) { v_n = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lgwin, @@ -79336,7 +79388,7 @@ wuffs_brotli__encoder__transform_io( WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; wuffs_brotli__encoder__ensure_initialized(self); - if ((self->private_impl.f_quality > 9u) || (self->private_impl.f_large_window && (self->private_impl.f_quality > 2u))) { + if (self->private_impl.f_quality > 9u) { status = wuffs_base__make_status(wuffs_brotli__error__unsupported_encoder_quality); goto exit; } else if ((self->private_impl.f_quality >= 2u) && (((uint64_t)(a_workbuf.len)) < self->private_impl.f_workbuf_end)) { @@ -80332,6 +80384,7 @@ wuffs_brotli__encoder__hasher_total_size( uint32_t v_htype = 0; uint32_t v_bucket_bits = 0; uint32_t v_block_bits = 0; + uint64_t v_rolling = 0; if ((a_quality == 4u) && (a_size_hint >= 1048576u)) { v_htype = 54u; @@ -80360,7 +80413,16 @@ wuffs_brotli__encoder__hasher_total_size( } v_block_bits = (a_quality - 1u); } - return ((uint64_t)(((uint64_t)(wuffs_brotli__encoder__hasher_alloc_size(self, + v_rolling = 0u; + if (a_lgwin > 24u) { + if ((v_htype == 3u) || (v_htype == 54u)) { + v_rolling = wuffs_brotli__encoder__hasher_rolling_alloc_size(self); + } else if ((v_htype == 6u) || (v_htype == 68u)) { + v_htype = 6u; + v_rolling = wuffs_brotli__encoder__hasher_rolling_alloc_size(self); + } + } + return ((uint64_t)(((uint64_t)(((uint64_t)(wuffs_brotli__encoder__hasher_alloc_size(self, v_htype, v_bucket_bits, v_block_bits, @@ -80372,7 +80434,7 @@ wuffs_brotli__encoder__hasher_total_size( v_htype, v_bucket_bits, v_block_bits, - 2u))); + 2u))) + v_rolling)); } // -------- func brotli.encoder.hash_region_size @@ -83855,6 +83917,15 @@ wuffs_brotli__encoder__choose_hasher( self->private_impl.f_h_num_last_distances = 10u; } } + self->private_impl.f_hr_jump = 0u; + if (self->private_impl.f_lgwin > 24u) { + if ((self->private_impl.f_hasher_type == 3u) || (self->private_impl.f_hasher_type == 54u)) { + self->private_impl.f_hr_jump = 4u; + } else if ((self->private_impl.f_hasher_type == 6u) || (self->private_impl.f_hasher_type == 68u)) { + self->private_impl.f_hasher_type = 6u; + self->private_impl.f_hr_jump = 1u; + } + } return wuffs_base__make_empty_struct(); } @@ -83948,6 +84019,8 @@ wuffs_brotli__encoder__hasher_setup( self->private_impl.f_h_bucket_bits, self->private_impl.f_h_block_bits, 1u))); + self->private_impl.f_hr_off = self->private_impl.f_h_off2; + self->private_impl.f_hr_fresh = true; self->private_impl.f_h_is_prepared = false; self->private_impl.f_h_is_setup = true; } @@ -84067,6 +84140,13 @@ wuffs_brotli__encoder__hasher_prepare( v_j += 1u; } } + if (self->private_impl.f_hr_jump != 0u) { + if (self->private_impl.f_hr_fresh) { + self->private_impl.f_hr_fresh = false; + wuffs_brotli__encoder__hasher_rolling_initialize(self, a_wb); + } + wuffs_brotli__encoder__hasher_rolling_prepare(self, a_wb, a_input_size, self->private_impl.f_rb_buf_off); + } return wuffs_base__make_empty_struct(); } @@ -84236,6 +84316,9 @@ wuffs_brotli__encoder__hasher_stitch_to_previous_block( wuffs_brotli__encoder__hasher_store(self, a_wb, ((uint64_t)(a_position - 2u))); wuffs_brotli__encoder__hasher_store(self, a_wb, ((uint64_t)(a_position - 1u))); } + if (self->private_impl.f_hr_jump != 0u) { + wuffs_brotli__encoder__hasher_rolling_stitch_to_previous_block(self, a_wb, a_num_bytes, a_position); + } return wuffs_base__make_empty_struct(); } @@ -84488,6 +84571,13 @@ wuffs_brotli__encoder__find_longest_match( a_dictionary_distance, a_max_distance); } + if (self->private_impl.f_hr_jump != 0u) { + wuffs_brotli__encoder__hasher_rolling_find_longest_match(self, + a_wb, + a_cur_ix, + a_max_length, + a_max_backward); + } return wuffs_base__make_empty_struct(); } @@ -85084,6 +85174,177 @@ wuffs_brotli__encoder__find_longest_match_forgetful( return wuffs_base__make_empty_struct(); } +// -------- func brotli.encoder.hasher_rolling_alloc_size + +WUFFS_BASE__GENERATED_C_CODE +static uint64_t +wuffs_brotli__encoder__hasher_rolling_alloc_size( + const wuffs_brotli__encoder* self) { + return (4u * ((uint64_t)(16777216u))); +} + +// -------- func brotli.encoder.hasher_rolling_initialize + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_brotli__encoder__hasher_rolling_initialize( + wuffs_brotli__encoder* self, + wuffs_base__slice_u8 a_wb) { + uint64_t v_i = 0; + + self->private_impl.f_hr_state = 0u; + self->private_impl.f_hr_next_ix = 0u; + self->private_impl.f_hr_factor_remove = 1u; + v_i = 0u; + while (v_i < 32u) { + self->private_impl.f_hr_factor_remove *= 69069u; + v_i += ((uint64_t)(self->private_impl.f_hr_jump)); + } + wuffs_brotli__encoder__fill_bytes(self, + a_wb, + self->private_impl.f_hr_off, + wuffs_brotli__encoder__hasher_rolling_alloc_size(self), + 255u); + return wuffs_base__make_empty_struct(); +} + +// -------- func brotli.encoder.hasher_rolling_prepare + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_brotli__encoder__hasher_rolling_prepare( + wuffs_brotli__encoder* self, + wuffs_base__slice_u8 a_wb, + uint64_t a_input_size, + uint64_t a_data_off) { + uint64_t v_i = 0; + + if (a_input_size < 32u) { + return wuffs_base__make_empty_struct(); + } + self->private_impl.f_hr_state = 0u; + v_i = 0u; + while (v_i < 32u) { + self->private_impl.f_hr_state = ((uint32_t)(((uint32_t)(69069u * self->private_impl.f_hr_state)) + (((uint32_t)(wuffs_brotli__encoder__byte_at(self, a_wb, ((uint64_t)(a_data_off + v_i))))) + 1u))); + v_i += ((uint64_t)(self->private_impl.f_hr_jump)); + } + return wuffs_base__make_empty_struct(); +} + +// -------- func brotli.encoder.hasher_rolling_stitch_to_previous_block + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_brotli__encoder__hasher_rolling_stitch_to_previous_block( + wuffs_brotli__encoder* self, + wuffs_base__slice_u8 a_wb, + uint64_t a_num_bytes, + uint64_t a_position) { + uint64_t v_position = 0; + uint64_t v_position_masked = 0; + uint64_t v_available = 0; + uint64_t v_jm1 = 0; + uint64_t v_diff = 0; + uint64_t v_mask = 0; + + v_position = a_position; + v_available = a_num_bytes; + v_jm1 = ((uint64_t)(((uint64_t)(self->private_impl.f_hr_jump)) - 1u)); + if ((v_position & v_jm1) != 0u) { + v_diff = ((uint64_t)(((uint64_t)(self->private_impl.f_hr_jump)) - (v_position & v_jm1))); + if (v_diff > v_available) { + v_available = 0u; + } else { + v_available -= v_diff; + } + v_position += v_diff; + } + v_mask = ((uint64_t)(self->private_impl.f_rb_mask)); + v_position_masked = (v_position & v_mask); + if (v_available > ((uint64_t)(v_mask - v_position_masked))) { + v_available = ((uint64_t)(v_mask - v_position_masked)); + } + wuffs_brotli__encoder__hasher_rolling_prepare(self, a_wb, v_available, ((uint64_t)(self->private_impl.f_rb_buf_off + v_position_masked))); + self->private_impl.f_hr_next_ix = v_position; + return wuffs_base__make_empty_struct(); +} + +// -------- func brotli.encoder.hasher_rolling_find_longest_match + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_brotli__encoder__hasher_rolling_find_longest_match( + wuffs_brotli__encoder* self, + wuffs_base__slice_u8 a_wb, + uint64_t a_cur_ix, + uint64_t a_max_length, + uint64_t a_max_backward) { + wuffs_base__slice_u8 v_data = {0}; + uint64_t v_mask = 0; + uint64_t v_cur_ix_masked = 0; + uint64_t v_jump = 0; + uint64_t v_pos = 0; + uint32_t v_code = 0; + uint8_t v_rem = 0; + uint8_t v_add = 0; + uint64_t v_found_ix = 0; + uint64_t v_backward = 0; + uint64_t v_found_ix_masked = 0; + uint64_t v_len = 0; + uint64_t v_score = 0; + uint64_t v_t = 0; + + if (self->private_impl.f_rb_buf_off > ((uint64_t)(a_wb.len))) { + return wuffs_base__make_empty_struct(); + } + v_data = wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_rb_buf_off); + v_mask = ((uint64_t)(self->private_impl.f_rb_mask)); + v_cur_ix_masked = (a_cur_ix & v_mask); + v_jump = ((uint64_t)(self->private_impl.f_hr_jump)); + if ((a_cur_ix & ((uint64_t)(v_jump - 1u))) != 0u) { + return wuffs_base__make_empty_struct(); + } + if (a_max_length < 32u) { + return wuffs_base__make_empty_struct(); + } + v_pos = self->private_impl.f_hr_next_ix; + while (v_pos <= a_cur_ix) { + v_code = (self->private_impl.f_hr_state & 1073741823u); + v_rem = wuffs_brotli__encoder__byte_at(self, v_data, (v_pos & v_mask)); + v_add = wuffs_brotli__encoder__byte_at(self, v_data, (((uint64_t)(v_pos + 32u)) & v_mask)); + v_found_ix = ((uint64_t)(4294967295u)); + self->private_impl.f_hr_state = ((uint32_t)(((uint32_t)(((uint32_t)(69069u * self->private_impl.f_hr_state)) + (((uint32_t)(v_add)) + 1u))) - ((uint32_t)(self->private_impl.f_hr_factor_remove * (((uint32_t)(v_rem)) + 1u))))); + if (v_code < 16777216u) { + v_t = ((uint64_t)(self->private_impl.f_hr_off + (4u * ((uint64_t)(v_code))))); + v_found_ix = ((uint64_t)(wuffs_brotli__encoder__load32le(self, a_wb, v_t))); + wuffs_brotli__encoder__poke_u32(self, a_wb, v_t, ((uint32_t)(v_pos))); + if ((v_pos == a_cur_ix) && (v_found_ix != ((uint64_t)(4294967295u)))) { + v_backward = (((uint64_t)(a_cur_ix - v_found_ix)) & 4294967295u); + if (v_backward <= a_max_backward) { + v_found_ix_masked = (v_found_ix & v_mask); + v_len = wuffs_brotli__encoder__find_match_length_with_limit(self, + v_data, + v_found_ix_masked, + v_cur_ix_masked, + a_max_length); + if ((v_len >= 4u) && (v_len > self->private_impl.f_sr_len)) { + v_score = wuffs_brotli__encoder__backward_reference_score(self, v_len, v_backward); + if (v_score > self->private_impl.f_sr_score) { + self->private_impl.f_sr_len = v_len; + self->private_impl.f_sr_distance = v_backward; + self->private_impl.f_sr_score = v_score; + self->private_impl.f_sr_len_code_delta = 0u; + } + } + } + } + } + v_pos += v_jump; + } + self->private_impl.f_hr_next_ix = ((uint64_t)(a_cur_ix + v_jump)); + return wuffs_base__make_empty_struct(); +} + // -------- func brotli.bit_cost.histogram_clear WUFFS_BASE__GENERATED_C_CODE diff --git a/script/manual-test-brotli-encoder-parity.c b/script/manual-test-brotli-encoder-parity.c index 9d1d5ba7..c7347037 100644 --- a/script/manual-test-brotli-encoder-parity.c +++ b/script/manual-test-brotli-encoder-parity.c @@ -96,6 +96,7 @@ static uint64_t g_num_mismatches; static uint32_t g_qmin = 0; static uint32_t g_qmax = 9; +static bool g_lw_only = false; typedef struct { uint32_t keys[MAX_PARAMS]; @@ -543,6 +544,93 @@ run_input_q01(const char* name, const uint8_t* data, size_t len, uint32_t q) { } } +// run_input_lw runs the large-window (BROTLI_PARAM_LARGE_WINDOW) grid for a +// quality from 2 to 9. At quality 2 (and below), the C code ignores the +// large-window parameter. At qualities 3 to 9, lgwin can be up to 30 and lgwin +// above 24 selects the composite hashers (H35, H55 and H65) instead of H3, +// H54, H6 and H68 (and only those: H5, H58 and H40 to H42 stay as they are). +static void // +run_input_lw(const char* name, const uint8_t* data, size_t len, uint32_t q) { + static const size_t metadata_lens[] = {0, 1, 100, 70000}; + bool big = len > (512 << 10); + + // lgwin and chunking. + static const uint32_t lgwins[] = {10, 18, 24, 25, 26, 27, 28, 29, 30}; + for (size_t w = 0; w < 9; w++) { + if (big && (lgwins[w] != 24) && (lgwins[w] != 25) && (lgwins[w] != 28) && + (lgwins[w] != 30)) { + continue; + } + for (uint32_t ch = 0; ch < 5; ch += 4) { + for (uint32_t fl = 0; fl < 3; fl += 2) { + if (big && (fl == 0)) { + continue; + } + config c = make_config(q, lgwins[w]); + add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW. + c.chunking = ch; + c.flush = fl; + c.seed = 0x10000 | (w << 8) | (ch << 4) | fl | (q << 12); + check(name, &c, data, len); + } + } + } + + // Other parameters, alone and in combination. + static const uint32_t extras[][6] = { + // Pairs of (BrotliEncoderParameter, value). A zero key ends the list. + {5, 1u << 20}, // BROTLI_PARAM_SIZE_HINT: H55 and H65. + {5, 0xFFFFFFFF}, // Replaced by the exact size. + {5, 1u << 30}, // + {3, 16}, // BROTLI_PARAM_LGBLOCK. + {3, 18}, // + {3, 24, 5, 1u << 21}, // ... and a big size hint. + {4, 1, 5, 1u << 20}, // DISABLE_LITERAL_CONTEXT_MODELING. + {7, 3, 8, 120}, // BROTLI_PARAM_NPOSTFIX and NDIRECT. + {7, 1, 8, 4, 5, 1u << 20}, // + {0, 2}, // FONT mode. + {0, 1, 5, 1u << 22}, // TEXT mode and a big size hint. + {9, 1000}, // BROTLI_PARAM_STREAM_OFFSET. + {9, 1u << 30, 5, 1u << 20}, + {2, 0xFFFFFFFF}, // LGWIN (int)-1, so 10. + {2, 31}, // LGWIN 31, so 30. + }; + for (size_t i = 0; i < sizeof extras / sizeof extras[0]; i++) { + for (size_t w = 0; w < 2; w++) { + static const uint32_t ws[] = {25, 30}; + if (big && (w != 0)) { + continue; + } + config c = make_config(q, ws[w]); + add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW. + for (size_t k = 0; (k < 6) && ((k == 0) || extras[i][k]); k += 2) { + uint32_t v = extras[i][k + 1]; + if ((extras[i][k] == 5) && (v == 0xFFFFFFFF)) { + v = (uint32_t)len; + } + add_param(&c, extras[i][k], v); + } + c.chunking = 4; + c.flush = 2; + c.seed = 0x20000 | i | (w << 8) | (q << 12); + check(name, &c, data, len); + } + } + + // Metadata blocks and limited dst lengths. + for (size_t i = 0; i < 4; i += 3) { + config c = make_config(q, 26); + add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW. + c.chunking = 4; + c.flush = 2; + c.metadata_position = 2; + c.metadata_len = metadata_lens[i]; + c.dst_chunk = (len <= (32 << 10)) ? 3 : 4096; + c.seed = 0x30000 | i | (q << 12); + check(name, &c, data, len); + } +} + // run_input_q2 runs the grid for a quality from 2 to 9. static void // run_input_q2(const char* name, const uint8_t* data, size_t len, uint32_t q) { @@ -649,17 +737,7 @@ run_input_q2(const char* name, const uint8_t* data, size_t len, uint32_t q) { } } } - if (q == 2) { - // Large window is supported (by the Wuffs encoder) only at quality 2. - for (uint32_t w = 22; w <= 30; w += 4) { - config c = make_config(q, w); - add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW. - c.chunking = 4; - c.flush = 2; - c.seed = w; - check(name, &c, data, len); - } - } + run_input_lw(name, data, len, q); if (len <= (256 << 10)) { static const size_t dst_chunks[] = {1, 2, 3, 7, 64, 4096}; for (size_t i = 0; i < 6; i++) { @@ -684,7 +762,11 @@ run_input(const char* name, const uint8_t* data, size_t len) { uint64_t mismatches0 = g_num_mismatches; time_t start = time(NULL); for (uint32_t q = g_qmin; q <= g_qmax; q++) { - if (q <= 1) { + if (g_lw_only) { + if (q >= 2) { + run_input_lw(name, data, len, q); + } + } else if (q <= 1) { run_input_q01(name, data, len, q); } else { run_input_q2(name, data, len, q); @@ -742,6 +824,8 @@ main(int argc, char** argv) { if (sscanf(argv[argi], "-q=%u-%u", &a, &b) == 2) { g_qmin = a; g_qmax = (b < 9) ? b : 9; + } else if (strcmp(argv[argi], "-lw") == 0) { + g_lw_only = true; } else if (sscanf(argv[argi], "-shard=%u/%u", &a, &b) == 2) { shard_k = (int)a; shard_n = (b > 0) ? (int)b : 1; diff --git a/std/brotli/encode_brotli.wuffs b/std/brotli/encode_brotli.wuffs index 9c299228..b1e629cc 100644 --- a/std/brotli/encode_brotli.wuffs +++ b/std/brotli/encode_brotli.wuffs @@ -178,6 +178,16 @@ pub struct encoder? implements base.io_transformer( dict_num_matches : base.u64, fc_max_hops : base.u64, + // The large-window composite hashers' rolling hasher (the C code's + // HashRolling). See encode_hasher_composite.wuffs. hr_jump is 0 when + // the hasher is not a composite one. + hr_jump : base.u32[..= 4], + hr_fresh : base.bool, + hr_state : base.u32, + hr_factor_remove : base.u32, + hr_next_ix : base.u64, + hr_off : base.u64, + // The C code's HasherSearchResult. sr_len : base.u64, sr_distance : base.u64, @@ -418,7 +428,7 @@ pub func encoder.workbuf_len() base.range_ii_u64 { lgwin = this.sanitized_lgwin(quality: q) if q <= 1 { n = ((2 as base.u64) << lgwin) + 503 + STORAGE_SLACK - } else if (q <= 9) and ((q == 2) or (this.p_large_window == 0)) { + } else if q <= 9 { n = this.compute_layout_offset(quality: q, lgwin: lgwin, lgblock: this.computed_lgblock(quality: q, lgwin: lgwin), idx: LAYOUT_END) } @@ -801,9 +811,8 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf var copy_input_size : base.u64 this.ensure_initialized!() - if (this.quality > 9) or (this.large_window and (this.quality > 2)) { - // TODO: implement Zopfli (qualities 10 and 11) and large-window - // brotli at qualities 3 and above. + if this.quality > 9 { + // TODO: implement Zopfli (qualities 10 and 11). return "#unsupported encoder quality" } else if (this.quality >= 2) and (args.workbuf.length() < this.workbuf_end) { return base."#bad workbuf length" diff --git a/std/brotli/encode_data.wuffs b/std/brotli/encode_data.wuffs index 0e1e98f9..71fdb728 100644 --- a/std/brotli/encode_data.wuffs +++ b/std/brotli/encode_data.wuffs @@ -90,11 +90,13 @@ pri func encoder.max_metablock_size(lgwin: base.u32[..= 30], lgblock: base.u32[. // hasher_total_size returns the sum of FN(HashMemAllocInBytes)'s // alloc_size[i] for the hasher that ChooseHasher picks for the given -// quality, lgwin and size_hint. +// quality, lgwin and size_hint (including, for lgwin above 24, the +// large-window composite hashers' rolling hasher table). pri func encoder.hasher_total_size(quality: base.u32[..= 11], lgwin: base.u32[..= 30], size_hint: base.u32) base.u64 { var htype : base.u32 var bucket_bits : base.u32[..= 24] var block_bits : base.u32[..= 16] + var rolling : base.u64 if (args.quality == 4) and (args.size_hint >= 0x10_0000) { htype = 54 @@ -123,9 +125,21 @@ pri func encoder.hasher_total_size(quality: base.u32[..= 11], lgwin: base.u32[.. } block_bits = args.quality - 1 } - return (this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 0) ~mod+ + rolling = 0 + if args.lgwin > 24 { + // The large-window composite hashers H35, H55 and H65 (see + // choose_hasher): HASHER_A plus the rolling hasher's table. + if (htype == 3) or (htype == 54) { + rolling = this.hasher_rolling_alloc_size() + } else if (htype == 6) or (htype == 68) { + htype = 6 + rolling = this.hasher_rolling_alloc_size() + } + } + return ((this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 0) ~mod+ this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 1)) ~mod+ - this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 2) + this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 2)) ~mod+ + rolling } // hash_region_size returns the workbuf length reserved for the hasher's diff --git a/std/brotli/encode_hasher.wuffs b/std/brotli/encode_hasher.wuffs index 570c8597..0ae661b4 100644 --- a/std/brotli/encode_hasher.wuffs +++ b/std/brotli/encode_hasher.wuffs @@ -102,7 +102,7 @@ pri func encoder.fc_bank_bits() base.u32[..= 16] { } // Mirrors ChooseHasher in quality.h (with BROTLI_MAX_SIMD_QUALITY defined as -// 6), for qualities 2 to 9 and lgwin up to 24. +// 6), for qualities 2 to 9. pri func encoder.choose_hasher!() { var q : base.u32[..= 11] @@ -147,6 +147,25 @@ pri func encoder.choose_hasher!() { this.h_num_last_distances = 10 } } + + this.hr_jump = 0 + if this.lgwin > 24 { + // Different hashers for large window brotli: not for qualities <= 2, + // these are too fast for large window. Not for qualities >= 10: + // their hasher already works well with large window. So the changes + // are: + // H3 --> H35: for quality 3. + // H54 --> H55: for quality 4 with size hint > 1MB + // H6/H68 --> H65: for qualities 5, 6, 7, 8, 9. + // See encode_hasher_composite.wuffs: the composite hasher's + // HASHER_A stays in this.hasher_type. + if (this.hasher_type == 3) or (this.hasher_type == 54) { + this.hr_jump = 4 + } else if (this.hasher_type == 6) or (this.hasher_type == 68) { + this.hasher_type = 6 + this.hr_jump = 1 + } + } } // hasher_alloc_size mirrors FN(HashMemAllocInBytes) (alloc_size[i], for i in @@ -220,6 +239,9 @@ pri func encoder.hasher_setup!(wb: slice base.u8, position: base.u64, input_size htype: this.hasher_type, bucket_bits: this.h_bucket_bits, block_bits: this.h_block_bits, i: 0) this.h_off2 = this.h_off1 ~mod+ this.hasher_alloc_size( htype: this.hasher_type, bucket_bits: this.h_bucket_bits, block_bits: this.h_block_bits, i: 1) + // The composite hashers' HASHER_B memory follows HASHER_A's. + this.hr_off = this.h_off2 + this.hr_fresh = true this.h_is_prepared = false this.h_is_setup = true } @@ -306,6 +328,18 @@ pri func encoder.hasher_prepare!(wb: slice base.u8, one_shot: base.bool, input_s j ~mod+= 1 } } + + if this.hr_jump <> 0 { + // The composite hasher's FN(Prepare) (in hash_composite_inc.h), for + // HASHER_B. HASHER_B's FN(Initialize) is deferred to the first + // FN(Prepare). HASHER_A's FN(Initialize) does not touch the hasher + // memory, so doing it earlier (in hasher_setup) is equivalent. + if this.hr_fresh { + this.hr_fresh = false + this.hasher_rolling_initialize!(wb: args.wb) + } + this.hasher_rolling_prepare!(wb: args.wb, input_size: args.input_size, data_off: this.rb_buf_off) + } } // Mirrors FN(HashBytes) in hash_longest_match_quickly_inc.h. @@ -433,6 +467,10 @@ pri func encoder.hasher_stitch_to_previous_block!(wb: slice base.u8, num_bytes: this.hasher_store!(wb: args.wb, ix: args.position ~mod- 2) this.hasher_store!(wb: args.wb, ix: args.position ~mod- 1) } + if this.hr_jump <> 0 { + // The composite hasher's HASHER_B. + this.hasher_rolling_stitch_to_previous_block!(wb: args.wb, num_bytes: args.num_bytes, position: args.position) + } } // Mirrors PrepareDistanceCache in hash.h, via each hasher's @@ -598,6 +636,11 @@ pri func encoder.find_longest_match!(wb: slice base.u8, cur_ix: base.u64, max_le this.find_longest_match_forgetful!(wb: args.wb, cur_ix: args.cur_ix, max_length: args.max_length, max_backward: args.max_backward, dictionary_distance: args.dictionary_distance, max_distance: args.max_distance) } + if this.hr_jump <> 0 { + // The composite hasher's HASHER_B. + this.hasher_rolling_find_longest_match!(wb: args.wb, cur_ix: args.cur_ix, max_length: args.max_length, + max_backward: args.max_backward) + } } // Mirrors FN(FindLongestMatch) in hash_longest_match_quickly_inc.h. diff --git a/std/brotli/encode_hasher_composite.wuffs b/std/brotli/encode_hasher_composite.wuffs new file mode 100644 index 00000000..21fa6674 --- /dev/null +++ b/std/brotli/encode_hasher_composite.wuffs @@ -0,0 +1,208 @@ +// Copyright 2026 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +// -------- + +// This file mirrors the C code's large-window hashers: the composite hashers +// (hash_composite_inc.h) H35 (H3 + HROLLING_FAST), H55 (H54 + HROLLING_FAST) +// and H65 (H6 + HROLLING), and the rolling hashers (hash_rolling_inc.h) +// HROLLING_FAST (JUMP 4) and HROLLING (JUMP 1), both with CHUNKLEN 32, +// NUMBUCKETS (1 << 24) and MASK ((NUMBUCKETS * 64) - 1). +// +// A composite hasher is represented by its first hasher (HASHER_A, H3, H54 or +// H6) in this.hasher_type and its rolling hasher (HASHER_B) by a non-zero +// this.hr_jump (4 for HROLLING_FAST, 1 for HROLLING). The composite's FN(etc) +// functions call HASHER_A's and then HASHER_B's, so the encode_hasher.wuffs +// functions (which implement HASHER_A) call the hasher_rolling_etc functions +// below after their own work when this.hr_jump is non-zero. +// +// The composite hasher's memory is HASHER_A's alloc_size[0] and [1] followed +// by HASHER_B's alloc_size[0] (the rolling table of NUMBUCKETS u32le values), +// at the this.hr_off workbuf offset. HASHER_A's HasherCommon is a copy of the +// composite's (the C code's ha_common), whose dict_num_lookups and +// dict_num_matches start at zero, like the composite's own, so HASHER_A can +// keep using this.dict_num_lookups and this.dict_num_matches. + +// Mirrors CHUNKLEN in hash.h, for the rolling hashers. +pri const ROLLING_CHUNKLEN : base.u64 = 32 + +// Mirrors NUMBUCKETS in hash.h, for the rolling hashers. +pri const ROLLING_NUMBUCKETS : base.u32 = 0x100_0000 + +// Mirrors MASK in hash.h, for the rolling hashers. +pri const ROLLING_MASK : base.u32 = 0x3FFF_FFFF + +// Mirrors FN(kRollingHashMul32) in hash_rolling_inc.h. +pri const ROLLING_HASH_MUL_32 : base.u32 = 69069 + +// Mirrors FN(kInvalidPos) in hash_rolling_inc.h. +pri const ROLLING_INVALID_POS : base.u32 = 0xFFFF_FFFF + +// hasher_rolling_alloc_size mirrors FN(HashMemAllocInBytes) in +// hash_rolling_inc.h: alloc_size[0], the table. +pri func encoder.hasher_rolling_alloc_size() base.u64 { + return 4 * (ROLLING_NUMBUCKETS as base.u64) +} + +// Mirrors FN(Initialize) in hash_rolling_inc.h. The C code's composite +// hashers call it (via their deferred initialization) in their first +// FN(Prepare), which HasherSetup calls right after FN(Initialize). +pri func encoder.hasher_rolling_initialize!(wb: slice base.u8) { + var i : base.u64 + + this.hr_state = 0 + this.hr_next_ix = 0 + + // Compute the factor of the oldest byte to remove: factor**steps modulo + // 0xffffffff (the multiplications rely on 32-bit overflow). + this.hr_factor_remove = 1 + i = 0 + while i < ROLLING_CHUNKLEN { + this.hr_factor_remove ~mod*= ROLLING_HASH_MUL_32 + i ~mod+= this.hr_jump as base.u64 + } + + // Every table entry is ROLLING_INVALID_POS. + this.fill_bytes!(s: args.wb, i: this.hr_off, n: this.hasher_rolling_alloc_size(), v: 0xFF) +} + +// Mirrors FN(Prepare) in hash_rolling_inc.h, where data is the workbuf's +// bytes starting at offset data_off. +pri func encoder.hasher_rolling_prepare!(wb: slice base.u8, input_size: base.u64, data_off: base.u64) { + var i : base.u64 + + // Too small size, cannot use this hasher. + if args.input_size < ROLLING_CHUNKLEN { + return nothing + } + this.hr_state = 0 + i = 0 + while i < ROLLING_CHUNKLEN { + // FN(HashRollingFunctionInitial). + this.hr_state = (ROLLING_HASH_MUL_32 ~mod* this.hr_state) ~mod+ + ((this.byte_at(s: args.wb, i: args.data_off ~mod+ i) as base.u32) + 1) + i ~mod+= this.hr_jump as base.u64 + } +} + +// Mirrors FN(StitchToPreviousBlock) in hash_rolling_inc.h. +pri func encoder.hasher_rolling_stitch_to_previous_block!(wb: slice base.u8, num_bytes: base.u64, position: base.u64) { + var position : base.u64 + var position_masked : base.u64 + var available : base.u64 + var jm1 : base.u64 + var diff : base.u64 + var mask : base.u64 + + // In this case we must re-initialize the hasher from scratch from the + // current position. + position = args.position + available = args.num_bytes + jm1 = (this.hr_jump as base.u64) ~mod- 1 + if (position & jm1) <> 0 { + diff = (this.hr_jump as base.u64) ~mod- (position & jm1) + if diff > available { + available = 0 + } else { + available ~mod-= diff + } + position ~mod+= diff + } + mask = this.rb_mask as base.u64 + position_masked = position & mask + // Wrapping around ringbuffer not handled. + if available > (mask ~mod- position_masked) { + available = mask ~mod- position_masked + } + this.hasher_rolling_prepare!(wb: args.wb, input_size: available, + data_off: this.rb_buf_off ~mod+ position_masked) + this.hr_next_ix = position +} + +// Mirrors FN(FindLongestMatch) in hash_rolling_inc.h. The search result is in +// the this.sr_etc fields. +pri func encoder.hasher_rolling_find_longest_match!(wb: slice base.u8, cur_ix: base.u64, max_length: base.u64, max_backward: base.u64) { + var data : roslice base.u8 + var mask : base.u64 + var cur_ix_masked : base.u64 + var jump : base.u64 + var pos : base.u64 + var code : base.u32 + var rem : base.u8 + var add : base.u8 + var found_ix : base.u64 + var backward : base.u64 + var found_ix_masked : base.u64 + var len : base.u64 + var score : base.u64 + var t : base.u64 + + if this.rb_buf_off > args.wb.length() { + return nothing + } + data = args.wb[this.rb_buf_off ..] + mask = this.rb_mask as base.u64 + cur_ix_masked = args.cur_ix & mask + jump = this.hr_jump as base.u64 + + if (args.cur_ix & (jump ~mod- 1)) <> 0 { + return nothing + } + + // Not enough lookahead. + if args.max_length < ROLLING_CHUNKLEN { + return nothing + } + + pos = this.hr_next_ix + while pos <= args.cur_ix { + code = this.hr_state & ROLLING_MASK + + rem = this.byte_at(s: data, i: pos & mask) + add = this.byte_at(s: data, i: (pos ~mod+ ROLLING_CHUNKLEN) & mask) + found_ix = ROLLING_INVALID_POS as base.u64 + + // FN(HashRollingFunction). + this.hr_state = ((ROLLING_HASH_MUL_32 ~mod* this.hr_state) ~mod+ ((add as base.u32) + 1)) ~mod- + (this.hr_factor_remove ~mod* ((rem as base.u32) + 1)) + + if code < ROLLING_NUMBUCKETS { + t = this.hr_off ~mod+ (4 * (code as base.u64)) + found_ix = this.load32le(s: args.wb, i: t) as base.u64 + this.poke_u32!(s: args.wb, i: t, v: (pos & 0xFFFF_FFFF) as base.u32) + if (pos == args.cur_ix) and (found_ix <> (ROLLING_INVALID_POS as base.u64)) { + // The cast to 32-bit makes backward distances up to 4GB work + // even if cur_ix is above 4GB, despite using 32-bit values in + // the table. + backward = (args.cur_ix ~mod- found_ix) & 0xFFFF_FFFF + if backward <= args.max_backward { + found_ix_masked = found_ix & mask + len = this.find_match_length_with_limit(s: data, + i1: found_ix_masked, i2: cur_ix_masked, limit: args.max_length) + if (len >= 4) and (len > this.sr_len) { + score = this.backward_reference_score(copy_length: len, backward_reference_offset: backward) + if score > this.sr_score { + this.sr_len = len + this.sr_distance = backward + this.sr_score = score + this.sr_len_code_delta = 0 + } + } + } + } + } + pos ~mod+= jump + } + + this.hr_next_ix = args.cur_ix ~mod+ jump + + // NOTE: this hasher does not search in the dictionary. It is used as + // backup-hasher, the main hasher already searches in it. +} From 75b09b6700110709cd2ee78f41c4a245232b47f8 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 27 Oct 2026 02:21:31 +0000 Subject: [PATCH 2/6] script: add -big inputs with far repeats to the encoder parity test The -big option checks synthetic inputs of 3 to 70 MiB with repeats at distances of up to 40 MiB (beyond the 16 MiB non-large-window limit), with the usual grid up to 12 MiB and a few one-shot and streaming large-window configurations above that. --- script/manual-test-brotli-encoder-parity.c | 189 +++++++++++++++++---- 1 file changed, 158 insertions(+), 31 deletions(-) diff --git a/script/manual-test-brotli-encoder-parity.c b/script/manual-test-brotli-encoder-parity.c index c7347037..985034c4 100644 --- a/script/manual-test-brotli-encoder-parity.c +++ b/script/manual-test-brotli-encoder-parity.c @@ -35,6 +35,13 @@ // -q=LO-HI only check qualities LO to HI (default 0-9). // -shard=K/N only check the inputs whose index i satisfies (i % N) == K, // so that N processes can split the work. +// -lw only run the large-window grid (BROTLI_PARAM_LARGE_WINDOW +// with lgwin 10 to 30, see run_input_lw), at qualities 2 and +// above. +// -big instead of the usual inputs, check synthetic inputs of 3 to +// 12 MiB (with the usual or, with -lw, the large-window grid) +// and of 24 to 70 MiB (with a few large-window configurations +// each), with repeats at distances of up to 40 MiB. // // The test/c/std/brotli.c test program (run by "wuffs test -mimic // std/brotli") checks a subset of this grid. @@ -77,8 +84,8 @@ #define MAX_STEPS (1 << 22) #define MAX_PARAMS 16 -#define MAX_INPUT (16 << 20) -#define MAX_OUTPUT (64 << 20) +#define MAX_INPUT (80 << 20) +#define MAX_OUTPUT (96 << 20) static uint32_t g_step_ops[MAX_STEPS]; static size_t g_step_lens[MAX_STEPS]; @@ -92,6 +99,7 @@ static uint8_t* g_workbuf; // Wuffs' workbuf. static size_t g_workbuf_len; static uint64_t g_num_configs; +static size_t g_last_want_len; static uint64_t g_num_mismatches; static uint32_t g_qmin = 0; @@ -136,6 +144,77 @@ fill_random(uint8_t* p, size_t n, uint64_t seed) { } } +// fill_mixed fills p[0 .. n] with a mixture of text (from a small +// vocabulary, so that there are many matches), runs and binary noise. +static void // +fill_mixed(uint8_t* p, size_t n_max, uint64_t seed) { + static const char* words[] = {"the ", "brotli ", "wuffs ", "of ", + "and ", "data\n", "time ", "", + "encoder", ", ", "zebra ", "1234"}; + uint64_t state = seed; + size_t n = 0; + while (n < n_max) { + uint64_t r = rand64(&state); + if ((r & 0xFF) < 200) { + const char* w = words[(r >> 8) % 12]; + size_t k = strlen(w); + while (k-- && (n < n_max)) { + p[n++] = (uint8_t)(*w++); + } + } else if ((r & 0xFF) < 230) { + size_t k = (size_t)((r >> 8) % 300); + while (k-- && (n < n_max)) { + p[n++] = (uint8_t)(r >> 40); + } + } else { + size_t k = (size_t)((r >> 8) % 2000); + while (k-- && (n < n_max)) { + p[n++] = (uint8_t)(rand64(&state) >> 56); + } + } + } +} + +// fill_far_repeats fills p[0 .. n] with 1 MiB blocks: most are fresh +// pseudo-random bytes or fill_mixed data (with a per-block seed), but some +// repeat (with sparse changes) the block that is near or far +// (near_mib or far_mib MiB) before them, so that encoders with a large +// enough window find matches at those distances. When far_mib is a multiple +// of 4 and near_mib is not 2 modulo 4, the far copies' sources are fresh +// blocks that are not also copied (or copies) nearer by. +static void // +fill_far_repeats(uint8_t* p, size_t n, size_t near_mib, size_t far_mib, + uint64_t seed) { + const size_t mib = 1 << 20; + for (size_t b = 0; (b * mib) < n; b++) { + uint8_t* q = p + (b * mib); + size_t k = ((n - (b * mib)) < mib) ? (n - (b * mib)) : mib; + size_t from = SIZE_MAX; + if ((b >= near_mib) && ((b % 4) == 1)) { + from = b - near_mib; + } else if ((b >= far_mib) && ((b % 4) == 3)) { + from = b - far_mib; + } + if (from != SIZE_MAX) { + // A shifted copy: the distance is not a multiple of 1 MiB (but, for + // odd b, it is a multiple of 4, which the HROLLING_FAST hasher, used + // by H35 and H55, needs to find it). + size_t shift = (size_t)(seed % 250) + (b * 7); + if (b & 1) { + shift *= 4; + } + memcpy(q, p + (from * mib) + shift, k); + for (size_t i = 4093; i < k; i += 65521) { + q[i] ^= 0x55; + } + } else if ((b % 3) == 0) { + fill_random(q, k, seed + b); + } else { + fill_mixed(q, k, seed + b); + } + } +} + static bool // add_step(uint32_t op, size_t len) { if (g_num_steps >= MAX_STEPS) { @@ -427,6 +506,7 @@ check(const char* name, const config* c, const uint8_t* data, size_t len) { size_t want_len = 0; const char* have_err = encode_wuffs(c, &have_len); const char* want_err = encode_mimic(c, &want_len); + g_last_want_len = want_len; if (have_err || want_err) { g_num_mismatches++; describe(desc, sizeof desc, name, c); @@ -779,6 +859,40 @@ run_input(const char* name, const uint8_t* data, size_t len) { fflush(stdout); } +// run_input_far checks a few large-window configurations (one-shot and +// streaming) for a very long input. +static void // +run_input_far(const char* name, const uint8_t* data, size_t len) { + uint64_t configs0 = g_num_configs; + uint64_t mismatches0 = g_num_mismatches; + time_t start = time(NULL); + for (uint32_t q = (g_qmin > 2) ? g_qmin : 2; q <= g_qmax; q++) { + static const uint32_t lgwins[] = {24, 25, 26, 30}; + for (size_t w = 0; w < 4; w++) { + config c = make_config(q, lgwins[w]); + add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW. + c.seed = 0x40000 | (w << 8) | (q << 12); + check(name, &c, data, len); + printf(" q=%u lgwin=%u: %zu compressed bytes\n", q, lgwins[w], + g_last_want_len); + } + // Streaming, with a flush and a size hint below 1 MiB (so H35 at + // quality 3 and H58 or H5 at qualities 5 to 9, but H3 at quality 4). + config c = make_config(q, 30); + add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW. + add_param(&c, 5, 12345); // BROTLI_PARAM_SIZE_HINT. + c.chunking = 4; + c.flush = 2; + c.seed = 0x50000 | (q << 12); + check(name, &c, data, len); + } + printf("%-50s %8zu bytes: %6" PRIu64 " configs, %" PRIu64 + " mismatches (%lds)\n", + name, len, g_num_configs - configs0, g_num_mismatches - mismatches0, + (long)(time(NULL) - start)); + fflush(stdout); +} + static uint8_t* // read_whole_file(const char* path, size_t* len) { FILE* f = fopen(path, "rb"); @@ -817,6 +931,7 @@ main(int argc, char** argv) { int shard_k = 0; int shard_n = 1; + bool big = false; int argi = 1; for (; argi < argc; argi++) { unsigned a = 0; @@ -826,6 +941,8 @@ main(int argc, char** argv) { g_qmax = (b < 9) ? b : 9; } else if (strcmp(argv[argi], "-lw") == 0) { g_lw_only = true; + } else if (strcmp(argv[argi], "-big") == 0) { + big = true; } else if (sscanf(argv[argi], "-shard=%u/%u", &a, &b) == 2) { shard_k = (int)a; shard_n = (b > 0) ? (int)b : 1; @@ -870,6 +987,42 @@ main(int argc, char** argv) { } int input_index = 0; + if (big) { + // Synthetic inputs of several MiB (with the usual grid) and of 24 to 70 + // MiB (with run_input_far's configurations), with repeats at distances + // above 16 MiB (1 << 24, the largest non-large-window window). + num_names = 0; + uint8_t* p = malloc(70 << 20); + if (!p) { + return 1; + } + static const struct { + size_t mib; + size_t near_mib; + size_t far_mib; + bool far; + } bigs[] = { + {3, 2, 2, false}, {6, 2, 5, false}, {12, 3, 10, false}, + {24, 3, 20, true}, {40, 3, 32, true}, {70, 5, 40, true}, + }; + for (size_t j = 0; j < sizeof bigs / sizeof bigs[0]; j++) { + if (((input_index++) % shard_n) != shard_k) { + continue; + } + size_t n = (bigs[j].mib << 20) - 12345; + fill_far_repeats(p, n, bigs[j].near_mib, bigs[j].far_mib, 0x777 + j); + char name[96]; + snprintf(name, sizeof name, + "synthetic: %zu MiB with repeats at %zu and %zu MiB", + bigs[j].mib, bigs[j].near_mib, bigs[j].far_mib); + if (bigs[j].far) { + run_input_far(name, p, n); + } else { + run_input(name, p, n); + } + } + free(p); + } for (int i = 0; i < num_names; i++) { if (((input_index++) % shard_n) != shard_k) { continue; @@ -884,7 +1037,7 @@ main(int argc, char** argv) { free(data); } - if (argi >= argc) { + if ((argi >= argc) && !big) { // Synthetic inputs. uint8_t* p = malloc(1 << 20); if (!p) { @@ -907,34 +1060,8 @@ main(int argc, char** argv) { run_input("synthetic: a repeated 100-byte pattern", p, 64 << 10); } if (((input_index++) % shard_n) == shard_k) { - // A mixture of text (from a small vocabulary, so that there are many - // matches), runs and binary noise. - static const char* words[] = {"the ", "brotli ", "wuffs ", "of ", - "and ", "data\n", "time ", "", - "encoder", ", ", "zebra ", "1234"}; - uint64_t state = 0xABCD; - size_t n = 0; - while (n < (600 << 10)) { - uint64_t r = rand64(&state); - if ((r & 0xFF) < 200) { - const char* w = words[(r >> 8) % 12]; - size_t k = strlen(w); - while (k-- && (n < (600 << 10))) { - p[n++] = (uint8_t)(*w++); - } - } else if ((r & 0xFF) < 230) { - size_t k = (size_t)((r >> 8) % 300); - while (k-- && (n < (600 << 10))) { - p[n++] = (uint8_t)(r >> 40); - } - } else { - size_t k = (size_t)((r >> 8) % 2000); - while (k-- && (n < (600 << 10))) { - p[n++] = (uint8_t)(rand64(&state) >> 56); - } - } - } - run_input("synthetic: 600 KiB of mixed text and binary", p, n); + fill_mixed(p, 600 << 10, 0xABCD); + run_input("synthetic: 600 KiB of mixed text and binary", p, 600 << 10); } for (size_t n = 0; n <= 40; n++) { if (((input_index++) % shard_n) != shard_k) { From dbb42802be74d74b9c03c3cb5661b0b208ffc917 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 27 Oct 2026 02:28:35 +0000 Subject: [PATCH 3/6] test/c/std/brotli: add large-window encoder tests Check large-window encoding at qualities 3 to 9 (lgwin 18 to 30, with the H35, H55 and H65 composite hashers) against libbrotlienc, with round trips through Wuffs' decoder (with the large-window quirk) and libbrotlidec, over some small files and a 46 MiB input with repeats at distances of about 20 and 40 MiB. Also check the large-window workbuf_len and allow encoder workbufs of up to 3 GiB in the test driver. --- test/c/std/brotli.c | 294 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 284 insertions(+), 10 deletions(-) diff --git a/test/c/std/brotli.c b/test/c/std/brotli.c index 439e09f1..0e07884d 100644 --- a/test/c/std/brotli.c +++ b/test/c/std/brotli.c @@ -894,7 +894,9 @@ wuffs_brotli_encode_plan(wuffs_base__io_buffer* dst, wuffs_base__range_ii_u64 workbuf_len = wuffs_brotli__encoder__workbuf_len(enc); wuffs_base__slice_u8 workbuf = g_work_slice_u8; - if (workbuf_len.max_incl > (1ull << 31)) { + // Large-window brotli at lgwin 30 needs more than 2 GiB. + if ((workbuf_len.max_incl > (3ull << 30)) || + (workbuf_len.max_incl > SIZE_MAX)) { ret = "workbuf_len is too large"; goto cleanup; } else if (workbuf_len.max_incl > g_work_slice_u8.len) { @@ -1336,10 +1338,43 @@ test_wuffs_brotli_encode_parameters() { src = make_io_buffer_from_string("", 0); dst.meta.wi = 0; - // Qualities 10 and 11 (and 12, which sanitizes to 11) are unsupported, and - // so is large-window brotli at qualities 3 and above. - for (uint32_t q = 3; q <= 12; q++) { - bool large_window = q < 10; + // With the large-window quirk, lgwin can be up to 30 (instead of 24) at + // qualities 3 and above. Like the C code, quality 2 and below ignore the + // quirk. Above lgwin 24, the ring buffer is (2 << lgwin) bytes and, at + // quality 3, the H35 hasher has a 64 MiB rolling hash table on top of + // H3's 256 KiB. + for (uint32_t q = 2; q <= 3; q++) { + uint64_t wlens[3] = {0}; + for (int k = 0; k < 3; k++) { + free(enc); + enc = wuffs_brotli__encoder__alloc(); + if (!enc) { + RETURN_FAIL("alloc failed"); + } + wuffs_brotli__encoder__set_quirk( + enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, q); + wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_LGWIN, + (k == 0) ? 24 : 30); + if (k > 0) { + wuffs_brotli__encoder__set_quirk( + enc, WUFFS_BROTLI__QUIRK_LARGE_WINDOW, (k == 2) ? 1 : 0); + } + wlens[k] = wuffs_brotli__encoder__workbuf_len(enc).max_incl; + } + BROTLI_ENC_CHECK(wlens[0] == wlens[1], + "lgwin 30 without large window is lgwin 24"); + if (q == 2) { + BROTLI_ENC_CHECK(wlens[0] == wlens[2], + "large window is ignored at quality 2"); + } else { + BROTLI_ENC_CHECK( + wlens[2] == (wlens[0] + (2ull << 30) - (2ull << 24) + (64 << 20)), + "workbuf_len at q3w30 with large window"); + } + } + + // Qualities 10 and 11 (and 12, which sanitizes to 11) are unsupported. + for (uint32_t q = 10; q <= 12; q++) { free(enc); enc = wuffs_brotli__encoder__alloc(); if (!enc) { @@ -1347,17 +1382,13 @@ test_wuffs_brotli_encode_parameters() { } wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, q); - if (large_window) { - wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_LARGE_WINDOW, - 1); - } BROTLI_ENC_CHECK(wuffs_brotli__encoder__workbuf_len(enc).max_incl == 0, "workbuf_len for an unsupported quality"); status = wuffs_brotli__encoder__transform_io(enc, &dst, &src, g_work_slice_u8); BROTLI_ENC_CHECK( status.repr == wuffs_brotli__error__unsupported_encoder_quality, - large_window ? "large window at quality 3..9" : "quality 10..11"); + "quality 10..11"); } // Requests. @@ -3133,6 +3164,245 @@ test_mimic_brotli_decode_large_window_far() { #endif // WUFFS_MIMIC +// ---------------- Brotli Large Window Encoder Tests + +// do_test_brotli_encode_large_window encodes data[:data_len] with the plan's +// config c (which should set the large-window quirk) and checks that +// decoding the result (with Wuffs' std/brotli decoder with the large-window +// quirk set, and, if mimic is true, with libbrotlidec) gives back the +// original data. If mimic is true, it also checks that libbrotlienc's output +// is the same. If max_compressed_len is non-zero, it checks that the +// compressed length is at most that. The decoded data goes in g_want. +const char* // +do_test_brotli_encode_large_window(const char* name, + const brotli_enc_config* c, + const uint8_t* data, + size_t data_len, + bool mimic, + size_t max_compressed_len) { + wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ + .data = g_src_slice_u8, + }); + CHECK_STRING(make_brotli_enc_plan(&src, c, data, data_len)); + wuffs_base__io_buffer have = ((wuffs_base__io_buffer){ + .data = g_have_slice_u8, + }); + wuffs_base__io_buffer src0 = src; + const char* status = wuffs_brotli_encode_plan( + &have, &src0, c->param_keys, c->param_values, c->num_params, + g_brotli_enc_step_ops, g_brotli_enc_step_lens, g_brotli_enc_num_steps, + c->dst_chunk); + if (status) { + RETURN_FAIL("%s: wuffs encode: %s", name, status); + } else if (max_compressed_len && (have.meta.wi > max_compressed_len)) { + RETURN_FAIL("%s: compressed length: have %zu, want at most %zu", name, + have.meta.wi, max_compressed_len); + } + have.meta.closed = true; + + for (int decoder = 0; decoder < (mimic ? 3 : 1); decoder++) { + wuffs_base__io_buffer want = ((wuffs_base__io_buffer){ + .data = g_want_slice_u8, + }); + wuffs_base__io_buffer have1 = have; + if (decoder == 0) { + status = wuffs_brotli_decode_with_options( + &want, &have1, UINT64_MAX, UINT64_MAX, true, BROTLI_DICT_SET_NONE); +#ifdef WUFFS_MIMIC + } else if (decoder == 1) { + status = mimic_brotli_decode_large_window( + &want, &have1, UINT64_MAX, UINT64_MAX, true, BROTLI_DICT_SET_NONE); + } else { + wuffs_base__io_buffer src1 = src; + status = mimic_brotli_encode( + &want, &src1, c->param_keys, c->param_values, c->num_params, + g_brotli_enc_step_ops, g_brotli_enc_step_lens, + g_brotli_enc_num_steps, c->dst_chunk); + if (status) { + RETURN_FAIL("%s: mimic encode: %s", name, status); + } + status = check_io_buffers_equal("", &have, &want); + if (status) { + RETURN_FAIL("%s: %s", name, status); + } + continue; +#endif // WUFFS_MIMIC + } + const char* which = (decoder == 0) ? "wuffs" : "mimic"; + if (status) { + RETURN_FAIL("%s: %s decode: %s", name, which, status); + } else if (have1.meta.ri != have1.meta.wi) { + RETURN_FAIL("%s: %s decode: trailing data", name, which); + } else if (want.meta.wi != data_len) { + RETURN_FAIL("%s: %s decoded length: have %zu, want %zu", name, which, + want.meta.wi, data_len); + } else if (memcmp(want.data.ptr, data, data_len)) { + RETURN_FAIL("%s: %s decoded bytes differ", name, which); + } + } + return NULL; +} + +// do_test_brotli_encode_large_window_files runs a grid of large-window +// configurations (qualities 3 to 9, lgwin 18 to 30) over a few small files. +const char* // +do_test_brotli_encode_large_window_files(bool mimic) { + static const char* filenames[] = { + "test/data/romeo.txt", + "test/data/brotli/alice29.txt", + "test/data/brotli/random_org_10k.bin", + "test/data/brotli/quickfox_repeated", + }; + static const uint32_t lgwins[] = {18, 25, 26, 28, 30}; + wuffs_base__io_buffer data = ((wuffs_base__io_buffer){ + .data = g_work_slice_u8, + }); + for (size_t f = 0; f < WUFFS_TESTLIB_ARRAY_SIZE(filenames); f++) { + // g_work is free to use for the input: each config's workbuf (of more + // than 64 MiB) is malloc'ed. + data.meta.wi = 0; + data.meta.ri = 0; + data.meta.closed = false; + CHECK_STRING(read_file(&data, filenames[f])); + uint8_t* copy = malloc(data.meta.wi ? data.meta.wi : 1); + if (!copy) { + return "malloc failed"; + } + memcpy(copy, data.data.ptr, data.meta.wi); + const char* ret = NULL; + for (uint32_t q = 3; (q <= 9) && !ret; q++) { + for (size_t w = 0; (w < 5) && !ret; w++) { + for (int k = 0; (k < 2) && !ret; k++) { + if ((k == 1) && (lgwins[w] != 26)) { + continue; + } + brotli_enc_config c = make_brotli_enc_config(q, lgwins[w]); + brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_LARGE_WINDOW, 1); + if (k == 1) { + // A big size hint selects H55 (at quality 4) and H65 (at + // qualities 5 to 9). + brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_SIZE_HINT, + 1 << 20); + } + c.chunking = (w & 1) ? BROTLI_ENC_CHUNK_SINGLE + : BROTLI_ENC_CHUNK_RANDOM; + c.flush = (w & 1) ? BROTLI_ENC_FLUSH_NONE : BROTLI_ENC_FLUSH_MIDDLE; + c.seed = (q << 8) | w; + char s[256]; + snprintf(s, sizeof s, "%s: q=%u lgwin=%u size_hint=%s", + filenames[f], q, lgwins[w], k ? "1<<20" : "0"); + ret = do_test_brotli_encode_large_window(s, &c, copy, data.meta.wi, + mimic, 0); + } + } + } + free(copy); + if (ret) { + return ret; + } + } + return NULL; +} + +// make_brotli_enc_far_data returns a malloc'ed buffer of pseudo-random bytes +// with two 2 MiB repeats at distances of (20 MiB + 64) and (40 MiB - 4096). +// The repeats' sources are not repeated elsewhere, so only an encoder with a +// large enough window (lgwin 25 or more for the first, 26 or more for the +// second) can compress it. +uint8_t* // +make_brotli_enc_far_data(size_t* len) { + const size_t mib = 1 << 20; + const size_t n = (46 * mib) + 12345; + uint8_t* p = malloc(n); + if (!p) { + return NULL; + } + brotli_enc_fill_random(p, n, 0xFA12); + memcpy(p + (21 * mib) + 64, p + (1 * mib), 2 * mib); + memcpy(p + (44 * mib) - 4096, p + (4 * mib), 2 * mib); + *len = n; + return p; +} + +const char* // +do_test_brotli_encode_large_window_far(bool mimic) { + size_t len = 0; + uint8_t* data = make_brotli_enc_far_data(&len); + if (!data) { + return "malloc failed"; + } + static const struct { + uint32_t quality; + uint32_t lgwin; + uint32_t size_hint; + uint32_t chunking; + size_t num_repeats; + } configs[] = { + // H35. + {3, 25, 0, BROTLI_ENC_CHUNK_SINGLE, 1}, + {3, 30, 0, BROTLI_ENC_CHUNK_RANDOM, 2}, + // H55. + {4, 26, 1 << 30, BROTLI_ENC_CHUNK_SINGLE, 2}, + // H65. + {5, 30, 1 << 30, BROTLI_ENC_CHUNK_RANDOM, 2}, + {7, 28, 0, BROTLI_ENC_CHUNK_SINGLE, 2}, + {9, 26, 0, BROTLI_ENC_CHUNK_SINGLE, 2}, + }; + const char* ret = NULL; + for (size_t i = 0; (i < WUFFS_TESTLIB_ARRAY_SIZE(configs)) && !ret; i++) { + brotli_enc_config c = + make_brotli_enc_config(configs[i].quality, configs[i].lgwin); + brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_LARGE_WINDOW, 1); + if (configs[i].size_hint) { + brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_SIZE_HINT, + configs[i].size_hint); + } + c.chunking = configs[i].chunking; + c.flush = (configs[i].chunking == BROTLI_ENC_CHUNK_RANDOM) + ? BROTLI_ENC_FLUSH_MIDDLE + : BROTLI_ENC_FLUSH_NONE; + c.seed = i; + char s[256]; + snprintf(s, sizeof s, "far data: q=%u lgwin=%u", configs[i].quality, + configs[i].lgwin); + // Pseudo-random data is incompressible, except for the repeats (of + // 2 MiB each), which must be found (mostly) to meet this limit. + size_t max_compressed_len = len - (configs[i].num_repeats * (3 << 19)); + ret = do_test_brotli_encode_large_window(s, &c, data, len, mimic, + max_compressed_len); + } + free(data); + return ret; +} + +const char* // +test_wuffs_brotli_encode_large_window_files() { + CHECK_FOCUS(__func__); + return do_test_brotli_encode_large_window_files(false); +} + +const char* // +test_wuffs_brotli_encode_large_window_far() { + CHECK_FOCUS(__func__); + return do_test_brotli_encode_large_window_far(false); +} + +#ifdef WUFFS_MIMIC + +const char* // +test_mimic_brotli_encode_large_window_files() { + CHECK_FOCUS(__func__); + return do_test_brotli_encode_large_window_files(true); +} + +const char* // +test_mimic_brotli_encode_large_window_far() { + CHECK_FOCUS(__func__); + return do_test_brotli_encode_large_window_far(true); +} + +#endif // WUFFS_MIMIC + // ---------------- Brotli Benches const char* // @@ -3204,6 +3474,8 @@ proc g_tests[] = { // Encoder tests. test_wuffs_brotli_encode_interface, + test_wuffs_brotli_encode_large_window_far, + test_wuffs_brotli_encode_large_window_files, test_wuffs_brotli_encode_metadata_round_trip, test_wuffs_brotli_encode_parameters, test_wuffs_brotli_encode_round_trip_corpus, @@ -3212,6 +3484,8 @@ proc g_tests[] = { #ifdef WUFFS_MIMIC + test_mimic_brotli_encode_large_window_far, + test_mimic_brotli_encode_large_window_files, test_mimic_brotli_encode_parity_corpus, test_mimic_brotli_encode_parity_grid, test_mimic_brotli_encode_parity_metadata, From 0b9cfddf3cc64cc2d13ab2ed3fa14122d098d986 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 27 Oct 2026 02:36:23 +0000 Subject: [PATCH 4/6] std/brotli: let a shorter encoder workbuf work for short inputs Move the ring buffer to the end of the workbuf layout (at qualities 2 to 9). A workbuf shorter than workbuf_len now works if it holds the input so far (plus 16 bytes of slack) in the ring buffer region and the input stays short enough for the ring buffer never to wrap. This matters most for large-window brotli, where workbuf_len at lgwin 30 is more than 2 GiB, mostly for a ring buffer that short inputs never touch. The parity harness now also checks its large-window configurations with the shortest such workbuf (and that one byte less fails). --- release/c/wuffs-unsupported-snapshot.c | 100 +++++++++++-------- script/manual-test-brotli-encoder-parity.c | 108 ++++++++++++++++++++- std/brotli/encode_brotli.wuffs | 25 ++++- std/brotli/encode_data.wuffs | 35 ++++--- test/c/std/brotli.c | 55 ++++++++++- 5 files changed, 262 insertions(+), 61 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index c78ddcfb..99ff4617 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -10010,6 +10010,7 @@ struct wuffs_brotli__encoder__struct { uint32_t f_rb_total_size; uint32_t f_rb_cur_size; uint32_t f_rb_pos; + bool f_rb_short_workbuf; uint32_t f_dist_alphabet_size_max; uint32_t f_dist_alphabet_size_limit; uint64_t f_dist_max_distance; @@ -68498,30 +68499,32 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = { #define WUFFS_BROTLI__LAYOUT_STORAGE 0u -#define WUFFS_BROTLI__LAYOUT_RB 1u +#define WUFFS_BROTLI__LAYOUT_HASH 1u -#define WUFFS_BROTLI__LAYOUT_HASH 2u +#define WUFFS_BROTLI__LAYOUT_CMDS 2u -#define WUFFS_BROTLI__LAYOUT_CMDS 3u +#define WUFFS_BROTLI__LAYOUT_TYPES_0 3u -#define WUFFS_BROTLI__LAYOUT_TYPES_0 4u +#define WUFFS_BROTLI__LAYOUT_LENGTHS_0 6u -#define WUFFS_BROTLI__LAYOUT_LENGTHS_0 7u +#define WUFFS_BROTLI__LAYOUT_HISTO_0 9u -#define WUFFS_BROTLI__LAYOUT_HISTO_0 10u +#define WUFFS_BROTLI__LAYOUT_COMB 12u -#define WUFFS_BROTLI__LAYOUT_COMB 13u +#define WUFFS_BROTLI__LAYOUT_CMAP 13u -#define WUFFS_BROTLI__LAYOUT_CMAP 14u +#define WUFFS_BROTLI__LAYOUT_RLE 14u -#define WUFFS_BROTLI__LAYOUT_RLE 15u +#define WUFFS_BROTLI__LAYOUT_BE_DEPTH_0 15u -#define WUFFS_BROTLI__LAYOUT_BE_DEPTH_0 16u +#define WUFFS_BROTLI__LAYOUT_BE_BITS_0 18u -#define WUFFS_BROTLI__LAYOUT_BE_BITS_0 19u +#define WUFFS_BROTLI__LAYOUT_RB 21u #define WUFFS_BROTLI__LAYOUT_END 22u +#define WUFFS_BROTLI__RB_SHORT_SLACK 16u + #define WUFFS_BROTLI__MAX_NUM_DELAYED_SYMBOLS 12287u #define WUFFS_BROTLI__F64_0_99 4607092346807469998u @@ -79349,6 +79352,7 @@ wuffs_brotli__encoder__transform_io( uint64_t v_remaining = 0; bool v_injected = false; uint64_t v_copy_input_size = 0; + uint64_t v_total = 0; uint8_t* iop_a_dst = NULL; uint8_t* io0_a_dst WUFFS_BASE__POTENTIALLY_UNUSED = NULL; @@ -79392,8 +79396,11 @@ wuffs_brotli__encoder__transform_io( status = wuffs_base__make_status(wuffs_brotli__error__unsupported_encoder_quality); goto exit; } else if ((self->private_impl.f_quality >= 2u) && (((uint64_t)(a_workbuf.len)) < self->private_impl.f_workbuf_end)) { - status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length); - goto exit; + self->private_impl.f_rb_short_workbuf = true; + if (((uint64_t)(a_workbuf.len)) < ((uint64_t)(self->private_impl.f_rb_buf_off + 16u))) { + status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length); + goto exit; + } } while (true) { if (self->private_impl.f_metadata_requested) { @@ -79491,6 +79498,13 @@ wuffs_brotli__encoder__transform_io( } if ((v_remaining != 0u) && (((uint64_t)(io2_a_src - iop_a_src)) > 0u)) { v_copy_input_size = wuffs_base__u64__min(v_remaining, ((uint64_t)(io2_a_src - iop_a_src))); + if (self->private_impl.f_rb_short_workbuf) { + v_total = ((uint64_t)(((uint64_t)(self->private_impl.f_input_pos + v_copy_input_size)) + 16u)); + if ((v_total > ((uint64_t)(self->private_impl.f_rb_size))) || (((uint64_t)(self->private_impl.f_rb_buf_off + v_total)) > ((uint64_t)(a_workbuf.len)))) { + status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length); + goto exit; + } + } v_mark = ((uint64_t)(iop_a_src - io0_a_src)); self->private_data.s_transform_io.scratch = v_copy_input_size; WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); @@ -80502,49 +80516,49 @@ wuffs_brotli__encoder__compute_layout_offset( v_size = 0u; if (v_i == 0u) { v_size = ((uint64_t)(((uint64_t)(((uint64_t)(2u * v_max_len)) + 503u)) + 16u)); - } else if (v_i == 1u) { + } else if (v_i == 21u) { v_size = ((uint64_t)(((uint64_t)((2u + (((uint64_t)(1u)) << wuffs_brotli__encoder__rb_bits(self, a_lgwin, a_lgblock))) + (((uint64_t)(1u)) << a_lgblock))) + 7u)); - } else if (v_i == 2u) { + } else if (v_i == 1u) { v_size = wuffs_brotli__encoder__hash_region_size(self, a_quality, a_lgwin); - } else if (v_i == 3u) { + } else if (v_i == 2u) { v_size = ((uint64_t)(16u * v_cmds_cap)); } else if (a_quality >= 4u) { - if (v_i == 4u) { + if (v_i == 3u) { v_size = ((v_max_len / 512u) + 1u); - } else if (v_i == 5u) { + } else if (v_i == 4u) { v_size = ((uint64_t)((v_cmds_cap / 1024u) + 1u)); - } else if (v_i == 6u) { + } else if (v_i == 5u) { v_size = ((uint64_t)((v_cmds_cap / 512u) + 1u)); - } else if (v_i == 7u) { + } else if (v_i == 6u) { v_size = (4u * ((v_max_len / 512u) + 1u)); - } else if (v_i == 8u) { + } else if (v_i == 7u) { v_size = ((uint64_t)(4u * ((uint64_t)((v_cmds_cap / 1024u) + 1u)))); - } else if (v_i == 9u) { + } else if (v_i == 8u) { v_size = ((uint64_t)(4u * ((uint64_t)((v_cmds_cap / 512u) + 1u)))); - } else if ((10u <= v_i) && (v_i < 13u)) { - if (v_i == 10u) { + } else if ((9u <= v_i) && (v_i < 12u)) { + if (v_i == 9u) { v_size = 266240u; - } else if (v_i == 11u) { + } else if (v_i == 10u) { v_size = 723712u; } else { v_size = 559232u; } - } else if (v_i == 13u) { + } else if (v_i == 12u) { v_size = 26624u; - } else if (v_i == 14u) { + } else if (v_i == 13u) { v_size = 16384u; - } else if (v_i == 15u) { + } else if (v_i == 14u) { v_size = 65536u; - } else if ((16u <= v_i) && (v_i < 22u)) { + } else if ((15u <= v_i) && (v_i < 21u)) { v_num_histo = 256u; v_hlen = 256u; - if ((v_i == 17u) || (v_i == 20u)) { + if ((v_i == 16u) || (v_i == 19u)) { v_hlen = 704u; - } else if ((v_i == 18u) || (v_i == 21u)) { + } else if ((v_i == 17u) || (v_i == 20u)) { v_hlen = 544u; } v_size = ((uint64_t)(v_num_histo * v_hlen)); - if (v_i >= 19u) { + if (v_i >= 18u) { v_size = ((uint64_t)(2u * v_size)); } } @@ -80573,18 +80587,18 @@ wuffs_brotli__encoder__compute_layout( v_q, v_lw, v_lb, - 1u); + 21u); self->private_impl.f_rb_buf_off = ((uint64_t)(self->private_impl.f_rb_off + 2u)); self->private_impl.f_h_off0 = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, v_lb, - 2u); + 1u); self->private_impl.f_cmds_off = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, v_lb, - 3u); + 2u); self->private_impl.f_cmds_cap = wuffs_brotli__encoder__commands_capacity(self, v_q, v_lw, v_lb); v_c = 0u; while (v_c < 3u) { @@ -80592,44 +80606,44 @@ wuffs_brotli__encoder__compute_layout( v_q, v_lw, v_lb, - (4u + v_c)); + (3u + v_c)); self->private_data.f_gbs_lengths_off[v_c] = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, v_lb, - (7u + v_c)); + (6u + v_c)); self->private_data.f_gbs_histo_off_base[v_c] = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, v_lb, - (10u + v_c)); + (9u + v_c)); self->private_data.f_be_depth_off[v_c] = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, v_lb, - (16u + v_c)); + (15u + v_c)); self->private_data.f_be_bits_off[v_c] = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, v_lb, - (19u + v_c)); + (18u + v_c)); v_c += 1u; } self->private_impl.f_mb_comb_off = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, v_lb, - 13u); + 12u); self->private_impl.f_mb_cmap_off = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, v_lb, - 14u); + 13u); self->private_impl.f_mb_rle_off = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, v_lb, - 15u); + 14u); self->private_impl.f_workbuf_end = wuffs_brotli__encoder__compute_layout_offset(self, v_q, v_lw, diff --git a/script/manual-test-brotli-encoder-parity.c b/script/manual-test-brotli-encoder-parity.c index 985034c4..b7416b50 100644 --- a/script/manual-test-brotli-encoder-parity.c +++ b/script/manual-test-brotli-encoder-parity.c @@ -99,6 +99,8 @@ static uint8_t* g_workbuf; // Wuffs' workbuf. static size_t g_workbuf_len; static uint64_t g_num_configs; +static uint64_t g_num_short_checks; +static size_t g_short_workbuf_len; // 0 means use workbuf_len. static size_t g_last_want_len; static uint64_t g_num_mismatches; @@ -116,6 +118,9 @@ typedef struct { size_t metadata_len; size_t dst_chunk; // 0 means unlimited. uint64_t seed; + // try_short is whether to also check (at qualities 2 to 9) Wuffs' output + // with the shortest workbuf that should work: see short_workbuf_len. + bool try_short; } config; static void // @@ -371,6 +376,9 @@ encode_wuffs(const config* c, size_t* have_len) { goto cleanup; } } + if ((g_short_workbuf_len > 0) && (g_short_workbuf_len < wlen)) { + wlen = g_short_workbuf_len; + } wuffs_base__slice_u8 workbuf = wuffs_base__make_slice_u8(g_workbuf, (size_t)wlen); @@ -492,6 +500,63 @@ describe(char* buf, size_t n, const char* name, const config* c) { } } +// short_workbuf_len returns the shortest workbuf (at qualities 2 to 9) that +// should work for the config c and an input of len bytes, given that +// workbuf_len is wlen, or 0 if it is not shorter than wlen. The ring buffer +// is the workbuf's last region, of (2 + (1 << rb_bits) + (1 << lgblock) + 7) +// bytes, and a shorter workbuf works if it holds (2 + len + 16) bytes of it +// and (len + 16) is at most (1 << rb_bits). This mirrors SanitizeParams, +// ComputeLgBlock and ComputeRbBits in quality.h. +static size_t // +short_workbuf_len(const config* c, size_t len, uint64_t wlen) { + int32_t quality = 11; + int32_t lgwin = 22; + int32_t lgblock = 0; + bool large_window = false; + for (size_t j = 0; j < c->num_params; j++) { + int32_t v = (int32_t)(c->values[j]); + switch (c->keys[j]) { + case 1: + quality = v; + break; + case 2: + lgwin = v; + break; + case 3: + lgblock = v; + break; + case 6: + large_window = v != 0; + break; + } + } + quality = (quality < 0) ? 0 : (quality > 11) ? 11 : quality; + if ((quality < 2) || (quality > 9)) { + return 0; + } + large_window = large_window && (quality > 2); + int32_t max_lgwin = large_window ? 30 : 24; + lgwin = (lgwin < 10) ? 10 : (lgwin > max_lgwin) ? max_lgwin : lgwin; + if (quality < 4) { + lgblock = 14; + } else if (lgblock == 0) { + lgblock = 16; + if ((quality >= 9) && (lgwin > lgblock)) { + lgblock = (lgwin < 18) ? lgwin : 18; + } + } else { + lgblock = (lgblock < 16) ? 16 : (lgblock > 24) ? 24 : lgblock; + } + int32_t rb_bits = 1 + ((lgwin > lgblock) ? lgwin : lgblock); + uint64_t rb_size = (uint64_t)1 << rb_bits; + uint64_t rb_region = 2 + rb_size + ((uint64_t)1 << lgblock) + 7; + if (((len + 16) > rb_size) || (rb_region > wlen)) { + return 0; + } + uint64_t n = wlen - rb_region + 2 + len + 16; + return (n < wlen) ? (size_t)n : 0; +} + static void // check(const char* name, const config* c, const uint8_t* data, size_t len) { char desc[512]; @@ -507,6 +572,41 @@ check(const char* name, const config* c, const uint8_t* data, size_t len) { const char* have_err = encode_wuffs(c, &have_len); const char* want_err = encode_mimic(c, &want_len); g_last_want_len = want_len; + if (c->try_short && !have_err && !want_err) { + // Also check the shortest workbuf that should work, and that one byte + // less fails. + wuffs_brotli__encoder* enc = wuffs_brotli__encoder__alloc(); + uint64_t wlen = 0; + if (enc) { + for (size_t j = 0; j < c->num_params; j++) { + wuffs_brotli__encoder__set_quirk(enc, brotli_enc_quirk_key(c->keys[j]), + c->values[j]); + } + wlen = wuffs_brotli__encoder__workbuf_len(enc).max_incl; + free(enc); + } + size_t short_len = short_workbuf_len(c, len, wlen); + if (short_len > 0) { + g_num_short_checks++; + g_short_workbuf_len = short_len - 1; + size_t too_short_len = 0; + const char* too_short_err = encode_wuffs(c, &too_short_len); + g_short_workbuf_len = short_len; + have_err = encode_wuffs(c, &have_len); + g_short_workbuf_len = 0; + if (!too_short_err || + (too_short_err != wuffs_base__error__bad_workbuf_length)) { + g_num_mismatches++; + describe(desc, sizeof desc, name, c); + printf("MISMATCH %s: a workbuf of %zu bytes did not fail: %s\n", + desc, short_len - 1, too_short_err ? too_short_err : "ok"); + fflush(stdout); + return; + } else if (have_err) { + have_err = "short workbuf failed"; + } + } + } if (have_err || want_err) { g_num_mismatches++; describe(desc, sizeof desc, name, c); @@ -651,6 +751,7 @@ run_input_lw(const char* name, const uint8_t* data, size_t len, uint32_t q) { c.chunking = ch; c.flush = fl; c.seed = 0x10000 | (w << 8) | (ch << 4) | fl | (q << 12); + c.try_short = true; check(name, &c, data, len); } } @@ -693,6 +794,7 @@ run_input_lw(const char* name, const uint8_t* data, size_t len, uint32_t q) { c.chunking = 4; c.flush = 2; c.seed = 0x20000 | i | (w << 8) | (q << 12); + c.try_short = true; check(name, &c, data, len); } } @@ -872,6 +974,7 @@ run_input_far(const char* name, const uint8_t* data, size_t len) { config c = make_config(q, lgwins[w]); add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW. c.seed = 0x40000 | (w << 8) | (q << 12); + c.try_short = true; check(name, &c, data, len); printf(" q=%u lgwin=%u: %zu compressed bytes\n", q, lgwins[w], g_last_want_len); @@ -1075,7 +1178,8 @@ main(int argc, char** argv) { free(p); } - printf("TOTAL: %" PRIu64 " configs, %" PRIu64 " mismatches\n", g_num_configs, - g_num_mismatches); + printf("TOTAL: %" PRIu64 " configs (%" PRIu64 + " also with a short workbuf), %" PRIu64 " mismatches\n", + g_num_configs, g_num_short_checks, g_num_mismatches); return g_num_mismatches ? 1 : 0; } diff --git a/std/brotli/encode_brotli.wuffs b/std/brotli/encode_brotli.wuffs index b1e629cc..48edc6bb 100644 --- a/std/brotli/encode_brotli.wuffs +++ b/std/brotli/encode_brotli.wuffs @@ -157,6 +157,12 @@ pub struct encoder? implements base.io_transformer( rb_cur_size : base.u32, rb_pos : base.u32, + // rb_short_workbuf is whether any transform_io call (at qualities 2 + // to 9) had a workbuf shorter than workbuf_len. If so, the ring + // buffer's tail may be incomplete, so the input must stay short + // enough for the ring buffer never to wrap. + rb_short_workbuf : base.bool, + // The C code's BrotliDistanceParams fields (other than // distance_postfix_bits and num_direct_distance_codes). dist_alphabet_size_max : base.u32, @@ -809,13 +815,20 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf var injected : base.bool var copy_input_size : base.u64 + var total : base.u64 this.ensure_initialized!() if this.quality > 9 { // TODO: implement Zopfli (qualities 10 and 11). return "#unsupported encoder quality" } else if (this.quality >= 2) and (args.workbuf.length() < this.workbuf_end) { - return base."#bad workbuf length" + // A workbuf shorter than workbuf_len works if it is long enough for + // the input so far (see the rb_short_workbuf check below), as long + // as the ring buffer never wraps. + this.rb_short_workbuf = true + if args.workbuf.length() < (this.rb_buf_off ~mod+ RB_SHORT_SLACK) { + return base."#bad workbuf length" + } } while true { @@ -915,6 +928,16 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf if (remaining <> 0) and (args.src.length() > 0) { copy_input_size = remaining.min(no_more_than: args.src.length()) + if this.rb_short_workbuf { + // The ring buffer must not wrap (or reach its tail) and + // the workbuf must hold the input so far plus some + // slack. + total = (this.input_pos ~mod+ copy_input_size) ~mod+ RB_SHORT_SLACK + if (total > (this.rb_size as base.u64)) or + ((this.rb_buf_off ~mod+ total) > args.workbuf.length()) { + return base."#bad workbuf length" + } + } mark = args.src.mark() args.src.skip?(n: copy_input_size) this.copy_input_to_ring_buffer!(wb: args.workbuf, bytes: args.src.since(mark: mark)) diff --git a/std/brotli/encode_data.wuffs b/std/brotli/encode_data.wuffs index 71fdb728..7ce4ecba 100644 --- a/std/brotli/encode_data.wuffs +++ b/std/brotli/encode_data.wuffs @@ -17,29 +17,36 @@ // The workbuf holds, in this order (see README.md's "Encoder Workbuf Layout" // section, which compute_layout_offset mirrors): // - the storage (the C code's GetBrotliStorage) for one metablock, -// - the ring buffer (the C code's RingBuffer.data_), // - the hasher's memory (the C code's common->extra), -// - the commands (16 bytes each), and +// - the commands (16 bytes each), // - at qualities 4 and above, the metablock split (see // encode_metablock_greedy.wuffs), the literal context map, the -// EncodeContextMap symbols and the block encoders' depths and bits. +// EncodeContextMap symbols and the block encoders' depths and bits, and +// - the ring buffer (the C code's RingBuffer.data_). // The workbuf layout's region indexes. LAYOUT_END's offset is the total // length. +// The ring buffer is last, so that a workbuf shorter than workbuf_len works +// for short inputs (see transform_io's rb_short_workbuf check). pri const LAYOUT_STORAGE : base.u32 = 0 -pri const LAYOUT_RB : base.u32 = 1 -pri const LAYOUT_HASH : base.u32 = 2 -pri const LAYOUT_CMDS : base.u32 = 3 -pri const LAYOUT_TYPES_0 : base.u32 = 4 -pri const LAYOUT_LENGTHS_0 : base.u32 = 7 -pri const LAYOUT_HISTO_0 : base.u32 = 10 -pri const LAYOUT_COMB : base.u32 = 13 -pri const LAYOUT_CMAP : base.u32 = 14 -pri const LAYOUT_RLE : base.u32 = 15 -pri const LAYOUT_BE_DEPTH_0 : base.u32 = 16 -pri const LAYOUT_BE_BITS_0 : base.u32 = 19 +pri const LAYOUT_HASH : base.u32 = 1 +pri const LAYOUT_CMDS : base.u32 = 2 +pri const LAYOUT_TYPES_0 : base.u32 = 3 +pri const LAYOUT_LENGTHS_0 : base.u32 = 6 +pri const LAYOUT_HISTO_0 : base.u32 = 9 +pri const LAYOUT_COMB : base.u32 = 12 +pri const LAYOUT_CMAP : base.u32 = 13 +pri const LAYOUT_RLE : base.u32 = 14 +pri const LAYOUT_BE_DEPTH_0 : base.u32 = 15 +pri const LAYOUT_BE_BITS_0 : base.u32 = 18 +pri const LAYOUT_RB : base.u32 = 21 pri const LAYOUT_END : base.u32 = 22 +// RB_SHORT_SLACK is how many bytes past the input's end (in the ring buffer) +// a short workbuf must have: the C code reads at most 7 bytes past the +// input's end (its kSlackForEightByteHashingEverywhere), rounded up. +pri const RB_SHORT_SLACK : base.u64 = 16 + // The C code's MAX_NUM_DELAYED_SYMBOLS (in quality.h). pri const MAX_NUM_DELAYED_SYMBOLS : base.u64 = 0x2FFF diff --git a/test/c/std/brotli.c b/test/c/std/brotli.c index 0e07884d..8a1d2f4e 100644 --- a/test/c/std/brotli.c +++ b/test/c/std/brotli.c @@ -1331,7 +1331,7 @@ test_wuffs_brotli_encode_parameters() { dst.meta.wi = 0; status = wuffs_brotli__encoder__transform_io( enc, &dst, &src, - wuffs_base__make_slice_u8(g_work_slice_u8.ptr, (size_t)(wlen - 1))); + wuffs_base__make_slice_u8(g_work_slice_u8.ptr, (size_t)(wlen / 2))); BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_workbuf_length, "short workbuf at quality 2..9"); } @@ -1373,6 +1373,59 @@ test_wuffs_brotli_encode_parameters() { } } + // At qualities 2 to 9, the ring buffer is the workbuf's last region (of + // (2 + (1 << rb_bits) + (1 << lgblock) + 7) bytes) and a shorter workbuf + // works if it holds (2 + input_length + 16) bytes of it (and the input is + // short enough for the ring buffer never to wrap). + { + free(enc); + enc = wuffs_brotli__encoder__alloc(); + if (!enc) { + RETURN_FAIL("alloc failed"); + } + wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, + 3); + wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_LGWIN, 30); + wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_LARGE_WINDOW, 1); + uint64_t wlen = wuffs_brotli__encoder__workbuf_len(enc).max_incl; + uint64_t rb_region = 2 + (2ull << 30) + (1 << 14) + 7; + BROTLI_ENC_CHECK(wlen > rb_region, "workbuf_len at q3w30"); + size_t short_len = (size_t)(wlen - rb_region + 2 + 3 + 16); + uint8_t* short_workbuf = malloc(short_len); + if (!short_workbuf) { + RETURN_FAIL("malloc failed"); + } + for (int k = 0; k < 2; k++) { + wuffs_brotli__encoder* e = wuffs_brotli__encoder__alloc(); + if (!e) { + free(short_workbuf); + RETURN_FAIL("alloc failed"); + } + wuffs_brotli__encoder__set_quirk(e, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, + 3); + wuffs_brotli__encoder__set_quirk(e, WUFFS_BROTLI__QUIRK_LGWIN, 30); + wuffs_brotli__encoder__set_quirk(e, WUFFS_BROTLI__QUIRK_LARGE_WINDOW, + 1); + wuffs_base__io_buffer s = make_io_buffer_from_string("abc", 3); + s.meta.closed = true; + dst.meta.wi = 0; + status = wuffs_brotli__encoder__transform_io( + e, &dst, &s, + wuffs_base__make_slice_u8(short_workbuf, short_len - (k ? 0 : 1))); + free(e); + if (k == 0) { + if (status.repr != wuffs_base__error__bad_workbuf_length) { + free(short_workbuf); + RETURN_FAIL("too short workbuf at q3w30: %s", status.repr); + } + } else if (status.repr) { + free(short_workbuf); + RETURN_FAIL("short workbuf at q3w30: %s", status.repr); + } + } + free(short_workbuf); + } + // Qualities 10 and 11 (and 12, which sanitizes to 11) are unsupported. for (uint32_t q = 10; q <= 12; q++) { free(enc); From eb5ae76130fd7887b87877fdb8e821cb597418ab Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 27 Oct 2026 02:40:28 +0000 Subject: [PATCH 5/6] std/brotli: document large-window encoding and short workbufs --- doc/std/compression-encoders.md | 6 +- std/brotli/README.md | 144 +++++++++++++++++++++++++++----- 2 files changed, 126 insertions(+), 24 deletions(-) diff --git a/doc/std/compression-encoders.md b/doc/std/compression-encoders.md index 7ebd9f0a..d1a1fa66 100644 --- a/doc/std/compression-encoders.md +++ b/doc/std/compression-encoders.md @@ -32,4 +32,8 @@ encoder has `request_flush`, `request_emit_metadata` and ## Implementations -- [std/brotli](/std/brotli/README.md#encoder) (qualities 0 to 9 only). +- [std/brotli](/std/brotli/README.md#encoder) (qualities 0 to 9 only, + including large-window brotli, with windows of up to 1 GiB, at qualities 3 + to 9). Its `workbuf_len` is the worst case for the parameters (more than + 2 GiB for large-window brotli at lgwin 30), but a shorter workbuf works for + short inputs: see the README's "Encoder Workbuf Layout" section. diff --git a/std/brotli/README.md b/std/brotli/README.md index 23de58fe..60a8057e 100644 --- a/std/brotli/README.md +++ b/std/brotli/README.md @@ -154,8 +154,8 @@ invariants for memory safety but Wuffs requires explicit bounds checks. - Metadata callbacks (metadata blocks are currently skipped). See the "Metadata Blocks" section below. - Fast paths. -- Encoder quality levels 10 and 11, encoder dictionaries and large-window - encoding at qualities 3 and above (see the "Encoder" section below). +- Encoder quality levels 10 and 11 and encoder dictionaries (see the + "Encoder" section below). ## Encoder @@ -178,11 +178,11 @@ qualities 2 to 9 (the C code's `EncodeData` path: a ring buffer, hashers, 2, `BrotliStoreMetaBlockTrivial` at quality 3 and, at qualities 4 to 9, the literal context modeling heuristics, `BrotliBuildMetaBlockGreedy`, `BrotliOptimizeHistograms` and `BrotliStoreMetaBlock`). For qualities 10 and -11 (including the default quality, 11), and for large-window brotli at -qualities 3 and above, `transform_io` returns +11 (including the default quality, 11), `transform_io` returns `"#unsupported encoder quality"` (and `workbuf_len` is 0). Large-window -brotli works at quality 2 (and is ignored at qualities 0 and 1, like the C -code). +brotli (see "Encoder Large Window" below) works at qualities 3 to 9 with +lgwin 10 to 30. Like the C code, qualities 0 to 2 ignore the large-window +quirk. The source files are: @@ -213,6 +213,10 @@ The source files are: H54, H5, H6, H58, H68, H40, H41 and H42) and `SearchInStaticDictionary` (with `static_dict.c`'s matching), as one set of functions selected by the hasher type at run time. +- `encode_hasher_composite.wuffs`: the large-window hashers: + `hash_composite_inc.h` (H35, H55 and H65, which combine H3, H54 or H6 with + a rolling hasher) and `hash_rolling_inc.h` (`HROLLING_FAST` and + `HROLLING`). - `encode_backward_references.wuffs`: `backward_references.c` and `backward_references_inc.h` (`BrotliCreateBackwardReferences`), `command.h` and `ComputeDistanceCode`. @@ -283,7 +287,7 @@ first `transform_io` call, after which `set_quirk` returns | `QUIRK_MODE` | `BROTLI_PARAM_MODE` | | `QUIRK_DISABLE_LITERAL_CONTEXT_MODELING` | `BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING` (0 or 1) | | `QUIRK_SIZE_HINT` | `BROTLI_PARAM_SIZE_HINT` | -| `QUIRK_LARGE_WINDOW` (shared with the decoder) | `BROTLI_PARAM_LARGE_WINDOW` | +| `QUIRK_LARGE_WINDOW` (shared with the decoder) | `BROTLI_PARAM_LARGE_WINDOW` (0 or 1) | | `QUIRK_NPOSTFIX` | `BROTLI_PARAM_NPOSTFIX` | | `QUIRK_NDIRECT` | `BROTLI_PARAM_NDIRECT` | | `QUIRK_STREAM_OFFSET` | `BROTLI_PARAM_STREAM_OFFSET` (at most `1 << 30`) | @@ -293,7 +297,8 @@ with `base.QUIRK_QUALITY`. At qualities 0 and 1, only the quality, lgwin and stream offset affect the output (lgblock, mode, size hint, large window, npostfix, ndirect and literal context modeling are stored, sanitized and then ignored, as in the C code). At qualities 2 to 9, they have the same effect as -in the C code: lgblock (at qualities 4 and above; the C code's +in the C code: large window (see "Encoder Large Window" below), lgblock (at +qualities 4 and above; the C code's `ComputeLgBlock` fixes it to 14 below that), npostfix, ndirect and the mode (only `BROTLI_MODE_FONT` matters, through `ChooseDistanceParams`, at qualities 4 and above), disabling literal context modeling (at qualities 5 @@ -319,17 +324,15 @@ code's `ComputeRbBits`), `max_len` is `1 << min(rb_bits, 24)` (the C code's 1. The metablock storage: `(2 * max_len) + 503 + 16` bytes, like at qualities 0 and 1 but for the largest metablock. -2. The ring buffer (the C code's `RingBuffer.buffer_`): `2 + (1 << rb_bits) + - (1 << lgblock) + 7` bytes. The first 2 bytes are the copy of the last two - bytes that precede `data_`, the tail (`1 << lgblock` bytes) mirrors the - start of the buffer and the 7 trailing bytes are the C code's slack for - 8-byte loads. -3. The hasher's memory (the C code's `common->extra[0 .. 3]`): the sum of +2. The hasher's memory (the C code's `common->extra[0 .. 3]`): the sum of `FN(HashMemAllocInBytes)` for the hasher that `ChooseHasher` picks. When the size hint quirk is unset (zero), that choice depends on the input's length (through `UpdateSizeHint`), so this is the larger of the two - possible choices (for a size hint below or at least 1 MiB). -4. The commands: `16 * cmds_cap` bytes, in the C struct order: `insert_len_` + possible choices (for a size hint below or at least 1 MiB). With lgwin + above 24 (large window), the composite hashers H35, H55 and H65 add their + rolling hasher's table (`(1 << 24)` u32le values, 64 MiB) after H3's, + H54's or H6's memory. +3. The commands: `16 * cmds_cap` bytes, in the C struct order: `insert_len_` (u32le), `copy_len_` (u32le, the copy length in the low 25 bits and `copy_code - copy_len` in the high 7 bits), `dist_extra_` (u32le), `cmd_prefix_` (u16le) and `dist_prefix_` (u16le). `cmds_cap` is @@ -339,7 +342,7 @@ code's `ComputeRbBits`), `max_len` is `1 << min(rb_bits, 24)` (the C code's input block) fewer than that many commands and, like the C code's `num_commands_ + bytes / 2 + 1` allocation, each input block of `n` bytes adds at most `(n / 2) + 1` commands. -5. At qualities 4 and above, `BrotliBuildMetaBlockGreedy`'s `MetaBlockSplit` +4. At qualities 4 and above, `BrotliBuildMetaBlockGreedy`'s `MetaBlockSplit` and `BrotliStoreMetaBlock`'s scratch space: - the block types (u8) and then the block lengths (u32le) of the literal, command and distance block splits, with room for `(max_len / 512) + 1`, @@ -355,13 +358,38 @@ code's `ComputeRbBits`), `max_len` is `1 << min(rb_bits, 24)` (the C code's - `EncodeContextMap`'s `rle_symbols` (256 * 64 u32le values) and - the block encoders' depths (u8) and then bits (u16le) for up to 256 histograms per category. +5. The ring buffer (the C code's `RingBuffer.buffer_`): `2 + (1 << rb_bits) + + (1 << lgblock) + 7` bytes. The first 2 bytes are the copy of the last two + bytes that precede `data_`, the tail (`1 << lgblock` bytes) mirrors the + start of the buffer and the 7 trailing bytes are the C code's slack for + 8-byte loads. -Unlike the C code, which allocates its storage and command buffers as it -goes (for the actual metablock and block sizes), `workbuf_len` is the worst -case for the sanitized parameters, so that one workbuf works for any input. -At qualities 2 to 9, `transform_io` returns `"#bad workbuf length"` if the -workbuf is shorter than that. Some examples (in MiB, with the default -lgblock and an unset size hint): +Unlike the C code, which allocates its storage, ring buffer and command +buffers as it goes (for the actual metablock and block sizes, and the actual +input length), `workbuf_len` is the worst case for the sanitized parameters, +so that one workbuf works for any input. A shorter workbuf still works for +short inputs, because the ring buffer is the last region and the encoder +only touches its first `2 + n + 7` bytes for an input of `n` bytes, as long +as the ring buffer never wraps around. Precisely, at qualities 2 to 9: + +- `transform_io` returns `"#bad workbuf length"` if the workbuf is shorter + than `workbuf_len - R + 18`, where `R` is the ring buffer region's length + (`2 + (1 << rb_bits) + (1 << lgblock) + 7`), i.e. if it does not reach 16 + bytes into the ring buffer's data. +- If any `transform_io` call gets a workbuf shorter than `workbuf_len`, then + from then on (for the rest of the stream), before copying input into the + ring buffer, `transform_io` returns `"#bad workbuf length"` (without + consuming that input) if the total input so far (not counting metadata) + would be `n` bytes with `n + 16 > (1 << rb_bits)` (the ring buffer would + wrap, or its tail would be needed) or with the workbuf shorter than + `workbuf_len - R + 2 + n + 16`. + +So, for a total input of `n` bytes with `n + 16 <= (1 << rb_bits)`, a +workbuf of `workbuf_len - R + n + 18` bytes produces the same output as one +of `workbuf_len` bytes. The parity test checks this with exactly that length +(and that one byte less fails). This matters most for large-window brotli, +whose ring buffer is up to 2 GiB. Some examples of `workbuf_len` (in MiB, +with the default lgblock and an unset size hint): | quality | lgwin 10 | lgwin 16 | lgwin 18 | lgwin 22 | lgwin 24 | | ------- | -------- | -------- | -------- | -------- | -------- | @@ -372,6 +400,21 @@ lgblock and an unset size hint): | 7 | 4.4 | 4.4 | 13.8 | 51.4 | 107.5 | | 9 | 5.1 | 5.1 | 39.5 | 77.1 | 133.2 | +With the large-window quirk (at qualities 3 and above; lgwin up to 24 gives +the same numbers as without it), larger lgwin values double the ring buffer +for each extra bit, and lgwin above 24 adds the composite hashers' 64 MiB +rolling hash table (at quality 4 and at qualities 5 to 9 with lgwin of at +least 19, only for a size hint of at least 1 MiB, or an unset one, as +`ChooseHasher` otherwise keeps H3, H5 or H58): + +| quality | lgwin 25 | lgwin 26 | lgwin 28 | lgwin 30 | +| ------- | -------- | -------- | -------- | -------- | +| 3 | 160.6 | 224.6 | 608.6 | 2144.6 | +| 4 | 199.4 | 263.4 | 647.4 | 2183.4 | +| 5 | 197.5 | 261.5 | 645.5 | 2181.5 | +| 7 | 203.5 | 267.5 | 651.5 | 2187.5 | +| 9 | 229.2 | 293.2 | 677.2 | 2213.2 | + Fixed-size state (the hash table and command and literal buffers of qualities 0 and 1, the Huffman scratch space, the greedy block splitters' state, the block encoders' block type and length codes, etc) lives in the `encoder` struct (which is therefore @@ -429,6 +472,45 @@ encoder makes the same `EncodeData` calls, at the same points. One emitting the flint, and its callers then call it again. +### Encoder Large Window + +The `QUIRK_LARGE_WINDOW` quirk mirrors `BROTLI_PARAM_LARGE_WINDOW` +(large-window brotli is an incompatible extension of RFC 7932, decodable by +this package's decoder with the same quirk, see "Large Window" above, and by +libbrotlidec with `BROTLI_DECODER_PARAM_LARGE_WINDOW`). Like the C code's +`SanitizeParams`, `EnsureInitialized` and `ChooseHasher`: + +- At qualities 0 to 2 (the C code's `MAX_QUALITY_FOR_STATIC_ENTROPY_CODES` + and below), the quirk is ignored: lgwin is clamped to 24 and the output is + standard brotli. +- At qualities 3 and above, lgwin is clamped to `[10 ..= 30]` (instead of + `[10 ..= 24]`) and the stream starts with the 14-bit large-window header + (`EncodeWindowBits`), even for lgwin 24 and below. The distance parameters + (`BrotliInitDistanceParams` with `large_window`) allow distances up to + `BROTLI_MAX_ALLOWED_DISTANCE` with an alphabet of up to + `BROTLI_LARGE_MAX_DISTANCE_BITS` (62) distance bits, so the distance + Huffman codes are stored with a larger alphabet size than without the + quirk, even when no distance is large. Backward distances are at most + `(1 << lgwin) - 16`, so their extra bits (at most 30) fit in a command's + 32-bit `dist_extra_`, as in the C code. +- lgwin above 24 changes the hasher (only at qualities 3 to 9; the C code's + qualities 10 and 11 use H10 regardless): H3 becomes H35 (H3 plus + `HROLLING_FAST`), H54 becomes H55 (H54 plus `HROLLING_FAST`) and H6 and + H68 become H65 (H6, not H68, plus `HROLLING`). H5, H58 and H40 to H42 stay + as they are, so qualities 5 to 9 only use H65 when the size hint (set or + estimated by `UpdateSizeHint`) is at least 1 MiB (and lgwin is at least 19, + which it is). The rolling hashers find matches of at least 32 bytes at any + distance in the window (`HROLLING_FAST` only at positions and distances + that are multiples of 4) and do not search the static dictionary. +- lgblock is unaffected (it is at most 24), but `rb_bits` (and so the ring + buffer) grows with lgwin. Metablocks are still at most `1 << 24` bytes. + +Positions (the C code's `size_t` and the `WrapPosition`'ed `uint32_t` +values) and distances are `base.u64` throughout the encoder, so nothing is +truncated for windows up to `1 << 30`. The rolling hashers' table holds +32-bit positions, like the C code's. + + ### Encoder Testing `wuffs test -mimic std/brotli` checks byte-exact parity with libbrotlienc @@ -462,6 +544,22 @@ libbrotlienc v1.2.1 on x86-64. `BROTLI_MAX_SIMD_QUALITY` is undefined (no 64-bit count-trailing-zeros builtin) or 7 (MSVC on x86-64) choose differently and so produce different (but valid) output at those qualities. +- The composite hashers (H35, H55 and H65) are not separate hasher types + here: `hasher_type` is their first hasher (H3, H54 or H6) and a non-zero + `hr_jump` (4 for `HROLLING_FAST`, 1 for `HROLLING`) adds the rolling + hasher, whose functions run after the first hasher's, like the C code's + composite `FN(Prepare)`, `FN(StitchToPreviousBlock)` and + `FN(FindLongestMatch)`. The C code's composite hasher defers its inner + hashers' `FN(Initialize)` to its first `FN(Prepare)`. Here, the first + hasher's `FN(Initialize)` runs in `hasher_setup` (it does not touch the + hasher's memory, so this is equivalent) and the rolling hasher's at the + first `hasher_prepare`. The C code's composite gives its first hasher a + copy of its `HasherCommon` (with zeroed static dictionary statistics); + here, the first hasher uses the encoder's statistics, which the composite + itself never uses, so the result is the same. +- The workbuf's regions are in a different order than the C code's + allocations and the ring buffer is fully reserved from the start (see the + workbuf layout above). This does not change the output. - `BrotliBuildMetaBlockGreedy`'s two kinds of block splitter (one for a single histogram per block type, `metablock_inc.h`, and one for several literal contexts, `ContextBlockSplitter` in `metablock.c`) are one From b52e608b7c01e9e322f2f18f28c7eed410fc287d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 27 Oct 2026 04:18:32 +0000 Subject: [PATCH 6/6] std/brotli: record the large-window parity test results --- std/brotli/README.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/std/brotli/README.md b/std/brotli/README.md index 60a8057e..58f411a1 100644 --- a/std/brotli/README.md +++ b/std/brotli/README.md @@ -516,15 +516,25 @@ truncated for windows up to `1 << 30`. The rolling hashers' table holds `wuffs test -mimic std/brotli` checks byte-exact parity with libbrotlienc over a grid of qualities (0 to 9), lgwins, modes, input chunkings, flushes, metadata blocks, size hints and other parameters (lgblock, npostfix, ndirect, -disabling literal context modeling, stream offset, large window at quality 2), -as well as round trips through the decoder. +disabling literal context modeling, stream offset, large window), as well as +round trips through the decoder. Its large-window tests cover qualities 3 to +9 with lgwin 18 to 30 (and the H35, H55 and H65 hashers) on small files and +on a 46 MiB input with repeats at distances of about 20 and 40 MiB, decoded +by both this package's decoder and libbrotlidec. `script/manual-test-brotli-encoder-parity.c` runs a larger grid over every file in `test/data/brotli`, some larger files in `test/data` (up to about 400 KiB) and some synthetic inputs (up to 1 MiB). Its `-q=LO-HI` and `-shard=K/N` options select the qualities and split the inputs over several -processes. With `-q=0-9` (the default) it runs 718,555 configurations -(about 86 thousand of them at qualities 0 and 1), all byte-exact against -libbrotlienc v1.2.1 on x86-64. +processes, `-lw` runs only its large-window grid (lgwin 10 to 30 at +qualities 2 to 9, with various chunkings, flushes, size hints and other +parameters, each also with the shortest workbuf that should work) and `-big` +replaces the inputs with synthetic ones of 3 to 70 MiB with repeats at +distances of up to 40 MiB. With `-q=0-9` (the default) it runs 814,706 +configurations (96,688 of them in the large-window grid), all byte-exact +against libbrotlienc v1.2.1 on x86-64. With `-lw -big` it runs 720 more +(on 3, 6 and 12 MiB inputs with the large-window grid and on 24, 40 and +70 MiB inputs with one-shot and streaming large-window configurations at +lgwin 24 to 30), also all byte-exact. ### Encoder Differences from the C Code