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

Simplify jpeg encoder and fix for Windows ARM #1304

Merged
merged 13 commits into from
Aug 7, 2020

Conversation

JimBobSquarePants
Copy link
Member

@JimBobSquarePants JimBobSquarePants commented Aug 4, 2020

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following matches the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Fixes #1275

  • Use safe cast to span where possible
  • Simplify pointer code to use known safe paths used during decoding.
  • Limit Unsafe API usage
  • Use simplified code to debug issues
  • Prove it works!

@codecov
Copy link

codecov bot commented Aug 4, 2020

Codecov Report

Merging #1304 into master will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1304      +/-   ##
==========================================
- Coverage   82.77%   82.76%   -0.01%     
==========================================
  Files         689      689              
  Lines       30733    30721      -12     
  Branches     3473     3473              
==========================================
- Hits        25439    25427      -12     
  Misses       4587     4587              
  Partials      707      707              
Flag Coverage Δ
#unittests 82.76% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...rc/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs 86.97% <100.00%> (-0.17%) ⬇️
...ormats/Jpeg/Components/Encoder/RgbToYCbCrTables.cs 100.00% <100.00%> (ø)
...omponents/Encoder/YCbCrForwardConverter{TPixel}.cs 100.00% <100.00%> (ø)
...geSharp/Formats/Jpeg/Components/GenericBlock8x8.cs 87.87% <100.00%> (+0.37%) ⬆️
src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs 94.61% <100.00%> (-0.04%) ⬇️
...geSharp/PixelFormats/PixelImplementations/Rgb24.cs 96.10% <100.00%> (+0.05%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a31a541...0c524d6. Read the comment docs.

@JimBobSquarePants JimBobSquarePants changed the title WIP Simplify jpeg encoder and fix for Windows ARM Simplify jpeg encoder and fix for Windows ARM Aug 6, 2020
@JimBobSquarePants JimBobSquarePants marked this pull request as ready for review August 6, 2020 20:50
@JimBobSquarePants JimBobSquarePants requested a review from a team August 6, 2020 20:50
Copy link
Member

@antonfirsov antonfirsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do a before/after benchmark comparison for EncodeJpeg, with an image different than Car.bmp?

I suggest re-encoding jpeg420exif.jpg, but config can be Config.ShortCore31 to get it done faster.

Comment on lines +206 to +214
#if NETSTANDARD2_0
// See https://github.com/SixLabors/ImageSharp/issues/1275
this.R = source.R;
this.G = source.G;
this.B = source.B;
#else
this = source.Rgb;
#endif
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! 🎉

Remember someone talking about opening a dotnet/runtime issue on twitter, if it exists we should provide this info.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't open one. (I always struggle to provide a cut down solution to help them). Defo wise though.

@JimBobSquarePants
Copy link
Member Author

Can we do a before/after benchmark comparison for EncodeJpeg, with an image different than Car.bmp?

I suggest re-encoding jpeg420exif.jpg, but config can be Config.ShortCore31 to get it done faster.

@antonfirsov I'm not all that fussed if we lose a percentile or two here. We need to create a hardware accelerated version of our transforms anyway.

@antonfirsov
Copy link
Member

if we lose a percentile or two here

I'm afraid it might be more, that's why I suggest an extensive benchmark run. Concerned about Block8x8F indexer performance, since it's used very frequently now, and it's code doesn't seem optimal to me.

@JimBobSquarePants
Copy link
Member Author

JimBobSquarePants commented Aug 6, 2020

I'm afraid it might be more, that's why I suggest an extensive benchmark run. Concerned about Block8x8F indexer performance, since it's used very frequently now, and it's code doesn't seem optimal to me.

We use it in a few places in the decoder, hence why I chose to reuse it as a known path.

Anyway.... Here's the before and after results with Jpeg420Exif_MidSizeYCbCr...

Before

Method Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
'System.Drawing Jpeg' 4.498 ms 2.060 ms 0.1129 ms 1.00 0.00 54.6875 31.2500 31.2500 2.22 MB
'ImageSharp Jpeg' 70.435 ms 19.521 ms 1.0700 ms 15.66 0.22 - - - 2.96 MB

After

Method Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
'System.Drawing Jpeg' 4.495 ms 0.6598 ms 0.0362 ms 1.00 0.00 54.6875 31.2500 31.2500 2.22 MB
'ImageSharp Jpeg' 70.219 ms 16.7004 ms 0.9154 ms 15.62 0.19 - - - 2.96 MB

I'd take the massive difference in speed between us and System.Drawing in this benchmark with a pinch of salt. They're not subsampling which will be a huge saving.

Actually I'm wrong. 4:4:4 was slower just then. Will have to profile to determine the difference.

@JimBobSquarePants JimBobSquarePants requested a review from a team August 7, 2020 10:54
Copy link
Member

@antonfirsov antonfirsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The numbers look fine. I guess that's 420, hope it doesn't regress 444 either.

@JimBobSquarePants JimBobSquarePants merged commit 64e7d6a into master Aug 7, 2020
@JimBobSquarePants JimBobSquarePants deleted the js/simplify-jpeg-encoder branch August 7, 2020 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JpegEncoder producing incorrect output on windows-arm (was working on beta0005)
2 participants