8000 perf(plugin-resize): faster BILINEAR, BICUBIC, HERMITE and BEZIER by hipstersmoothie · Pull Request #1528 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

perf(plugin-resize): faster BILINEAR, BICUBIC, HERMITE and BEZIER - #1528

Merged
hipstersmoothie merged 3 commits into
mainfrom
sesh/jimp-faster-resize-modes-758
Nov 24, 2026
Merged

hipstersmoothie merged 3 commits into
mainfrom
sesh/jimp-faster-resize-modes-758

Conversation

@hipstersmoothie
@hipstersmoothie hipstersmoothie commented Nov 23, 2026 •
Copy link
Copy Markdown
Collaborator

Fixes #758

The resize3.js linked 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, both premultiplyAlpha settings, up- and downscaling, and odd sizes.

What changed

BICUBIC, HERMITE, BEZIER (interpolate2D, previously _interpolate2D)

  • Each source row is premultiplied once into a Float64Array, using the same arithmetic as premultiplied(). Before, each value was recomputed, with its division, for every one of the 4 taps × 4 channels.
  • The kernels are module-level functions called through 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.
  • Each column's source position and t are computed once, not once per row.
  • Row pass, column pass and block averaging are small per-row functions. In one big function called once, V8 kept leaving and re-entering optimized code for the inner loops ("exit from OSR'd inner loop" in --trace-deopt).
  • Where t === 0, every kernel gives x1 exactly (rounded for HERMITE/BEZIER, truncated by the Uint8Array store 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.
  • I didn't change any kernel formula. Precomputing per-t weights would change float rounding and so change bytes.

BILINEAR

  • Precomputes each axis's 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 was data.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.ts width 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 with Math.fround at each step, exactly as the old Float32Array accumulator 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

  1. Committed snapshot (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 × both premultiplyAlpha settings: 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 use Math.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, round and fround, which give the same results everywhere.
  2. Differential fuzzing (local, not committed): methods.resize from 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, with premultiplyAlpha on and off, plus extra default-mode runs at large widths. Each comparison checked width, height, every byte, and whether the result is a Buffer. 0 mismatches.

Benchmarks

Node 24, Apple Silicon, hands.jpg (2376×1584). CPU time per resize call 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.

mode case main this PR speedup
default 2376×1584 → 1188 (½) 81 19 4.4×
default 2376×1584 → 1000 80 24 3.3×
default 600×400 → 1500 (2.5× up) 27 25 1.1×
default 1188×792 RGBA → 500 20 15 1.4×
BILINEAR 2376×1584 → 1188 (½) 22 13 1.6×
BILINEAR 2376×1584 → 1000 46 28 1.6×
BILINEAR 600×400 → 1500 (2.5× up) 99 20 5.0×
BILINEAR 1188×792 RGBA → 500 15 10 1.5×
BICUBIC 2376×1584 → 1188 (½) 167 43 3.8×
BICUBIC 2376×1584 → 1000 146 85 1.7×
BICUBIC 600×400 → 1500 (2.5× up) 52 26 2.0×
BICUBIC 1188×792 RGBA → 500 43 21 2.0×
HERMITE 2376×1584 → 1188 (½) 170 43 3.9×
HERMITE 2376×1584 → 1000 184 123 1.5×
HERMITE 600×400 → 1500 (2.5× up) 77 42 1.9×
HERMITE 1188×792 RGBA → 500 62 41 1.5×
BEZIER 2376×1584 → 1188 (½) 205 43 4.8×
BEZIER 2376×1584 → 1000 201 139 1.4×
BEZIER 600×400 → 1500 (2.5× up) 81 47 1.7×
BEZIER 1188×792 RGBA → 500 68 44 1.5×
LANCZOS (control) 2376×1584 → 1188 (½) 66 61 1.1×
LANCZOS (control) 2376×1584 → 1000 60 58 1.0×
LANCZOS (control) 600×400 → 1500 (2.5× up) 40 39 1.0×
LANCZOS (control) 1188×792 RGBA → 500 15 15 1.0×

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:

mode 2376 → 1188 (½) main this PR 2376 → 1000 main this PR
BICUBIC 109 39 (2.8×) 200 76 (2.6×)
HERMITE 222 32 (6.9×) 241 120 (2.0×)
BEZIER 247 32 (7.7×) 263 133 (2.0×)

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 t or 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

@hipstersmoothie hipstersmoothie added patch Increment the patch version when merged performance Improve performance of an existing feature labels Nov 23, 2026
hipstersmoothie and others added 3 commits November 23, 2026 15:55
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
hipstersmoothie force-pushed the sesh/jimp-faster-resize-modes-758 branch from c2d0c99 to 6bc73c3 Compare November 23, 2026 23:55
@hipstersmoothie
hipstersmoothie merged commit cca86f3 into main Nov 24, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/jimp-faster-resize-modes-758 branch November 24, 2026 00:01
@hipstersmoothie
Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.0.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch Increment the patch version when merged performance Improve performance of an existing feature released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slightly faster version of resize2.js available

1 participant

0