Skip to content

Speed up PNG decode and encode - #3285

Merged
JimBobSquarePants merged 3 commits into
mainfrom
js/perf-png
Nov 25, 2026
Merged

JimBobSquarePants merged 3 commits into
mainfrom
js/perf-png

Conversation

@JimBobSquarePants

@JimBobSquarePants JimBobSquarePants commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

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 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

This pull request makes PNG decode and encode faster.

  • One generic filter decoder and one generic filter encoder replace the separate filter classes.
  • The adaptive filter computes the sums of all four filters in one pass. It writes only the chosen filter.
  • 8-bit and 16-bit rows convert in bulk through PixelOperations.
  • The IDAT CRC check reads in blocks and does not allocate a buffer for each chunk.
  • BMP and TIFF do not check the pixel type. They convert through PixelOperations.

Benchmarks

Summary:

  • Decode is faster in every layout.
  • Decode allocates about 8 KB. main allocates up to 34 KB.
  • Encode takes the same time as main.
  • Encode allocates less than 10 KB. main allocates up to 346 KB.
  • Paeth decode of 8-bit RGB takes 30.25 ms. main takes 53.75 ms.
  • The other filtered decodes are close to main.

main and this branch ran in the same session on the same machine, with the benchmark Config.Short settings (3 iterations). DecodePng and EncodePng use no filter.

BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.10075)
AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores
.NET 10.0.15, X64 RyuJIT x86-64-v4

DecodePng

Layout Build Mean Error StdDev Allocated
Gray1 main 3.837 ms 1.3157 ms 0.0721 ms 8.06 KB
Gray1 branch 1,554.6 us 291.5 us 15.98 us 7620 B
Gray4 main 6.082 ms 1.0743 ms 0.0589 ms 9.63 KB
Gray4 branch 3,695.6 us 208.7 us 11.44 us 7623 B
Gray8 main 9.389 ms 0.7675 ms 0.0421 ms 13.9 KB
Gray8 branch 7,355.1 us 2,255.0 us 123.60 us 7486 B
Gray16 main 16.935 ms 1.9785 ms 0.1084 ms 22.57 KB
Gray16 branch 14,554.4 us 2,035.3 us 111.56 us 7645 B
GrayAlpha8 main 12.785 ms 0.3943 ms 0.0216 ms 14.35 KB
GrayAlpha8 branch 11,584.8 us 4,056.7 us 222.36 us 7501 B
GrayAlpha16 main 30.889 ms 2.7231 ms 0.1493 ms 24.18 KB
GrayAlpha16 branch 27,267.2 us 2,250.3 us 123.34 us 7673 B
Palette1 main 7.881 ms 0.7036 ms 0.0386 ms 8.23 KB
Palette1 branch 1,803.6 us 524.6 us 28.75 us 7927 B
Palette4 main 10.827 ms 7.9737 ms 0.4371 ms 9.69 KB
Palette4 branch 4,250.1 us 1,303.8 us 71.46 us 8422 B
Palette8 main 13.176 ms 1.5028 ms 0.0824 ms 20.14 KB
Palette8 branch 8,244.0 us 9,442.9 us 517.60 us 16837 B
Rgb8 main 21.340 ms 21.9809 ms 1.2048 ms 22.44 KB
Rgb8 branch 20,334.2 us 4,279.8 us 234.59 us 7529 B
Rgb16 main 41.908 ms 4.3388 ms 0.2378 ms 27.51 KB
Rgb16 branch 37,169.2 us 2,912.4 us 159.64 us 7757 B
Rgba8 main 26.175 ms 15.1486 ms 0.8303 ms 26.71 KB
Rgba8 branch 25,601.1 us 1,158.2 us 63.49 us 7529 B
Rgba16 main 49.419 ms 25.2195 ms 1.3824 ms 28.45 KB
Rgba16 branch 41,535.6 us 2,941.7 us 161.24 us 7757 B
Rgba8Adam7 main 33.148 ms 6.9604 ms 0.3815 ms 33.52 KB
Rgba8Adam7 branch 27,350.2 us 1,377.0 us 75.48 us 7673 B

EncodePng

Layout Build Mean Error StdDev Allocated
Gray16 main 107.3 ms 8.01 ms 0.44 ms 240.75 KB
Gray16 branch 102.3 ms 11.15 ms 0.61 ms 9.39 KB
GrayAlpha16 main 162.5 ms 15.90 ms 0.87 ms 247.55 KB
GrayAlpha16 branch 158.3 ms 2.92 ms 0.16 ms 9.39 KB
Rgb8 main 141.7 ms 20.10 ms 1.10 ms 27.54 KB
Rgb8 branch 141.8 ms 35.75 ms 1.96 ms 9.39 KB
Rgb16 main 248.2 ms 32.56 ms 1.78 ms 255.04 KB
Rgb16 branch 242.7 ms 5.84 ms 0.32 ms 9.55 KB
Rgba8 main 181.4 ms 4.44 ms 0.24 ms 39.04 KB
Rgba8 branch 183.2 ms 23.52 ms 1.29 ms 9.55 KB
Rgba16 main 360.5 ms 54.98 ms 3.01 ms 345.9 KB
Rgba16 branch 355.3 ms 26.66 ms 1.46 ms 9.66 KB

DecodeFilteredPng

The photo is encoded with one filter for all rows, as 8-bit RGB and 8-bit RGBA.

Filter Layout Build Mean Error StdDev Allocated
None Rgb8 main 20.81 ms 12.850 ms 0.704 ms 25.94 KB
None Rgb8 branch 19.82 ms 1.173 ms 0.064 ms 8.17 KB
None Rgba8 main 26.00 ms 2.635 ms 0.144 ms 26.71 KB
None Rgba8 branch 25.52 ms 0.462 ms 0.025 ms 8.68 KB
Sub Rgb8 main 25.46 ms 7.653 ms 0.419 ms 22 KB
Sub Rgb8 branch 22.14 ms 4.835 ms 0.265 ms 8.17 KB
Sub Rgba8 main 25.38 ms 8.336 ms 0.457 ms 22.21 KB
Sub Rgba8 branch 25.03 ms 0.503 ms 0.028 ms 8.68 KB
Up Rgb8 main 18.47 ms 1.527 ms 0.084 ms 21.58 KB
Up Rgb8 branch 18.48 ms 1.017 ms 0.056 ms 8.17 KB
Up Rgba8 main 22.56 ms 1.872 ms 0.103 ms 23.07 KB
Up Rgba8 branch 23.40 ms 18.699 ms 1.025 ms 8.68 KB
Average Rgb8 main 24.72 ms 2.218 ms 0.122 ms 20.94 KB
Average Rgb8 branch 23.61 ms 1.739 ms 0.095 ms 8.17 KB
Average Rgba8 main 27.01 ms 1.834 ms 0.101 ms 21.93 KB
Average Rgba8 branch 27.39 ms 1.132 ms 0.062 ms 8.68 KB
Paeth Rgb8 main 53.75 ms 4.106 ms 0.225 ms 28.24 KB
Paeth Rgb8 branch 30.25 ms 0.391 ms 0.021 ms 8.17 KB
Paeth Rgba8 main 34.18 ms 2.586 ms 0.142 ms 25.62 KB
Paeth Rgba8 branch 34.17 ms 1.428 ms 0.078 ms 10.24 KB

PNG decode:
- One generic filter decoder for each filter. It uses pixel operators for
  pixels of 1, 2, 3, 4, 6 and 8 bytes. The separate filter classes are gone.
- A scanline processor for each frame. Palette rows and gray rows of fewer
  than 8 bits use a pixel table and the shared PaletteExpander.
- 8-bit and 16-bit rows convert in bulk through PixelOperations.
- Adam7 pass 7 converts straight into the image row.
- The CRC check of IDAT and fdAT chunks reads the data in blocks, with no
  buffer for each chunk.

PNG encode:
- 16-bit rows convert into the scanline buffer, then change to big-endian in
  place.
- The adaptive filter computes the sums of all four filters in one pass, and
  writes only the chosen filter.

Pixel operations:
- Rgba32CompatiblePixelOperations<TPixel, TOrder> converts luminance and
  16-bit layouts to each 8-bit RGB and RGBA layout with SIMD kernels.
- Rgba64CompatiblePixelOperations<TPixel> converts each layout to Rgba64
  and Rgb48.
- Each source type's To method calls the destination's From method, so each
  conversion has one implementation.

BMP and TIFF:
- No decoder checks the pixel type. BMP bit fields write Rgba32 rows, and
  5-6-5 masks use the bulk Bgr565 conversion. The TIFF 32-bit integer
  decoders convert through PixelOperations<Rgb96> and
  PixelOperations<Rgba128>.

SimdUtils:
- Remove FastRound, PseudoRound and HasVector8. Vector.Round replaces
  FastRound.
- Generic Pad3 and Slice3 with the Pad3x8Layout and Pad3x16Layout masks.
- Remove the obsolete Block8x8F benchmarks.
- DecodePng decodes one photo in each PNG color type and bit depth, and
  EncodePng encodes it to each 8-bit and 16-bit layout. Both use no filter,
  so the System.Drawing comparison stays fair.
- PngFilterDecode covers each pixel width from 1 to 8 bytes.
- Each PNG benchmark has its results as a comment.
- The Sub decode uses byte lanes, because its predictor does not read the
  row above and needs no 16-bit arithmetic.
- PngFilterEncoder.Encode uses a left offset of 0 when the operator does
  not read the left pixel. The Up filter then starts its vector loops at
  the first byte, as on main.
- The PngFilterEncode benchmark passes the real byte count for Up, as the
  encoder does.
- The PNG filter decoder advances a pixel reference.
- Unsafe.Add offsets added on this branch are unsigned.
- DecodeFilteredPng decodes one photo stored with each filter, in 8-bit
  RGB and RGBA.
@JimBobSquarePants
JimBobSquarePants merged commit ecaa02f into main Nov 25, 2026
12 checks passed
@JimBobSquarePants
JimBobSquarePants deleted the js/perf-png branch November 25, 2026 07:48
@JimBobSquarePants JimBobSquarePants added this to the v5.0.0 milestone Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant