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

Super slow open+resize+save on big Jpeg with Mono #94

Closed
nah0y opened this issue Jan 27, 2017 · 15 comments
Closed

Super slow open+resize+save on big Jpeg with Mono #94

nah0y opened this issue Jan 27, 2017 · 15 comments

Comments

@nah0y
Copy link

nah0y commented Jan 27, 2017

Description

I'm using Mono and Xamarin Studio for that. I'm simply trying to resize a big picture (5184x3456 - 8.2MB) and it takes 12sc to complete.

Steps to Reproduce

I created a simple console project, added Nuget dependencies to ImageSharp. And I'm using this in the Program.cs:

string filePath = ""; // Path to my image

Stopwatch sw1 = new Stopwatch();
sw1.Start();
string tmpFilePath = Path.GetTempPath() + "x-imagesharp" + Path.GetExtension(filePath);
using (FileStream stream = File.OpenRead(filePath))
using (FileStream output = File.OpenWrite(tmpFilePath))
{
	Image image = new Image(stream);
	image.Resize(200, 300).Save(output);
}
sw1.Stop();
Console.WriteLine("Image sharp: " + sw1.ElapsedMilliseconds);

System Configuration

  • ImageSharp version: 1.0.0-alpha1-00073
  • Other ImageSharp packages and versions: 1.0.0-alpha1-00073 && 1.0.0-alpha1-00053
  • Environment (Operating system, version and so on): OSX 10.11.6
  • .NET Framework version: Mono / .NET 4.5
  • Additional information: Link to the picture (it's the interior of a mouth, so if you don't want to click on it, no problem. Nothing gore or anything though :D): http://i.imgur.com/TSdnmgr.jpg
@JimBobSquarePants
Copy link
Member

JimBobSquarePants commented Jan 27, 2017

Hmmm... Something amiss here. 946ms on my laptop.

Could you do me a favor and tell me what the results of Vector.IsHardwareAccelerated is on your machine? I'm assuming false. Are you compiling in 32 or 64bit mode?

Mono supports NET4.6 as I recall, would it be possible to bump to that and get back to us?

I'll double check we're not doing something daft and releasing our code in debug mode also.

@JimBobSquarePants
Copy link
Member

@dlemstra
Copy link
Member

dlemstra commented Jan 27, 2017

I wonder which part is slow, what is the output of the following code.

Stopwatch sw1 = new Stopwatch();
sw1.Start();
string tmpFilePath = Path.GetTempPath() + "x-imagesharp" + Path.GetExtension(filePath);
using (FileStream stream = File.OpenRead(filePath))
using (FileStream output = File.OpenWrite(tmpFilePath))
{
	Image image = new Image(stream);
	sw1.Stop();
	Console.WriteLine("Image sharp read: " + sw1.ElapsedMilliseconds);
	sw1.Restart();

	image.Resize(200, 300);
	sw1.Stop();
	Console.WriteLine("Image sharp resize: " + sw1.ElapsedMilliseconds);
	sw1.Restart();

	image.Save(output);
	sw1.Stop();
	Console.WriteLine("Image sharp save: " + sw1.ElapsedMilliseconds);
}

@antonfirsov
Copy link
Member

antonfirsov commented Jan 28, 2017

@nah0y I'm afraid, there is no hardware SIMD acceleration for System.Numerics.Vectors types in your runtime. After a quick (non-exhausting) googling it seems Mono added this feature only a month ago.

What is the value of System.Numerics.Vector.IsHardwareAccelerated in release mode?

If it's false, the only way to speed up ImageSharp jpeg encoder/decoder is to upgrade to the latest mono framework/runtime. (You probably need to build from sources.)

@antonfirsov antonfirsov changed the title Super slow resize on big image Super slow open+resize+save on big image with Mono Jan 28, 2017
@antonfirsov
Copy link
Member

antonfirsov commented Jan 28, 2017

Renamed the issue to hold more information about the context.

@antonfirsov antonfirsov changed the title Super slow open+resize+save on big image with Mono Super slow open+resize+save on big Jpeg with Mono Jan 28, 2017
@nah0y
Copy link
Author

nah0y commented Jan 28, 2017

Hey guys! Sorry for the wait, I'm back with more info :)

I tried on my new Macbook Pro and same result. 9762ms.
I'm targetting x86.
Vector.IsHardwareAccelerated returns false in release mode
Using .NET 4.5.1: 9686ms.
I don't have .Net 4.6 on this machine for now sorry, I'll have to install it, but not today :/

I don't understand why you wrote this :/

Definitely in release mode. https://github.com/JimBobSquarePants/ImageSharp/blob/f14dd6f91adcda9569bbf49fdff6c613ac2948bd/build/Program.cs#L106

For info, in the output console I have around 12 threads that are started one after the other:

Thread started: #2
Thread started: #3
Thread started: #4
Thread started: #5
Thread started: #6
Thread started: #7
Thread started: #8
Thread started: #9
Thread started: #10

And from what I understand it's because of Vector.IsHardwareAccelerated is false and I need the latest mono runtime (thanks @antonfirsov for clarifying that). If I don't want to build from sources and wait for an update, it's an update of Mono right? Like currently they're at 4.6, so I'll have to wait for 4.7. I'll try to give it a try when it's available if I don't forget 😄

@nah0y
Copy link
Author

nah0y commented Jan 28, 2017

And @dlemstra, here's the result:

Image sharp read: 5360
Image sharp resize: 4393
Image sharp save: 34

@antonfirsov
Copy link
Member

@nah0y yeah you need a new mono version, 4.8 should be the next one, but I see no info about System.Numerics.Vectors in their roadmap. Maybe you should ask on their forums.

Thanks for sharing your results. Looking at resize performance is on my list. Strange it's that slow on Mono.

@nah0y
Copy link
Author

nah0y commented Jan 29, 2017

Yes strange ;)
Thanks a lot to all of you for your time!

@antonfirsov
Copy link
Member

@nah0y I don't know if you are still interested, but recently I figured out that SIMD support will be shipped with mono 5.0.

We are also working on perf. improvements meanwhile.

@nah0y
Copy link
Author

nah0y commented Mar 19, 2017

@antonfirsov Hey! Thanks for the news. I finally did it another way. But I'm still interested to see how it evolves!

@clegendre
Copy link

Hello,

I'm experiencing the same issue on Xamarin Andoid version 7.3 (based on Mono 5).
The Image.Load and Image.Save methods are taking so much times on large images (4000x3000 px).
On a LG G3 mobile running Android 5.0, it takes approximatively 1 minute to Load / Manipulate Exifs / Save in a MemoryStream.
@antonfirsov I'm interested on how you did it another way. Which library did you used ?

ImageSharp version: 1.0.0-alpha9-00054

@antonfirsov
Copy link
Member

antonfirsov commented May 18, 2017

@clegendre Unfortunately I'm unfamiliar with mobile development and the details of the Xamarin architecture. 4k*3k means lots of pixels, what are the expected times for that operation (using other libraries/applications) on your mobile device?

Also: What is the value of System.Numerics.Vector.IsHardwareAccelerated on your device? Are you sure you tested it in release mode?

It seems, that it's not necessary to decode the image pixels in your use case, only the exif data + resave the modified exif with the original pixel data. @JimBobSquarePants shouldn't we add support for this?

@JimBobSquarePants
Copy link
Member

@antonfirsov I believe a standalone metadata editor would be the best tool for that job of which there are several out in the wild. Could we build one? Yeah certainly, Probably pretty easy with the existing tools we have now but trying to configure the existing decoders to only parse metadata while ignoring the pixels seems like it would add a lot of complexity and overhead to our code.

@clegendre Could you do me a favor and check the value Anton asked for and also post a code sample + device specifications. 1 min is slow... crazy slow even without SIMD. I don't see how we are doing enough work to even cause that much time to be consumed.

@clegendre
Copy link

System.Numerics.Vector.IsHardwareAccelerated is set to true.
Tested in Debug and Release mode.

We've also tested on a new Lenovo smartphone under Android 7: images taken with the camera are slightly bigger, but even with a better proc, loading then saving in memory took approximatively 20-30 seconds.

@antonfirsov We managed to do it another way, by setting the Exifs directly on the Camera before the image is taken (this is indeed the right way to do that). Thumbnail is done using the native Android API.

@JimBobSquarePants I'll try to post a repro sample this weekend.

BTW, I love your library and the API design is fantastic, you're making a great job!

@SixLabors SixLabors deleted a comment from AuriR Sep 17, 2024
@SixLabors SixLabors locked and limited conversation to collaborators Sep 17, 2024
@SixLabors SixLabors locked and limited conversation to collaborators Sep 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants