Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions benchmarks/Sdcb.SimdCodecs.Jpeg.Benchmarks/EncodeBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Buffers;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using Sdcb.SimdCodecs.Abstractions;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
Expand Down Expand Up @@ -85,6 +87,13 @@ private static bool RunMode(string mode, List<Image> images, int reps, bool gate
contestants.Insert(1, new("baseline", baseline));
if (mode == "baseline")
{
// The same encoder through the streaming session, fed 16-row bands: its bytes must equal "ours".
var sink = new ArrayBufferWriter<byte>();
contestants.Insert(1, new("streamed", (img, c) => EncodeStreamed(sink, img, new JpegEncodeOptions
{
Quality = c.Quality,
Subsampling = c.Subsample420 ? JpegSubsampling.Chroma420 : JpegSubsampling.Chroma444,
})));
contestants.Add(new("ImageSharp", (img, c) =>
{
using var image = SixLabors.ImageSharp.Image.LoadPixelData<Rgb24>(img.Rgb, img.Width, img.Height);
Expand Down Expand Up @@ -120,10 +129,14 @@ private static bool RunMode(string mode, List<Image> images, int reps, bool gate
var tallies = contestants.ToDictionary(c => c.Name, _ => new Tally());
foreach (var image in images)
{
byte[] oursJpeg = [];
foreach (var c in contestants)
{
var t = tallies[c.Name];
var jpeg = c.Encode(image, config); // also the warm-up
if (c.Name == "ours") oursJpeg = jpeg;
else if (c.Name == "streamed" && !jpeg.AsSpan().SequenceEqual(oursJpeg))
throw new InvalidOperationException($"{image.Name} {config}: the streaming session's bytes differ from JpegEncoder.Encode's");
var times = new double[reps];
for (var r = 0; r < reps; r++)
{
Expand Down Expand Up @@ -167,6 +180,22 @@ private static bool RunMode(string mode, List<Image> images, int reps, bool gate
return ok;
}

/// <summary>Encodes through <see cref="JpegImageEncoder"/>'s streaming session, 16 rows per <c>WriteRows</c>.</summary>
private static byte[] EncodeStreamed(ArrayBufferWriter<byte> sink, Image img, JpegEncodeOptions options)
{
sink.ResetWrittenCount();
using var session = JpegImageEncoder.Create(sink, options);
var frame = session.AddFrame(img.Width, img.Height, PixelFormat.Rgb24);
var rowBytes = img.Width * 3;
for (var y = 0; y < img.Height; y += 16)
{
var n = Math.Min(16, img.Height - y);
frame.WriteRows(img.Rgb.AsSpan(y * rowBytes, n * rowBytes), n);
}
session.Commit();
return sink.WrittenSpan.ToArray();
}

// ---------------------------------------------------------------- inputs

private static List<Image> LoadMasters(int maxDim)
Expand Down
12 changes: 12 additions & 0 deletions benchmarks/Sdcb.SimdCodecs.Jpeg.Benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,15 @@ Recorded on a Ryzen 7 5800X, Windows 11, .NET 10.0.11, AVX2 (no AVX-512), MP/s:
| q95 4:4:4 | 266 | 262 | 139 | 144 / 96 | 51 / 42 |

At the same quality our files are 0.2–2.3% smaller than libjpeg-turbo's in every mode and PSNR-Y is 0.004–0.09 dB higher (float AAN DCT, unrounded colour conversion).

In `baseline` mode a `streamed` row is the same encoder through `JpegImageEncoder`'s streaming session, fed 16-row bands into a reused `ArrayBufferWriter`; the run aborts unless its bytes equal `JpegEncoder.Encode`'s. Recorded 2026-11-23 on the same Ryzen 7 5800X (Windows 11, .NET 10.0.15, AVX2), median of 15, machine shared with other jobs, MP/s:

| config | buffered | streamed | streamed ÷ buffered time |
|---|---:|---:|---:|
| q50 4:2:0 | 636 | 604 | 1.05× |
| q75 4:2:0 | 587 | 588 | 1.00× |
| q85 4:2:0 | 543 | 543 | 1.00× |
| q90 4:2:0 | 505 | 500 | 1.01× |
| q95 4:2:0 | 442 | 441 | 1.00× |
| q90 4:4:4 | 312 | 311 | 1.00× |
| q95 4:4:4 | 275 | 276 | 1.00× |
Loading