fix: reduce peak memory ~9× in LZW decoder and TIFF encoder; clean up TODO#104
Merged
fix: reduce peak memory ~9× in LZW decoder and TIFF encoder; clean up TODO#104
Conversation
Agent-Logs-Url: https://github.com/cross-org/image/sessions/a2dbab3b-c67b-40c9-8677-19be1c239c59 Co-authored-by: Hexagon <419737+Hexagon@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
Hexagon
April 9, 2026 22:55
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Reduces peak memory usage in two hot paths by eliminating large intermediate number[] byte accumulators, replacing them with Uint8Array-based buffering and final assembly via pre-sized typed arrays. This aligns with the project’s focus on efficient binary image encode/decode paths.
Changes:
- Refactored
LZWDecoder.decompress()to write into a growableUint8Arrayinstead of anumber[]. - Refactored TIFF
encode()/encodeFrames()to keep compressed pixel data asUint8Arraychunks and assemble output via a single preallocatedUint8Array. - Cleaned up
TODO.mdand added[Unreleased]changelog entries for the memory improvements.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| TODO.md | Removes completed items and marks M5/M6 as fixed. |
| src/utils/lzw.ts | Implements a growable Uint8Array output buffer for LZW decompression. |
| src/formats/tiff.ts | Avoids pixel data copying into number[]; assembles final TIFF bytes with Uint8Array.set(). |
| CHANGELOG.md | Documents the LZW/TIFF memory improvements under [Unreleased]. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two medium-severity memory inefficiencies where large typed data was funnelled through
number[](8 bytes/element), causing ~9× peak memory overhead before finalUint8Arrayconversion.Changes
src/utils/lzw.ts— M5LZWDecoder.decompress()replacednumber[]accumulator with a doublingUint8Arraybufferbuf.set(entry, len)(bulk) instead of byte-by-bytepush()buf.slice(0, len)to release the oversized backing buffer and allow GCsrc/formats/tiff.ts— M6encode()andencodeFrames()now keep compressed pixel frames asUint8Arraychunks; only the small IFD section (~hundreds of bytes) is built in anumber[]Uint8Array+set()calls — eliminates the intermediate JS number array for pixel data entirelyTODO.mdRemoved all verified-complete items (C1–C3, H1–H4, M1–M4, M7, L5). Remaining open: M5/M6 (now addressed above) and L1–L4, L6–L8.
CHANGELOG.mdAdded entries for M5 and M6 under
[Unreleased].