Repository navigation
fix(wasm): Release image decoder memory after each decode (backport #24848) - #24911
Conversation
|
Cherry-pick of 11ae190 has failed: To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally |
898f386 to
abbdd0a
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24911/wasm-skia-net9/index.html |
|
|
abbdd0a to
114b9dd
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24911/wasm-skia-net9/index.html |
|
This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again. |
|
@Mergifyio backport release/stable/6.7 |
✅ Backports have been createdDetails
|
GitHub Issue: related to unoplatform/kahua-private#547
PR Type:
🐞 Bugfix
What changed? 🚀
On Skia WebAssembly, the browser image decoder keeps every worker's last image in memory for the rest of the session.
ImageLoader.tsruns one dedicated worker pernavigator.hardwareConcurrency(8 on an iPad), each with oneOffscreenCanvas. A decode resized the canvas to the image and never shrank it, and never closed theImageBitmap, which was freed only when the worker's own GC ran. On the C# side, theJSObjectreturned byLoadFromArraywas never disposed, so the response, including the w×h×4 pixel buffer transferred from the worker, stayed alive until the wrapper's finalizer ran. The managed GC isn't prompted by that memory, so it can be a long wait.This reduces the browser process footprint, which is what iOS memory limits count. It doesn't change the growth of WebAssembly linear memory (
HEAPU8.length): the heap peak was the same in every measured build.ImageLoader.ts): aftergetImageData, the bitmap is closed and the canvas is shrunk back to 0×0 in afinally, so errors release them too. Width and height now come from theImageData, because a closed bitmap reports 0×0.ImageSourceHelpers: theJSObjectis disposed withusing.ImageSourceHelpers, master only: the RGBA→BGRA swap now happens in place on the managed copy, instead of allocating a second full-size array per decode.CopyPixelscopies the pixels into native memory either way.There are no public API changes. The response returned to C# (
{error, bytes, width, height}) is unchanged.Validation
Memory was sampled from outside the browser with macOS
footprint, at fixed phases of each run.Safari 26.6 (desktop), worker only: a script-only page runs each build's worker code without .NET: 8 workers, 3 rounds of 2048² PNGs.
Chrome, full SamplesApp: a probe test decodes 8 images concurrently per round, 3 rounds of 2048².
JSObjectaccounts for about 30 MiB.Decode time (Chrome, worker only, same-size decodes on one worker): 256² took 1.5–2.4 ms per decode before and 1.5–1.6 ms after; 1024² about 21 ms in both. The Safari timings varied up to 6× between runs of the same build and are not reported.
Runtime tests (Skia WebAssembly, headless Chrome, Release): all of
Given_ImageandGiven_BitmapSourcepass: 44 passed, 0 failed. Three tests are skipped by[Ignore], andWhen_Transitive_Asset_With_Link_Loadedis inconclusive in the browser, as it is on master.New test:
Given_BitmapSource.When_Browser_Workers_Are_Reusedcalls the worker API directly. ThroughImage, a broken worker can't be seen: a failed browser decode falls back to SKCodec and the image still opens. With a worker that throws instead of returning pixels, this test fails whileWhen_Alpha_Blending,When_Exif_Rotated_From_StreamandWhen_Exif_Rotated_MsAppxstill pass.The test passes without the fix too. It guards the reset and error paths; the memory effect is shown by the measurements above. The expected pixels were also checked on Safari 26.6, which returns the same values as Chrome.
Build:
Uno.UI,Uno.UI.Runtime.Skia.WebAssembly.BrowserandUno.UI.RuntimeTests.Skiacompile fornet10.0with analyzers, code-style enforcement and warnings as errors.net11.0was not built locally.Not validated:
Backports
ImageLoader.tsis identical onservicing/6.8andrelease/stable/6.7, and the worker change and the test apply there unchanged. The in-place swap commit is master only: 6.x wraps the pinned managed array in anSKImagewithout copying it. When backporting, drop that commit and keep only theusinginImageSourceHelpers.cs. That file is the one conflict a dry-run merge predicts on both branches.Not in this PR
errorormessageerrorhandler and no timeout. A worker that crashes leaves its decode waiting forever and drops out of the pool. This already happens on master.hardwareConcurrencyworkers at startup, with no cap and no idle shutdown. Several decodes at once still peak at roughly workers × (bitmap + canvas + pixels).PR Checklist ✅
Screenshots Compare Test Runresults.🤖 Generated with Claude Code
This is an automatic backport of pull request #24848 done by [Mergify](https://mergify.com).