Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ The WASM worker bundle is loaded lazily — only when the first image needs proc

3. **Single instance**: `getVips()` caches a promise so concurrent first-time calls share one initialization. The worker stays alive across uploads and is terminated by `terminateVipsWorker()` when the upload queue empties or when it is recycled (see Memory management below).

4. **Memory management**: WASM linear memory can only grow, never shrink, so the pipeline actively limits how much it accumulates:
4. **Memory management**: WASM linear memory can only grow, never shrink (and with wasm-vips 0.0.19 it grows up to 2 GiB, although growth can fail on memory-constrained devices), so the pipeline actively limits how much it accumulates:
- The libvips operation cache is disabled at startup (`Cache.max(0)`). libvips otherwise caches the results of previous operations, which grows WASM memory unbounded across a batch of uploads and can trigger out-of-memory crashes.
- The worker is recycled (terminated and recreated on next use) after every 50 completed vips operations, counting both successes and failures so a burst of failures can't bypass the budget. Recycling is deferred while any operation is still in flight so an active worker is never killed mid-operation.
- Emscripten's `setAutoDeleteLater(true)` handles automatic cleanup, and each operation also runs a manual cleanup after completion. Operations are tracked in an `inProgressOperations` set so they can be cancelled mid-flight via a progress-callback kill switch.
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/upload-media/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Enhancements

- Add a `generateAnimatedImageSubsizes` store setting, carried in the REST API root index from the new `wp_generate_animated_image_subsizes` filter, and a matching `preserveAnimation` option on `vipsResizeImage`. Sub-sizes of animated GIF and WebP images, cropped ones included, keep their animation instead of flattening to the first frame unless the setting is `false` ([#80385](https://github.com/WordPress/gutenberg/pull/80385)).
- Upgrade `wasm-vips` to 0.0.19, which can grow WASM memory up to 2 GiB, and raise the client-side image memory budgets accordingly ([#84980](https://github.com/WordPress/gutenberg/pull/84980)).

## 0.42.0 (2026-10-07)

Expand Down
17 changes: 9 additions & 8 deletions packages/upload-media/src/feature-detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,27 +209,28 @@ const BYTES_PER_PIXEL = 4;
/**
* Memory budget (in bytes) for processing interlaced images client-side.
*
* wasm-vips is hard-capped at 1 GiB of WASM memory. Progressive JPEGs and
* Adam7-interlaced PNGs cannot be decoded with shrink-on-load, so the entire
* image must be buffered at once. A tighter ~0.5 GiB budget leaves headroom
* for the encode step and avoids the out-of-memory failures these images hit.
* wasm-vips can grow its WASM memory up to 2 GiB, but growth can fail on
* memory-constrained devices. Progressive JPEGs and Adam7-interlaced PNGs
* cannot be decoded with shrink-on-load, so the entire image must be buffered
* at once. A tighter ~0.75 GiB budget leaves headroom for the encode step and
* avoids the out-of-memory failures these images hit.
*/
const INTERLACED_MEMORY_BUDGET = 0.5 * 1024 * 1024 * 1024;
const INTERLACED_MEMORY_BUDGET = 0.75 * 1024 * 1024 * 1024;

/**
* Memory budget (in bytes) for processing non-interlaced images client-side.
*
* Baseline images can be shrunk during decode, so vips needs far less peak
* memory. A generous ~0.9 GiB budget acts as a backstop against extreme sizes
* memory. A generous ~1.5 GiB budget acts as a backstop against extreme sizes
* while leaving the vast majority of real-world uploads on the client.
*/
const BASELINE_MEMORY_BUDGET = 0.9 * 1024 * 1024 * 1024;
const BASELINE_MEMORY_BUDGET = 1.5 * 1024 * 1024 * 1024;

/**
* Determines whether an image is too large to process client-side.
*
* Very large images, especially interlaced/progressive ones, can exceed the
* 1 GiB wasm-vips memory cap and fail to process. Such images are better
* 2 GiB wasm-vips memory ceiling and fail to process. Such images are better
* handled by the server, which has no comparable per-image memory ceiling.
*
* @param dimensions The image's parsed dimensions and interlacing.
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-media/src/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,8 @@ export function prepareItem( id: QueueItemId ) {
} )
: file;

// Gate very large images out of client-side processing. wasm-vips is
// capped at 1 GiB of memory, so high-megapixel images, especially
// Gate very large images out of client-side processing. wasm-vips's
// memory can grow to at most 2 GiB, so high-megapixel images, especially
// interlaced/progressive ones, which can't be decoded with
// shrink-on-load, can exhaust it and fail. These are routed to the
// server, which has no comparable per-image ceiling. If dimensions
Expand Down
3 changes: 3 additions & 0 deletions packages/upload-media/src/store/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ let completedVipsOperations = 0;
/**
* Maximum number of vips operations before recycling the worker.
* Each operation can consume 50-100MB+ of WASM memory for large images.
* The threshold is unchanged now that wasm-vips memory can grow to 2 GiB: a
* grown heap is only handed back when the worker is recycled, so a lower
* ceiling on accumulation still matters.
*/
const MAX_VIPS_OPS_BEFORE_RECYCLE = 50;

Expand Down
23 changes: 17 additions & 6 deletions packages/upload-media/src/test/feature-detection.jsdom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ describe( 'feature-detection', () => {
} );

it( 'applies a tighter budget to interlaced images', () => {
// ~150 MP: over the ~0.5 GiB interlaced budget but under the
// ~0.9 GiB baseline budget.
const dimensions = { width: 15000, height: 10000 };
// ~200 MP: over the ~0.75 GiB interlaced budget but under the
// ~1.5 GiB baseline budget.
const dimensions = { width: 17000, height: 12000 };

expect(
exceedsClientProcessingMemory( {
Expand All @@ -408,12 +408,23 @@ describe( 'feature-detection', () => {
).toBe( false );
} );

it( 'allows a large interlaced image the 1 GiB budgets rejected', () => {
// ~150 MP interlaced exceeded the old ~0.5 GiB budget.
expect(
exceedsClientProcessingMemory( {
width: 15000,
height: 10000,
interlaced: true,
} )
).toBe( false );
} );

it( 'gates extremely large baseline images', () => {
// ~300 MP exceeds even the generous baseline budget.
// ~450 MP exceeds even the generous baseline budget.
expect(
exceedsClientProcessingMemory( {
width: 20000,
height: 15000,
width: 25000,
height: 18000,
interlaced: false,
} )
).toBe( true );
Expand Down
1 change: 1 addition & 0 deletions packages/vips/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Enhancements

- Add an optional `preserveAnimation` option to `resizeImage`. A resize of an animated image then keeps every frame, re-encoded with `gifsave` settings tuned for speed and size (`effort: 2`, `interframe_maxerror: 8`), instead of flattening to the first frame. Cropped resizes cut every frame with the same rectangle. An animation whose decoded frames would not fit the WASM heap flattens too, so the resize degrades rather than failing ([#80385](https://github.com/WordPress/gutenberg/pull/80385)).
- Upgrade `wasm-vips` to 0.0.19, which can grow WASM memory up to 2 GiB, and raise the client-side image memory budgets accordingly ([#84980](https://github.com/WordPress/gutenberg/pull/84980)).

## 4.3.0 (2026-10-07)

Expand Down
2 changes: 1 addition & 1 deletion packages/vips/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"types": "build-types",
"dependencies": {
"@wordpress/worker-threads": "file:../worker-threads",
"wasm-vips": "^0.0.18"
"wasm-vips": "^0.0.19"
},
"devDependencies": {
"@types/node": "^24.13.3",
Expand Down
17 changes: 8 additions & 9 deletions packages/vips/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ const BYTES_PER_PIXEL = 4;
/**
* Memory budget (in bytes) for decoding every frame of an animated image.
*
* wasm-vips runs in a fixed 1 GiB WASM heap that cannot grow (wasm-vips 0.0.18
* builds with `INITIAL_MEMORY` === `MAXIMUM_MEMORY` === 1 GiB), so an
* animation loaded with `[n=-1]` — every frame stacked into one tall image —
* costs at most frames * width * pageHeight * 4 bytes. A long animation of
* otherwise modest dimensions can therefore exhaust the heap even though a
* single frame is small. ~0.5 GiB leaves headroom for the resize and re-encode
* steps.
* An animation loaded with `[n=-1]` (every frame stacked into one tall image)
* costs at most frames * width * pageHeight * 4 bytes, so a long animation of
* otherwise modest dimensions can exhaust the heap even though a single frame
* is small. wasm-vips 0.0.19 grows its heap up to 2 GiB, but growth can fail
* on memory-constrained devices, so ~0.75 GiB leaves headroom for that and
* for the resize and re-encode steps.
*
* The estimate is the fully materialized frame stack, which is an upper bound:
* libvips streams the sequential load/resize/save pipeline, so real peak usage
Expand All @@ -55,7 +54,7 @@ const BYTES_PER_PIXEL = 4;
* that would have fit against never failing an upload outright. See
* `logAnimationFallback()` for how that decision is surfaced.
*/
const ANIMATION_MEMORY_BUDGET = 0.5 * 1024 * 1024 * 1024;
const ANIMATION_MEMORY_BUDGET = 0.75 * 1024 * 1024 * 1024;

/**
* Warns that an animation was flattened even though it was asked to keep it.
Expand Down Expand Up @@ -84,7 +83,7 @@ function logAnimationFallback(
estimatedBytes
) } MiB, over the ${ mib(
ANIMATION_MEMORY_BUDGET
) } MiB budget for the 1 GiB WASM heap.`
) } MiB budget for the WASM heap.`
);
}

Expand Down
42 changes: 36 additions & 6 deletions packages/vips/src/test/resize-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,36 @@ describe( 'resizeImage', () => {
);
} );

it( 'keeps an animation the 1 GiB heap budget flattened', async () => {
const gifFile = new File( [ '<BLOB>' ], 'example.gif', {
lastModified: 1234567891,
type: 'image/gif',
} );
const buffer = await gifFile.arrayBuffer();

// 150 frames of 1200x900 need ~650 MB, over the old ~0.5 GiB budget.
mockState.width = 1200;
mockState.pageHeight = 900;
mockState.height = 900 * 150;

await resizeImage(
'itemId',
buffer,
'image/gif',
{
width: 100,
height: 100,
},
{ preserveAnimation: true }
);

expect( mockThumbnailBuffer ).toHaveBeenCalledWith( buffer, 100, {
height: 100,
size: 'down',
option_string: '[n=-1]',
} );
} );

it( 'falls back to the first frame when the animation exceeds the memory budget', async () => {
const gifFile = new File( [ '<BLOB>' ], 'example.gif', {
lastModified: 1234567891,
Expand All @@ -494,14 +524,14 @@ describe( 'resizeImage', () => {
const buffer = await gifFile.arrayBuffer();

/*
* A 1200x900 GIF is only ~4 MB decoded as a single frame, but 150
* frames of it need ~650 MB, which does not fit the 1 GiB WASM
* heap. Such an animation must flatten rather than abort the
* upload.
* A 1200x900 GIF is only ~4 MB decoded as a single frame, but 250
* frames of it need ~1.1 GB, which does not fit the animation
* memory budget. Such an animation must flatten rather than abort
* the upload.
*/
mockState.width = 1200;
mockState.pageHeight = 900;
mockState.height = 900 * 150;
mockState.height = 900 * 250;

await resizeImage(
'itemId',
Expand Down Expand Up @@ -530,7 +560,7 @@ describe( 'resizeImage', () => {
* budget it was measured against.
*/
expect( console ).toHaveWarnedWith(
expect.stringContaining( '150 frames' )
expect.stringContaining( '250 frames' )
);
} );

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"esbuild": "^0.27.2",
"filenamify": "^4.2.0",
"playwright": "^1.63.0",
"wasm-vips": "^0.0.18",
"wasm-vips": "^0.0.19",
"ws": "^8.18.3",
"y-websocket": "^3.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion test/performance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@types/node": "^24.13.3",
"@wordpress/e2e-test-utils-playwright": "file:../../packages/e2e-test-utils-playwright",
"@wordpress/scripts": "file:../../packages/scripts",
"wasm-vips": "^0.0.18"
"wasm-vips": "^0.0.19"
},
"publishConfig": {
"access": "public"
Expand Down
Loading