Skip to content

Optimize deflate_find_min_cost_path() with AVX2 and NEON - #477

Merged
ebiggers merged 6 commits into
masterfrom
dev
Nov 10, 2026
Merged

ebiggers merged 6 commits into
masterfrom
dev

Conversation

@ebiggers

@ebiggers ebiggers commented Nov 9, 2026

Copy link
Copy Markdown
Owner

No description provided.

@ebiggers
ebiggers force-pushed the dev branch 7 times, most recently from 7388149 to fa387b0 Compare November 10, 2026 06:32
Replace the 'optimum_nodes' array, which was an array of pairs of u32,
with two arrays of u32.

deflate_find_min_cost_path() does up to 256 consecutive reads from
'cost_to_end' for each write it does to 'cost_to_end' and 'item'.
Making the 'cost_to_end' values contiguous enables SIMD optimizations
without having to deinterleave the fields.

This also works well for deflate_tally_item_list() and
deflate_flush_block(), which only use 'item', not 'cost_to_end'.
The sizing of the 'cost_to_end' array currently relies on the fact that
deflate_compress_near_optimal() can choose a block_length very close to
MAX_BLOCK_LENGTH only at the very end of the input buffer.  In that case
there will be no matches extending past block_length, so the usual
sentinel entries in 'cost_to_end' are unneeded.

It's correct, but a bit subtle.  Let's just reserve the extra 257
entries (which is negligible when the array already had size 305000),
and omit the MIN() in deflate_optimize_and_flush_block().  This way,
this subtle assumption about MAX_BLOCK_LENGTH goes away.
@ebiggers
ebiggers force-pushed the dev branch 5 times, most recently from 735a41d to db26939 Compare November 10, 2026 07:19
Introduce AVX2 (x86_32 and x86_64) and NEON (arm64 only for now)
optimized implementations of deflate_find_min_cost_path().

These implementations use vector instructions to handle the
consideration of multiple lengths for the same match offset much more
efficiently than the scalar code did.

Some benchmark results at compression level 12:
  - 43% higher compression throughput on an x86_64 AMD Ryzen 9950X
    (Zen 5) processor
  - 27% higher compression throughput on an arm64 C1-Ultra processor

The resulting compressed data remains identical.

The portable implementation is made faster as well, as it's restructured
to the more efficient form that matches the AVX2 and NEON code.
deflate_optimize_and_flush_block() computes the frequency of each byte
in the uncompressed block twice: once in deflate_choose_all_literals()
and once in deflate_set_initial_costs().  This is redundant.

Move deflate_set_initial_costs() up before the static block check (which
overwrites c->freqs) and make it reuse c->freqs from
deflate_choose_all_literals().
@ebiggers
ebiggers merged commit a8cac98 into master Nov 10, 2026
52 checks passed
@ebiggers
ebiggers deleted the dev branch November 10, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant