Skip to content

feat: Caches dict Huffman tree upon attach#320

Merged
hellobertrand merged 5 commits into
mainfrom
perf/dict-tree-at-attach
Jul 11, 2026
Merged

feat: Caches dict Huffman tree upon attach#320
hellobertrand merged 5 commits into
mainfrom
perf/dict-tree-at-attach

Conversation

@hellobertrand

Copy link
Copy Markdown
Owner

Optimizes dictionary literal Huffman processing by moving the tree and code generation out of the per-block hot path.

This change significantly improves performance by:

  • Introducing a zxc_dict_huf_state_t structure within the compression context (zxc_cctx_t) to store the dictionary's PivCo Huffman tree, canonical codes, and unpacked code lengths.
  • Allocating memory for this state directly within the context's workspace when a dictionary is active.
  • Modifying zxc_cctx_attach_dict_huf to build this dictionary Huffman state once during context attachment, rather than rebuilding it for every block.
  • Updating all dictionary-aware Huffman encoding, decoding, and size calculation functions to directly utilize this prebuilt and cached state from the context.

This reduces redundant computation, lowering CPU overhead during compression and decompression of dictionary-referenced literal sections.

@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/lib/zxc_dispatch.c 66.66% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@hellobertrand hellobertrand force-pushed the perf/dict-tree-at-attach branch 2 times, most recently from 9430950 to 3aa5c24 Compare July 11, 2026 15:24
Introduces a "tree-at-attach" mechanism for dictionary-based PivCo Huffman.
The Huffman tree, canonical codes, and unpacked code lengths are now pre-built once when a dictionary is attached to the compression context. This avoids redundant computation (unpacking lengths and rebuilding the tree) for every block, improving performance for dictionary literal sections during encoding and decoding.
Instead of storing a pointer to caller-owned raw code lengths and building the tree on demand, `zxc_cctx_attach_dict_huf` now immediately unpacks the lengths and constructs the PivCo tree and canonical codes into the context. This "tree-at-attach" strategy eliminates redundant computation during per-block encoding/decoding and clarifies memory ownership, as the input `lengths` are only required to be valid during the attachment call.
Applies the standard function suffixing macro to the dictionary Huffman size calculation function. This ensures it adheres to the consistent naming convention and enables specialized, potentially optimized, variants for the 'tree-at-attach' performance strategy.
Removes the `static` keyword from `zxc_pivco_tree_t tree` declarations within Huffman dictionary test cases. This ensures each test invocation uses a fresh, stack-allocated tree, preventing unintended state sharing between test runs. This adjustment is necessary to align the test setup with the 'tree-at-attach' strategy, where the dictionary's Huffman tree is explicitly managed by the compression context.
The dictionary Huffman state (PivCo tree, canonical codes, and code lengths) is now encapsulated in a `zxc_dict_huf_state_t` struct. This struct is allocated within the compression context's workspace, but only when a dictionary is attached.

This refactoring reduces the base `zxc_cctx_t` struct size for contexts that do not use a dictionary, improving memory efficiency by avoiding the unconditional allocation of dictionary-specific Huffman data. It reinforces the "tree-at-attach" strategy by centralizing and conditionally allocating this pre-built state.
@hellobertrand hellobertrand force-pushed the perf/dict-tree-at-attach branch from 3aa5c24 to a99e1cc Compare July 11, 2026 16:34
@sonarqubecloud

sonarqubecloud Bot commented Jul 11, 2026

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@hellobertrand hellobertrand merged commit 13d791e into main Jul 11, 2026
83 of 84 checks passed
@hellobertrand hellobertrand deleted the perf/dict-tree-at-attach branch July 11, 2026 19:31
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