Skip to content

Convert TypedImage to TypedImageRef prior to resizing to reduce monomorphization and code size - #66

Open
bgw wants to merge 1 commit into
Cykooz:mainfrom
bgw:reduce-monomorphization
Open

bgw wants to merge 1 commit into
Cykooz:mainfrom
bgw:reduce-monomorphization

Conversation

@bgw

@bgw bgw commented Nov 19, 2026

Copy link
Copy Markdown

Thank you for this library! We've been using it in https://github.com/vercel-labs/next-safe-image-optimization (which we want to start using in Next.js very soon).

I've been trying to reduce binary size with insights gathered via https://github.com/Boshen/cargo-bsize, and I discovered that the combination of TypedImage and TypedImageRef in fast_image_resize can lead to a bunch of wasted binary size.

It's very easy to convert a TypedImage to a TypedImageRef, and TypedImageRef shouldn't be any slower to resize, so we should always do the conversion when possible. This reduces the amount of monomorphization work the compiler does.

Some real-world numbers for aarch64 linux here: vercel-labs/next-safe-image-optimization#22

If you're interested, I think there's some similar improvements that could be done for the cropped image representations, but it seemed possibly a little more involved, and this PR seemed like a good first step.

AI Disclaimer

I don't know what your desired contribution policy for this repository is.

I used Claude Code with Opus 5.5 to write the code, though this was not the first approach it suggested. The first was involved inverting a loop, modified every convolution implementation (a huge diff), and added extra branching for every row of pixels (likely a couple % perf hit).

I rewrote all the comments to be clearer, and carefully reviewed the diff before submission. This PR description is hand-written.

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