Skip to content

split pTreeList into struct-of-arrays: eliminates multiply in hot loop#113

Merged
dloebl merged 1 commit into
mainfrom
optimize-lzw-treelist
Mar 31, 2026
Merged

split pTreeList into struct-of-arrays: eliminates multiply in hot loop#113
dloebl merged 1 commit into
mainfrom
optimize-lzw-treelist

Conversation

@dloebl

@dloebl dloebl commented Mar 24, 2026

Copy link
Copy Markdown
Owner

Improve raw LZW encoding speed by ~30% (ARM) / ~12% (x86) by splitting pTreeList into separate arrays:
pTreeList stored 3 uint16_t fields per node, requiring a * 3 multiply on every access in the hot loop. Splitting into three individual arrays (pTreeListMap, pTreeListColor, pTreeListIdx) eliminates the multiply - each array is indexed directly by parentIndex.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves raw LZW encoding performance by refactoring the per-node “tree list” representation from an array-of-struct-like layout into a struct-of-arrays layout, removing a * 3 index multiply from a hot path.

Changes:

  • Split pTreeList into three dedicated uint16_t arrays: pTreeListMap, pTreeListColor, and pTreeListIdx.
  • Update dictionary reset, child insertion, and tree crawl logic to use the new arrays.
  • Update allocation and cleanup to manage the three arrays.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dloebl
dloebl force-pushed the optimize-lzw-treelist branch from 771ac2c to 7c16f5a Compare March 24, 2026 13:56

@MCLoebl MCLoebl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the variable names make sense to you, please merge. Could you check if the speed improvement is still there for more noisy (real) images? When pTreeMap is needed more, the speed improvement may be smaller I guess.

@dloebl
dloebl merged commit f1272ac into main Mar 31, 2026
15 checks passed
@dloebl
dloebl deleted the optimize-lzw-treelist branch March 31, 2026 09:41
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