Skip to content

Conversation

@hjanuschka
Copy link
Contributor

Summary

Add #[inline(never)] wrapper pattern to prevent monomorphization of heavy process functions across multiple input types. The public API remains generic for ergonomics, but delegates to non-generic internal functions that use dynamic dispatch.

Changes

  • decoder.rs: Add process_dyn/skip_frame_dyn with #[inline(never)]
  • process.rs: Use &mut dyn JxlBitstreamInput
  • codestream_parser/mod.rs: Use &mut dyn JxlBitstreamInput
  • box_parser.rs: Use &mut dyn JxlBitstreamInput

Binary size impact

Tested with example using 3 different input types (&[u8], BufReader<Cursor>, BufReader<File>):

Metric Before After
.text section 4,065 KB 3,883 KB
process() copies 3 × 86KB 1 × 86KB
Savings ~177 KB

Why this approach?

We evaluated 3 options:

  • Option A: #[inline(never)] on internal functions only → didn't work (public API still monomorphized)
  • Option B: Change public API to &mut dyn → works but breaks API
  • Option C (this PR): Generic wrapper + #[inline(never)] internal → works, API compatible ✓

Test plan

  • cargo build --release passes
  • cargo fmt passes
  • CI tests pass

Add #[inline(never)] wrapper pattern to prevent monomorphization of
heavy process functions across multiple input types. The public API
remains generic for ergonomics, but delegates to non-generic internal
functions that use dynamic dispatch.

Changes:
- decoder.rs: Add process_dyn/skip_frame_dyn with #[inline(never)]
- process.rs: Use &mut dyn JxlBitstreamInput
- codestream_parser/mod.rs: Use &mut dyn JxlBitstreamInput
- box_parser.rs: Use &mut dyn JxlBitstreamInput

Binary size impact (with 3 input types):
- Before: 4,065 KB (.text), 3x86KB process copies
- After:  3,883 KB (.text), 1x86KB process copy
- Savings: ~177 KB
@hjanuschka hjanuschka closed this Dec 2, 2025
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