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
13 changes: 11 additions & 2 deletions benchmarks/Sdcb.SimdCodecs.Bmp.Benchmarks/BmpBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public class BmpDecodeBenchmarks
[Benchmark]
public void Wic() => _run[Names.Wic]();

public static readonly string[] Methods = [Names.Current, Names.CurrentBgra, Names.Baseline, Names.ImageSharp, Names.Wic];
[Benchmark]
public void CurrentRows() => _run[Names.CurrentRows]();

[Benchmark]
public void CurrentReadAll() => _run[Names.CurrentReadAll]();

public static readonly string[] Methods = [Names.Current, Names.CurrentRows, Names.CurrentReadAll, Names.CurrentBgra, Names.Baseline, Names.ImageSharp, Names.Wic];
}

/// <summary>BenchmarkDotNet over the encode cases; see <see cref="BmpDecodeBenchmarks"/>.</summary>
Expand Down Expand Up @@ -76,7 +82,10 @@ public class BmpEncodeBenchmarks
[Benchmark]
public void BaselineStream() => _run[Names.BaselineStream]();

public static readonly string[] Methods = [Names.Current, Names.CurrentStream, Names.CurrentBgra, Names.Baseline, Names.BaselineStream, Names.ImageSharp, Names.Wic];
[Benchmark]
public void CurrentSession() => _run[Names.CurrentSession]();

public static readonly string[] Methods = [Names.Current, Names.CurrentStream, Names.CurrentSession, Names.CurrentBgra, Names.Baseline, Names.BaselineStream, Names.ImageSharp, Names.Wic];
}

/// <summary>Runs every contestant of a case back to back before the next case, in the matrix's order.</summary>
Expand Down
9 changes: 6 additions & 3 deletions benchmarks/Sdcb.SimdCodecs.Bmp.Benchmarks/Cases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,26 @@ internal sealed record EncodeCase(string Content, string Format)
public static IReadOnlyList<EncodeCase> All { get; } =
[
.. from content in SourceImage.Contents
from format in new[] { "rgb24", "argb32", "rgb565", "p8", "rle8", "ico-set" }
from format in new[] { "rgb24", "argb32", "rgb24-topdown", "argb32-topdown", "rgb565", "p8", "rle8", "ico-set" }
select new EncodeCase(content, format),
];

public static IEnumerable<EncodeCase> Selected => All.Where(c => Settings.Selected(c.Name));

public static EncodeCase Find(string name) => All.Single(c => c.Name == name);

public bool Alpha => Format is "argb32" or "ico-set";
public bool Alpha => Format is "argb32" or "argb32-topdown" or "ico-set";

/// <summary>Whether a decode of the output must give the source back exactly.</summary>
public bool Lossless => Format is "rgb24" or "argb32" or "ico-set";
public bool Lossless => Format is "rgb24" or "argb32" or "rgb24-topdown" or "argb32-topdown" or "ico-set";

public BmpEncodeOptions Options => Format switch
{
"rgb24" => new BmpEncodeOptions { Format = BmpEncodeFormat.Rgb24 },
"argb32" => new BmpEncodeOptions { Format = BmpEncodeFormat.Argb32 },
// Top-down direct colour: the layouts the encoder session streams.
"rgb24-topdown" => new BmpEncodeOptions { Format = BmpEncodeFormat.Rgb24, TopDown = true },
"argb32-topdown" => new BmpEncodeOptions { Format = BmpEncodeFormat.Argb32, TopDown = true },
"rgb565" => new BmpEncodeOptions { Format = BmpEncodeFormat.Rgb565 },
"p8" => new BmpEncodeOptions { Format = BmpEncodeFormat.Indexed8 },
_ => new BmpEncodeOptions { Format = BmpEncodeFormat.Indexed8, Rle = true },
Expand Down
69 changes: 64 additions & 5 deletions benchmarks/Sdcb.SimdCodecs.Bmp.Benchmarks/Contestants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,41 @@ bool Current(byte[] into, PixelFormat format) => icon
if (Want(Names.Current)) setup.Contestants.Add(new(Names.Current, () => Current(rgba, PixelFormat.Rgba32)));
if (Want(Names.CurrentBgra)) setup.Contestants.Add(new(Names.CurrentBgra, () => Current(bgra, PixelFormat.Bgra32)));

// The streaming reader over a seekable stream (a bottom-up bitmap streams by seeking back a band at a time), RGBA
// rows a band of 64 at a time into the same kind of buffer: what reading a file costs next to decoding its bytes.
if (!icon && Want(Names.CurrentRows))
{
var buffer = new byte[w * h * 4];
void Rows()
{
using var reader = BmpImageDecoder.CreateRowReader(
new Abstractions.CodecSupport.StreamInput(new MemoryStream(data, writable: false)), PixelFormat.Rgba32, null);
reader.Initialize();
while (reader.RowsRead < reader.Height)
reader.ReadRows(buffer.AsSpan(reader.RowsRead * w * 4), 64);
}
if (only is null)
{
Rows();
if (!buffer.AsSpan().SequenceEqual(rgba))
throw new InvalidOperationException($"{c.Name}: the streaming reader's rows differ from TryDecodeInto's; refusing to benchmark.");
}
setup.Contestants.Add(new(Names.CurrentRows, Rows));
}

// What a caller without the reader does with a stream: read it whole (into a reused buffer of the right size,
// the cheapest way), then decode the bytes.
if (!icon && Want(Names.CurrentReadAll))
{
var file = new byte[data.Length];
setup.Contestants.Add(new(Names.CurrentReadAll, () =>
{
using var stream = new MemoryStream(data, writable: false);
stream.ReadExactly(file);
BmpImageDecoder.TryDecodeInto(file, rgba, 0, PixelFormat.Rgba32);
}));
}

if (Baseline.IsBuilt && Want(Names.Baseline) && Baseline.CanDecode(icon))
{
var buffer = new byte[w * h * 4];
Expand Down Expand Up @@ -184,14 +219,38 @@ void Check(string name, byte[] bmp)
}));
}

// The encoder session, 64 rows at a time into a reused writer: it streams for the top-down cases.
if (Want(Names.CurrentSession) && BmpEncoder.CanStream(options))
{
var writer = new System.Buffers.ArrayBufferWriter<byte>();
void Session()
{
writer.ResetWrittenCount();
using var session = BmpImageEncoder.Create(writer, options);
var frame = session.AddFrame(w, h, PixelFormat.Rgba32);
for (var y = 0; y < h; y += 64)
{
var n = Math.Min(64, h - y);
frame.WriteRows(rgba.AsSpan(y * w * 4, n * w * 4), n);
}
session.Commit();
}
Session();
var file = writer.WrittenSpan.ToArray();
if (only is null && !file.AsSpan().SequenceEqual(BmpEncoder.Encode(rgba, w, h, PixelFormat.Rgba32, options)))
throw new InvalidOperationException($"{c.Name}: the session's bytes differ from BmpEncoder.Encode's; refusing to benchmark.");
Check(Names.CurrentSession, file);
setup.Contestants.Add(new(Names.CurrentSession, Session));
}

// B, G, R(, A) input, for the layouts WIC writes.
byte[]? native = c.Format switch
{
"rgb24" => Swizzle(rgba, 3),
"argb32" => Swizzle(rgba, 4),
"rgb24" or "rgb24-topdown" => Swizzle(rgba, 3),
"argb32" or "argb32-topdown" => Swizzle(rgba, 4),
_ => null,
};
var nativeFormat = c.Format == "rgb24" ? PixelFormat.Bgr24 : PixelFormat.Bgra32;
var nativeFormat = c.Format is "rgb24" or "rgb24-topdown" ? PixelFormat.Bgr24 : PixelFormat.Bgra32;
if (native is not null && Want(Names.CurrentBgra))
{
var stream = new MemoryStream();
Expand Down Expand Up @@ -225,8 +284,8 @@ void Check(string name, byte[] bmp)

IsBits? bits = c.Format switch
{
"rgb24" => IsBits.Pixel24,
"argb32" => IsBits.Pixel32,
"rgb24" or "rgb24-topdown" => IsBits.Pixel24,
"argb32" or "argb32-topdown" => IsBits.Pixel32,
"rgb565" => IsBits.Pixel16,
"p8" => IsBits.Pixel8,
_ => null, // ImageSharp writes no RLE
Expand Down
6 changes: 6 additions & 0 deletions benchmarks/Sdcb.SimdCodecs.Bmp.Benchmarks/Infrastructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ internal static class Names
public const string Current = "Current";
public const string CurrentBgra = "CurrentBgra";
public const string CurrentStream = "CurrentStream";
public const string CurrentRows = "CurrentRows";
public const string CurrentReadAll = "CurrentReadAll";
public const string CurrentSession = "CurrentSession";
public const string Baseline = "Baseline";
public const string BaselineStream = "BaselineStream";
public const string ImageSharp = "ImageSharp";
Expand All @@ -74,6 +77,9 @@ internal static class Names
Current => "current",
CurrentBgra => "current BGR(A)",
CurrentStream => "current → stream",
CurrentRows => "current rows ← stream",
CurrentReadAll => "current ← read whole stream",
CurrentSession => "current session → writer",
Baseline => "baseline",
BaselineStream => "baseline → stream",
Wic => "WIC",
Expand Down
27 changes: 14 additions & 13 deletions benchmarks/Sdcb.SimdCodecs.Bmp.Benchmarks/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,26 @@ public static string RenderDecode(IReadOnlyList<(DecodeSetup Setup, Dictionary<s
"the same to BGRA, the layout WIC produces; ImageSharp = `Image.Load<Rgba32>` + `CopyPixelDataTo`, one thread " +
"(it reads no icons); WIC = WPF's `BitmapDecoder` + `FormatConvertedBitmap` to BGRA. Ratios below 1 mean we are " +
"faster: current ÷ ImageSharp (both RGBA), current BGR(A) ÷ WIC (both BGRA). Every timed output was checked " +
"against the others first.");
"against the others first. `current rows ← stream` = the streaming reader (`BmpImageDecoder.CreateRowReader`) " +
"over a seekable `MemoryStream`, RGBA rows 64 at a time; `current ← read whole stream` = the same stream read whole into a reused buffer, then `TryDecodeInto`: what streaming replaces. `rows ÷ current` is what streaming costs over decoding bytes already in memory, `rows ÷ read whole` over reading the stream first.");
sb.AppendLine();
sb.AppendLine("| Case | KB | MP | current | current BGR(A) | baseline | ImageSharp | WIC | current MP/s | ÷ ImageSharp | ÷ WIC | ÷ baseline |");
sb.AppendLine("|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|");
sb.AppendLine("| Case | KB | MP | current | current rows ← stream | current ← read whole stream | current BGR(A) | baseline | ImageSharp | WIC | current MP/s | ÷ ImageSharp | ÷ WIC | ÷ baseline | rows ÷ current | rows ÷ read whole |");
sb.AppendLine("|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|");
var notes = new List<string>();
foreach (var (s, ms) in rows)
{
var mp = s.Width * (double)s.Height / 1e6;
sb.AppendLine($"| `{s.Case.Name}` | {s.Data.Length / 1024.0:F0} | {mp:F2} | {Ms(ms, Names.Current)} | {Ms(ms, Names.CurrentBgra)} | " +
sb.AppendLine($"| `{s.Case.Name}` | {s.Data.Length / 1024.0:F0} | {mp:F2} | {Ms(ms, Names.Current)} | {Ms(ms, Names.CurrentRows)} | {Ms(ms, Names.CurrentReadAll)} | {Ms(ms, Names.CurrentBgra)} | " +
$"{Ms(ms, Names.Baseline)} | {Ms(ms, Names.ImageSharp)} | {Ms(ms, Names.Wic)} | " +
$"{(Get(ms, Names.Current) is { } c ? (mp / (c / 1000)).ToString("F0", CultureInfo.InvariantCulture) : "—")} | " +
$"{Ratio(ms, Names.Current, Names.ImageSharp)} | {Ratio(ms, Names.CurrentBgra, Names.Wic)} | {Ratio(ms, Names.Current, Names.Baseline)} |");
$"{Ratio(ms, Names.Current, Names.ImageSharp)} | {Ratio(ms, Names.CurrentBgra, Names.Wic)} | {Ratio(ms, Names.Current, Names.Baseline)} | {Ratio(ms, Names.CurrentRows, Names.Current)} | {Ratio(ms, Names.CurrentRows, Names.CurrentReadAll)} |");
notes.AddRange(s.Notes.Select(n => $"`{s.Case.Name}`: {n}"));
}

sb.AppendLine();
sb.AppendLine("Geometric means over the cases both decode:");
sb.AppendLine();
foreach (var (a, b) in new[] { (Names.Current, Names.ImageSharp), (Names.CurrentBgra, Names.Wic), (Names.Current, Names.Baseline) })
foreach (var (a, b) in new[] { (Names.Current, Names.ImageSharp), (Names.CurrentBgra, Names.Wic), (Names.Current, Names.Baseline), (Names.CurrentRows, Names.Current), (Names.CurrentRows, Names.CurrentReadAll) })
{
foreach (var content in SourceImage.Contents)
{
Expand All @@ -75,7 +76,7 @@ public static string RenderDecode(IReadOnlyList<(DecodeSetup Setup, Dictionary<s
}

AppendAllocations(sb, rows.Select(r => (r.Setup.Case.Name, r.Setup.Allocated)).ToList(),
[Names.Current, Names.CurrentBgra, Names.Baseline, Names.ImageSharp, Names.Wic]);
[Names.Current, Names.CurrentRows, Names.CurrentReadAll, Names.CurrentBgra, Names.Baseline, Names.ImageSharp, Names.Wic]);
AppendNotes(sb, notes);
return sb.ToString();
}
Expand All @@ -89,20 +90,20 @@ public static string RenderEncode(IReadOnlyList<(EncodeSetup Setup, Dictionary<s
"= the `Stream` overload into a reused stream, as ImageSharp and WIC write; `baseline` / `baseline → stream` = the same two for the previous commit; `current BGR(A)` = that from the B, G, R(, A) " +
"input WIC gets; ImageSharp = `BmpEncoder` with the same bits per pixel into a reused stream, one " +
"thread, from an `Image<Rgba32>` built beforehand; WIC = WPF's `BmpBitmapEncoder` over a `BitmapSource` of B, G, " +
"R(, A), into a reused stream (24- and 32-bit only). Every output decodes, and the lossless layouts give the source back.");
"R(, A), into a reused stream (24- and 32-bit only); `current session → writer` = `BmpImageEncoder.Create`, 64 rows per `WriteRows` into a reused `ArrayBufferWriter` (streaming only for the top-down cases). Every output decodes, and the lossless layouts give the source back.");
sb.AppendLine();
sb.AppendLine("| Case | current | KB | current → stream | current BGR(A) | baseline | baseline → stream | ImageSharp | KB | WIC | KB | stream ÷ ImageSharp | BGR(A) ÷ WIC | ÷ baseline | stream ÷ baseline |");
sb.AppendLine("|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|");
sb.AppendLine("| Case | current | KB | current → stream | current session → writer | current BGR(A) | baseline | baseline → stream | ImageSharp | KB | WIC | KB | stream ÷ ImageSharp | BGR(A) ÷ WIC | ÷ baseline | stream ÷ baseline | session ÷ stream |");
sb.AppendLine("|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|");
var notes = new List<string>();
foreach (var (s, ms) in rows)
{
sb.AppendLine($"| `{s.Case.Name}` | {Ms(ms, Names.Current)} | {Kb(s, Names.Current)} | {Ms(ms, Names.CurrentStream)} | {Ms(ms, Names.CurrentBgra)} | " +
sb.AppendLine($"| `{s.Case.Name}` | {Ms(ms, Names.Current)} | {Kb(s, Names.Current)} | {Ms(ms, Names.CurrentStream)} | {Ms(ms, Names.CurrentSession)} | {Ms(ms, Names.CurrentBgra)} | " +
$"{Ms(ms, Names.Baseline)} | {Ms(ms, Names.BaselineStream)} | {Ms(ms, Names.ImageSharp)} | {Kb(s, Names.ImageSharp)} | {Ms(ms, Names.Wic)} | {Kb(s, Names.Wic)} | " +
$"{Ratio(ms, Names.CurrentStream, Names.ImageSharp)} | {Ratio(ms, Names.CurrentBgra, Names.Wic)} | {Ratio(ms, Names.Current, Names.Baseline)} | {Ratio(ms, Names.CurrentStream, Names.BaselineStream)} |");
$"{Ratio(ms, Names.CurrentStream, Names.ImageSharp)} | {Ratio(ms, Names.CurrentBgra, Names.Wic)} | {Ratio(ms, Names.Current, Names.Baseline)} | {Ratio(ms, Names.CurrentStream, Names.BaselineStream)} | {Ratio(ms, Names.CurrentSession, Names.CurrentStream)} |");
notes.AddRange(s.Notes.Select(n => $"`{s.Case.Name}`: {n}"));
}
AppendAllocations(sb, rows.Select(r => (r.Setup.Case.Name, r.Setup.Allocated)).ToList(),
[Names.Current, Names.CurrentStream, Names.CurrentBgra, Names.Baseline, Names.BaselineStream, Names.ImageSharp, Names.Wic]);
[Names.Current, Names.CurrentStream, Names.CurrentSession, Names.CurrentBgra, Names.Baseline, Names.BaselineStream, Names.ImageSharp, Names.Wic]);
AppendNotes(sb, notes);
return sb.ToString();
}
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/Sdcb.SimdCodecs.Bmp.Benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ Every bitmap is `1920×1080` (`--size WxH`), cut from the PNG benchmark's two so
|---|---|---|
| decode | `rgb24`, `bgra32` (32-bit with alpha), `rgb16`, `p8`, `p4`, `p1` | ImageSharp, a foreign encoder |
| decode | `rgb24-topdown`, `argb32-v4` (V4 alpha mask), `rgb565`, `rle8`, `rle4`, `ico256-bmp32`, `ico256-png`, `ico48-bmp8` | ours (layouts ImageSharp does not write) |
| encode | `rgb24`, `argb32`, `rgb565`, `p8`, `rle8` from RGBA; `ico-set` (16, 32, 48 as DIB, 256 as PNG) | — |
| encode | `rgb24`, `argb32`, `rgb24-topdown`, `argb32-topdown` (the layouts the encoder session streams), `rgb565`, `p8`, `rle8` from RGBA; `ico-set` (16, 32, 48 as DIB, 256 as PNG) | — |

## Contestants

| Name | Decode | Encode |
|---|---|---|
| `current` | `BmpImageDecoder` / `IcoImageDecoder.TryDecodeInto` to RGBA in a reused buffer | `BmpEncoder` / `IcoEncoder`, from RGBA |
| `current BGR(A)` | the same to BGRA, WIC's layout | the same from the B, G, R(, A) input WIC gets |
| `current rows ← stream` / `current session → writer` | `BmpImageDecoder.CreateRowReader` over a seekable `MemoryStream`, RGBA rows 64 at a time (bitmaps only) | `BmpImageEncoder.Create`, 64 rows per `WriteRows` into a reused `ArrayBufferWriter` (the cases whose options stream) |
| `baseline` | the previous commit's decoder, same process (`baseline [git-ref]`) | the previous commit's encoder, to an array and (`baseline → stream`) into a reused stream |
| ImageSharp 3.1.12 | `Image.Load<Rgba32>` + `CopyPixelDataTo`, `MaxDegreeOfParallelism = 1` (no icons: it has no ICO codec) | `BmpEncoder` with the same bits per pixel, into a reused stream (no RLE) |
| WIC | WPF's `BitmapDecoder` + `FormatConvertedBitmap` to BGRA (Windows only) | WPF's `BmpBitmapEncoder` from B, G, R(, A) into a reused stream (24- and 32-bit) |
Expand Down
Loading