Skip to content

fix(wasm): Release image decoder memory after each decode (backport #24848) - #24911

Merged
ajpinedam merged 4 commits into
servicing/6.8from
mergify/bp/servicing/6.8/pr-24848
Oct 2, 2026
Merged

ajpinedam merged 4 commits into
servicing/6.8from
mergify/bp/servicing/6.8/pr-24848

Conversation

@mergify

@mergify mergify Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

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.ts runs one dedicated worker per navigator.hardwareConcurrency (8 on an iPad), each with one OffscreenCanvas. A decode resized the canvas to the image and never shrank it, and never closed the ImageBitmap, which was freed only when the worker's own GC ran. On the C# side, the JSObject returned by LoadFromArray was 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.

  • Worker (ImageLoader.ts): after getImageData, the bitmap is closed and the canvas is shrunk back to 0×0 in a finally, so errors release them too. Width and height now come from the ImageData, because a closed bitmap reports 0×0.
  • ImageSourceHelpers: the JSObject is disposed with using.
  • 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. CopyPixels copies 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.

    • Canvas memory: 128 MiB, never released, before the fix; 0 after.
    • Page process 30 s after the last decode: 333 MiB before, 94–110 MiB after (baseline 123 MiB).
    • Decoded bitmaps at their peak: +333 MiB before, +124 MiB after.
  • Chrome, full SamplesApp: a probe test decodes 8 images concurrently per round, 3 rounds of 2048².

    • Page process 5 s after the last decode: 1,286–1,414 MiB before, 1,012–1,013 MiB after.
    • Browser-side allocations (PartitionAlloc): 333–350 MiB before, 61–63 MiB after (baseline 59 MiB). With only the worker change, 92 MiB, so disposing the JSObject accounts for about 30 MiB.
    • Full GCs for the same decodes: 53–58 before, 42–46 after, from the in-place swap.
  • 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_Image and Given_BitmapSource pass: 44 passed, 0 failed. Three tests are skipped by [Ignore], and When_Transitive_Asset_With_Link_Loaded is inconclusive in the browser, as it is on master.

  • New test: Given_BitmapSource.When_Browser_Workers_Are_Reused calls the worker API directly. Through Image, 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 while When_Alpha_Blending, When_Exif_Rotated_From_Stream and When_Exif_Rotated_MsAppx still pass.

    • It decodes one more image than there are workers, so at least one worker decodes again after its reset.
    • It then decodes invalid bytes followed by a valid image.
    • It checks dimensions, byte count and the premultiplied first pixel.

    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.Browser and Uno.UI.RuntimeTests.Skia compile for net10.0 with analyzers, code-style enforcement and warnings as errors. net11.0 was not built locally.

  • Not validated:

    • On an iPad.
    • The SamplesApp runtime tests in Safari: a covered Safari window throttles timers, which stalled the runs.

Backports

ImageLoader.ts is identical on servicing/6.8 and release/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 an SKImage without copying it. When backporting, drop that commit and keep only the using in ImageSourceHelpers.cs. That file is the one conflict a dry-run merge predicts on both branches.

Not in this PR

  • The worker pool has no error or messageerror handler and no timeout. A worker that crashes leaves its decode waiting forever and drops out of the pool. This already happens on master.
  • The pool creates hardwareConcurrency workers at startup, with no cap and no idle shutdown. Several decodes at once still peak at roughly workers × (bitmap + canvas + pixels).
  • On master, the R/B swap could move into the worker's premultiply loop, and the managed copy could be skipped by copying the JS buffer straight into native memory.

PR Checklist ✅

🤖 Generated with Claude Code


This is an automatic backport of pull request #24848 done by [Mergify](https://mergify.com).

@mergify mergify Bot added the conflicts Automated merge hit conflicts label Oct 2, 2026
@mergify

mergify Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Cherry-pick of 11ae190 has failed:

On branch mergify/bp/servicing/6.8/pr-24848
Your branch is ahead of 'origin/servicing/6.8' by 2 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 11ae190.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/Uno.UI/UI/Xaml/Media/Imaging/ImageSourceHelpers.cs

no changes added to commit (use "git add" and/or "git commit -a")

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

@github-actions github-actions Bot added the platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform label Oct 2, 2026
@ajpinedam
ajpinedam enabled auto-merge October 2, 2026 05:09
@ajpinedam
ajpinedam force-pushed the mergify/bp/servicing/6.8/pr-24848 branch from 898f386 to abbdd0a Compare October 2, 2026 05:15
@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 236874 has failed on Uno.UI - CI.

@ajpinedam
ajpinedam force-pushed the mergify/bp/servicing/6.8/pr-24848 branch from abbdd0a to 114b9dd Compare October 2, 2026 16:55
@unodevops

Copy link
Copy Markdown
Contributor

@ajpinedam
ajpinedam merged commit 7bd01e7 into servicing/6.8 Oct 2, 2026
41 of 42 checks passed
@ajpinedam
ajpinedam deleted the mergify/bp/servicing/6.8/pr-24848 branch October 2, 2026 19:27
@mergify

mergify Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

@agneszitte

Copy link
Copy Markdown
Member

@Mergifyio backport release/stable/6.7

@mergify

mergify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

backport release/stable/6.7

✅ Backports have been created

Details

agneszitte added a commit that referenced this pull request Oct 28, 2026
…6.7/pr-24911

fix(wasm): Release image decoder memory after each decode (backport #24848) (backport #24911)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automated merge hit conflicts platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants