Repository navigation
perf(plugin-resize): faster BILINEAR, BICUBIC, HERMITE and BEZIER - #1528
Merged
Merged
Conversation
Hashes of the output of every mode, with and without premultiplied alpha, for a set of images and sizes, generated from the current resizers so later speedups can be checked to give the same bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Give exactly the same bytes as before, a lot faster: - BICUBIC, HERMITE and BEZIER: each source row is premultiplied once instead of every time a pixel is read, the kernels are module functions called from their own call sites rather than closures passed in, the per-column positions are worked out once, and the passes run a row at a time in small functions that V8 optimizes well. Where t is 0, as when shrinking by a whole factor, every kernel gives the pixel itself, so it is copied. - BILINEAR: each needed source row is premultiplied and interpolated to the new width once, rather than for every destination pixel. - default: the check for transparency no longer calls a function for every byte, and the width pass works a line at a time instead of a column at a time, keeping the sums in float32 as before. Fixes #758 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Math.sin differs in the last bit between platforms and engines, so the bytes of Lanczos resizes can't be pinned. The other modes only use arithmetic that gives the same results everywhere, so the test can run in browsers too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hipstersmoothie
force-pushed
the
sesh/jimp-faster-resize-modes-758
branch
from
November 23, 2026 23:55
c2d0c99 to
6bc73c3
Compare
Collaborator
Author
|
🚀 PR was released in |
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.
Fixes #758
The
resize3.jslinked in the issue is based on the v0 file and predates #1433, #1459, #1481, #1499 and #1502, so it couldn't be dropped in. This PR applies the same idea (no per-pixel closure calls) plus a few more, while keeping byte-identical output to main for every mode, bothpremultiplyAlphasettings, up- and downscaling, and odd sizes.What changed
BICUBIC, HERMITE, BEZIER (
interpolate2D, previously_interpolate2D)Float64Array, using the same arithmetic aspremultiplied(). Before, each value was recomputed, with its division, for every one of the 4 taps × 4 channels.interpolate(kernel, …), so each call site always calls the same function and V8 can inline it. Before, a closure was passed in, and once a process had used more than one mode, that call site saw several functions and V8 stopped inlining it.tare computed once, not once per row.--trace-deopt).t === 0, every kernel givesx1exactly (rounded for HERMITE/BEZIER, truncated by theUint8Arraystore for BICUBIC), so the pixel is copied. That covers shrinking by a whole factor (e.g. ½): passes 1 and 2 become copies and only the block average does real work.tweights would change float rounding and so change bytes.BILINEAR
min/max/distances once. Each needed source row is premultiplied and interpolated to the new width once (the two rows in use are cached), instead of being recomputed for every destination pixel.Default resizer
index.ts: the transparency check wasdata.some((value, i) => …), a closure call for every byte (15M for hands.jpg) and about 60% of the default resizer's time. It's now a loop over just the alpha bytes, with the same result.resize.tswidth passes walked every row for each output column. They now precompute each column's source offsets and weights once and go line by line. In the averaging pass, sums are rounded withMath.froundat each step, exactly as the oldFloat32Arrayaccumulator rounded them. I tried the same kind of rewrite on the interpolated height pass, but it gained nothing, so it's unchanged.LANCZOS, LANCZOS2, MITCHELL and NEAREST_NEIGHBOR are untouched.
Exactness
src/exactness.test.ts, runs in Node and the browser): 7 images (noise with random alpha, opaque noise and a gradient fading to transparent; 1×1 up to 97×61) are resized to 15 sizes each (up, down by whole and fractional factors, one axis only, unchanged, swapped axes, 1×1). That's 102 results per mode, for default, NEAREST_NEIGHBOR, BILINEAR, BICUBIC, HERMITE, BEZIER and MITCHELL × bothpremultiplyAlphasettings: 1,428 outputs. The test snapshots a 53-bit hash of each output plus its size. I chose hashes over committing reference images or a vendored copy of the old functions (~1,100 lines) because they're small and stay meaningful for later changes. The snapshot is committed in the first commit, generated from main's resizers. The perf commit doesn't touch it, and it still passes. LANCZOS and LANCZOS2 aren't in the test: they useMath.sin, whose last bit differs between platforms (macOS arm64 vs Linux CI) and engines (Node vs Chromium), which changes a few bytes. They're untouched by this PR anyway. The last commit only drops their entries from the snapshot; no other hash changed. The other modes use only+ - * /,floor,ceil,roundandfround, which give the same results everywhere.methods.resizefrom a build of main was compared with the final build of this branch on 31,000 random cases. Source sizes ran 1–400 px; image kinds were noise, opaque, smooth, binary alpha, fully transparent and flat; target sizes were random, scale factors (½, ⅓, ¼, 0.37, 0.9, 1.1, 1.5, 2, 3, 4.3) or one axis unchanged. Every mode was included, withpremultiplyAlphaon and off, plus extra default-mode runs at large widths. Each comparison checked width, height, every byte, and whether the result is aBuffer. 0 mismatches.Benchmarks
Node 24, Apple Silicon,
hands.jpg(2376×1584). CPU time perresizecall in ms. The benchmark loads main and this branch in the same process and alternates them, because the machine was shared and heavily loaded. Each process runs one mode, and the numbers are the median of 3 rounds × 11 runs. LANCZOS (unchanged) is a control.The table above is the best case for main, since each process only ever used one mode. The audit numbers came from a single process that uses all modes in turn (median of 7, wall clock), where main's closure call sites see several functions:
What's left
On non-integer downscales (e.g. 2376 → 1000), HERMITE and BEZIER are 1.4–2× faster rather than 4–8×. That time is now the kernel arithmetic itself, about 18M evaluations at ~5 ns each. Making it much faster would mean changing the formulas, e.g. precomputing weights per
tor reordering the multiplications, and that changes float rounding and therefore bytes. I left the formulas exactly as they were. The default resizer's upscale is mostly the interpolated height pass (one rounded multiply-add per output byte). A per-line rewrite of that pass gained nothing, so it's unchanged.🤖 Generated with Claude Code