Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance issue with Mutate in WASM #1695

Closed
4 tasks done
TomatorCZ opened this issue Jul 13, 2021 · 7 comments
Closed
4 tasks done

Performance issue with Mutate in WASM #1695

TomatorCZ opened this issue Jul 13, 2021 · 7 comments
Labels

Comments

@TomatorCZ
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

I use Blazor WebAssembly for that. I create a new blank image (1920x1080) and change the background color with Mutate, but Mutate took about 32 seconds in Debug and 23 seconds in Release mode.

Steps to Reproduce

Create a new Blazor WebAssembly project
Add the code below to a Razor page.

{
	protected override void OnInitialized()
	{
		base.OnInitialized();
		System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
		stopWatch.Start();
		var a = new SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32>(new Configuration(), 1920, 1080);
		stopWatch.Stop();
		Console.WriteLine(stopWatch.ElapsedMilliseconds);
		stopWatch.Restart();
		a.Mutate(o => o.BackgroundColor(Color.Black));
		stopWatch.Stop();
		Console.WriteLine(stopWatch.ElapsedMilliseconds);
	}

}

Open the browser console where the results are displayed.

System Configuration

  • ImageSharp version: 1.0.3
  • Other ImageSharp packages and versions:
  • Environment (Operating system, version and so on): Google Chrome 91.0.4472.124, Windows 10
  • .NET Framework version: net5.0
  • Additional information: I found a related issue
@br3aker
Copy link
Contributor

br3aker commented Jul 13, 2021

While this doesn't fix this particular issue, have you tried Image ctor from given color as a workaround?

var img = new Image<Rgba32>(1920, 1080, new Rgba32(0, 0, 0, 255));

@JimBobSquarePants
Copy link
Member

Yeah no, this should have been a discussion not an issue. WASM doesn't have SIMD yet.

@tocsoft
Copy link
Member

tocsoft commented Jul 13, 2021

FYI @TomatorCZ .BackgroundColor() is for converting transparent backgrounds of existing images, however what it looks like from your example is you would be better of using any of theses other APIs:

  1. The constructor overload that takes a Color,
  2. .Clear(color)

The reason BackgroundColor() will likely be slower is because it has to process each pixel independently rather that just flood filling the images pixel memory with a single color value which is what the other 2 do.

@antonfirsov
Copy link
Member

antonfirsov commented Jul 13, 2021

@TomatorCZ afaik WASM is interpreted in .NET 5.0 blazor by default, thus uncapable to run any computation-intensive managed code efficiently, including image processing. This should improve for next version with AOT (see dotnet/runtime#44316), but I think it will be a very long way until performance will be acceptable. We recommend to move your image processing code to a server-side component.

@h3x4d3c1m4l
Copy link

@TomatorCZ You might want to try switching to .NET 6. The AoT compilation to native WASM already works very well...

Instructions can be found here. Although these instructions are written for .NET 6 preview 4 I found these exact instructions to be still working as of .NET 6 preview 6.

@stephajn
Copy link

I'm sorry to give an older issue (and a closed one at that) a bump, but I'm just curious if as of the release of .NET 6, if ImageSharp is now a viable option to use in browser with Blazor WASM, or if this kind of work should still ideally be done at the server. Or, is it now a case of using AOT in release builds to get around any performance issues?

@JimBobSquarePants
Copy link
Member

I know MS did a lot of work improving performance for .NET 6 and even demoed those improvements using ImageSharp. However I haven't performed any benchmarking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants