Skip to content

Avoid stack-heavy Huffman optimization for fixed deflate blocks - #310

Closed
xiaobai050 wants to merge 1 commit into
m5stack:masterfrom
xiaobai050:fix/miniz-fixed-huffman-stack-20260923
Closed

xiaobai050 wants to merge 1 commit into
m5stack:masterfrom
xiaobai050:fix/miniz-fixed-huffman-stack-20260923

Conversation

@xiaobai050

Copy link
Copy Markdown

Summary

Construct RFC 1951 fixed literal/length and distance Huffman codes directly when starting a static deflate block. The generic optimizer is still used for dynamic blocks.

The fixed codes are predetermined by the format, but tdefl_start_static_block() currently calls tdefl_optimize_huffman_table() twice. In an ESP-IDF build this call path had a 5,584-byte stack frame, even with static_table=true, which made PNG snapshot compression expensive on a small UI task stack. The code tables already live in tdefl_compressor; this change fills them using scalar locals and reverses each code for the deflate bit order.

Validation

  • The same source transformation has been used in a local M5GFX adapter for PNG snapshots; fixed deflate streams were checked with lossless pixel round trips and worst-case inputs in that project.
  • The fixed-code ranges and bit lengths follow RFC 1951 section 3.2.6. Dynamic Huffman construction is unchanged.
  • Source diff check passed. This standalone upstream branch has not been compiled in this preparation environment.

@ainyan03

Copy link
Copy Markdown
Contributor

Thank you for this. The fixed codes match RFC 1951, and on the host the compressor gives byte-identical output before and after the change (static and dynamic blocks, 168 inputs).

The files under src/lgfx/ are maintained in LovyanGFX and synced into M5GFX from there, so I have taken your commit (with you as the author) into lovyan03/LovyanGFX#943. It will reach M5GFX with the next sync, so I'm closing this one.

One note on the effect: the peak stack of the built-in createPng does not change, because it uses dynamic blocks, which still run tdefl_optimize_huffman_table(). The saving applies to callers that force static blocks, as in your use.

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.

3 participants