Skip to content

fix(cctx): refines compression context sticky option behavior - #534

Merged
hellobertrand merged 3 commits into
mainfrom
fix/cctx-opts-defaults
Oct 16, 2026
Merged

hellobertrand merged 3 commits into
mainfrom
fix/cctx-opts-defaults

Conversation

@hellobertrand

@hellobertrand hellobertrand commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Reusable compression contexts (zxc_cctx) mishandled their sticky options: a zero level or block_size in a non-NULL opts reused the last call's value instead of a default, and a call that failed (e.g. ZXC_ERROR_DST_TOO_SMALL) still updated the remembered options. Fixing both exposed related defects in the block API.

Option resolution

  • opts == NULL reuses the options of the last successful call, starting from the creation options. A failed call leaves them untouched.
  • In a non-NULL opts, a zero level or block_size means the value the context was created with: the library defaults for zxc_create_cctx(NULL), otherwise what zxc_create_cctx or zxc_init_static_cctx received. It never depends on earlier calls. Static contexts therefore keep their pinned level and block size with no special case.
  • checksum_enabled and seekable are taken as given (0 = off), as in zxc_compress().

Block API

  • block_size is validated as requested: an invalid value returns ZXC_ERROR_BAD_BLOCK_SIZE whatever the source size, instead of being accepted and remembered (which then broke every frame call with NULL options).
  • The remembered block_size is the requested one, not the carve grown to hold the source.
  • The workspace is re-carved only when too small for the block; a larger carve encodes a block byte-for-byte identically, so alternating block sizes no longer reallocate on every call.

Behaviour changes

  • A zero level / block_size no longer reuses the previous call's value.
  • zxc_compress_block() now rejects a block_size that is not a power of two in range; main accepted some such values.
  • Static workspace sizes are unchanged.

Fixes #532

Summary by CodeRabbit

  • New Features
    • Reusable compression contexts retain settings from the last successful call when options are omitted. Zero-valued compression level or block size uses the context’s creation-time setting.
  • Bug Fixes
    • Invalid block sizes are more clearly identified, and failed compression calls leave retained settings unchanged.
    • Heap-based contexts can reuse workspace after a larger block operation; static contexts continue to reject changes requiring more workspace.
  • Documentation
    • Updated API and examples to explain option persistence, fallback behavior, and workspace handling.

Ensure that zero-valued options are correctly interpreted as defaults and that options are only cached upon successful compression calls. This prevents failed operations from polluting the sticky state and allows zeroed fields to properly trigger default behavior (such as using a pinned block size in static contexts).

Signed-off-by: Bertrand Lebonnois <5901952+hellobertrand@users.noreply.github.com>
Clarify the behavior of `zxc_compress_opts_t` by ensuring that zero-valued `level` or `block_size` fields fall back to the context creation values rather than library-wide defaults. This change ensures that sticky options correctly persist across successful calls while maintaining consistent behavior for both standard and static compression contexts. Failed calls now correctly leave the sticky state unchanged.

Signed-off-by: Bertrand Lebonnois <5901952+hellobertrand@users.noreply.github.com>
Ensure that zero-valued options in `zxc_compress_cctx` and block compression calls strictly revert to creation-time defaults rather than the last successful call's state. This change explicitly tracks `base_level` and `base_block_size` to decouple them from the sticky cache, preventing cross-call interference. Additionally, refine workspace re-carving logic to better handle block size and dictionary transitions, and ensure failed calls do not mutate the sticky context.

Signed-off-by: Bertrand Lebonnois <5901952+hellobertrand@users.noreply.github.com>
@hellobertrand hellobertrand added the bug Something isn't working label Oct 16, 2026
@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f4b5bcdf-fa58-4e75-927c-05489f990dd1
📥 Commits

Reviewing files that changed from the base of the PR and between 32db796 and 82417a2.

📒 Files selected for processing: 9
  • README.md
  • docs/API.md
  • docs/EXAMPLES.md
  • include/zxc_buffer.h
  • src/lib/zxc_dispatch.c
  • tests/test_common.h
  • tests/test_context_api.c
  • tests/test_main.c
  • tests/test_static_ctx.c

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

📝 Walkthrough

Reusable compression contexts now resolve zero-valued level and block_size from creation settings. Calls with NULL options reuse settings from the last successful call. Frame and block compression update sticky settings only after success.

Changes: Reusable compression context options

Layer / File(s) Summary
Option resolution and context initialization
src/lib/zxc_dispatch.c, include/zxc_buffer.h, docs/API.md, README.md, docs/EXAMPLES.md
Contexts now store creation-time base options and sticky options. Zero level or block_size uses the creation value. Documentation describes these rules and NULL-option reuse.
Frame compression option handling
src/lib/zxc_dispatch.c, tests/test_common.h, tests/test_context_api.c, tests/test_main.c, docs/API.md
Frame compression resolves options before processing and updates sticky options only after a positive result. Tests compare frame and block outputs after successful and failed calls, and check zero-valued options and creation-value fallbacks.
Block compression and static-context behavior
src/lib/zxc_dispatch.c, include/zxc_buffer.h, tests/test_static_ctx.c
Block compression resolves options and adjusts workspace size to fit the source. Heap contexts can reuse a larger carve for smaller blocks. Static-context tests check that zero-valued options use pinned initialization values.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 82417

No issue requiring a change before merge was established; normal checks remain appropriate.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: refining sticky option behavior for reusable compression contexts.
Linked Issues check Passed Issue #532 requires creation-value defaults for zero level and block_size in non-NULL options, sticky reuse only for NULL options, and unchanged sticky settings after failed calls. The implementation …
Out of Scope Changes check Passed The block API fixes, documentation updates, implementation refactoring, and regression tests support the sticky option requirements in #532 or the current PR scope. The changes validate requested bloc…
Docstring Coverage Passed Docstring coverage is 82.35% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 17 functions across 6 files. (3 skipped: 3 …
Full details: Linked Issues check

Explanation

Issue #532 requires creation-value defaults for zero level and block_size in non-NULL options, sticky reuse only for NULL options, and unchanged sticky settings after failed calls. The implementation resolves these cases for frame and block compression, and updates sticky settings only after success. Tests cover successful calls, failed calls, NULL reuse, creation values, and invalid block sizes.

Full details: Out of Scope Changes check

Explanation

The block API fixes, documentation updates, implementation refactoring, and regression tests support the sticky option requirements in #532 or the current PR scope. The changes validate requested block sizes, preserve the requested sticky block size, and avoid unnecessary workspace re-carving. No unrelated change is established.

Full details: Docstring Coverage

Explanation

Docstring coverage is 82.35% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 17 functions across 6 files. (3 skipped: 3 unsupported.)

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@hellobertrand
hellobertrand marked this pull request as ready for review October 16, 2026 15:02
@hellobertrand

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@codecov

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@hellobertrand

Copy link
Copy Markdown
Owner Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@hellobertrand
hellobertrand merged commit 88a39b4 into main Oct 16, 2026
159 checks passed
@hellobertrand
hellobertrand deleted the fix/cctx-opts-defaults branch October 16, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zxc_compress_cctx: zero fields in a non-NULL opts reuse the remembered settings instead of the documented defaults (also after a failed call)

1 participant