From f6a8d949c3781dbdcad1428c83c30e1b50be87a6 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Tue, 1 Dec 2026 07:46:00 -0800 Subject: [PATCH 1/2] Benchmarks: a cost-per-column table with every typed method on one row class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A cost-per-column benchmark in which every typed method builds the same row class. No library code changes. - `CostPerColumnBenchmarks`: the same read at 1 and 20 columns over the synthetic reader in default access: the hand-written selector (baseline), its identical copy as the noise control, the annotated generated reader, object arrays and Dapper `Query`, all but the arrays building `GenRowD1` or `GenRowD20`. The cost of one more cell is the 20-column time less the 1-column time, over the cells added. - Each timed process sets up and checks only its own rung against plain ADO.NET; `--verify-gate` covers the new table, and in a counters build it proves the generated rung is served by generated code. - The Dapper.AOT advice to add Dapper's column attribute is listed with the project's other suppressed advice: Dapper matches these columns by name. Published conditions: `ODX_GATE_SOURCES=synthetic ODX_GATE_ROWS=1000`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 5.5 --- .../CostPerColumnReads.cs | 81 ++++++++++++ .../CostPerColumnBenchmarks.cs | 121 ++++++++++++++++++ .../GateBenchmarks.cs | 13 ++ ...Open.Database.Extensions.Benchmarks.csproj | 4 +- 4 files changed, 217 insertions(+), 2 deletions(-) create mode 100644 Benchmarks/Open.Database.Extensions.Benchmarks.Generated/CostPerColumnReads.cs create mode 100644 Benchmarks/Open.Database.Extensions.Benchmarks/CostPerColumnBenchmarks.cs diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/CostPerColumnReads.cs b/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/CostPerColumnReads.cs new file mode 100644 index 00000000..0197c05b --- /dev/null +++ b/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/CostPerColumnReads.cs @@ -0,0 +1,81 @@ +using Open.Database.Extensions.Generator.Models; +using Open.Database.Extensions.Synchronous; +using System.Data; + +namespace Open.Database.Extensions.Benchmarks.Generated; + +/// +/// The ODX rungs of the cost-per-column table: the annotated generated reader and the hand-written selector, both building the same row class +/// ( at one column, at twenty). The hand selector reads each member as the generated reader does: +/// constant ordinals, a NULL test on the nullable members only. +/// +public static class CostPerColumnReads +{ + public static List Hand1(IExecuteReader command) + => command.Map(static r => new GenRowD1 { Id = r.GetInt32(0) }).ToList(); + + /// The A/A control of . + public static List HandCopy1(IExecuteReader command) + => command.Map(static r => new GenRowD1 { Id = r.GetInt32(0) }).ToList(); + + public static List Generated1(IExecuteReader command) + => command.Map().ToList(); + + public static List Hand20(IExecuteReader command) + => command.Map(static r => Read20(r)).ToList(); + + /// The A/A control of . + public static List HandCopy20(IExecuteReader command) + => command.Map(static r => ReadCopy20(r)).ToList(); + + public static List Generated20(IExecuteReader command) + => command.Map().ToList(); + + static GenRowD20 Read20(IDataRecord r) => new() + { + Id = r.GetInt32(0), + Created = r.GetDateTime(1), + Title = r.IsDBNull(2) ? null : r.GetString(2), + Price = r.GetDecimal(3), + Qty = r.IsDBNull(4) ? null : r.GetInt32(4), + IsActive = r.GetBoolean(5), + Key = r.GetGuid(6), + Score = r.GetDouble(7), + Note = r.IsDBNull(8) ? null : r.GetString(8), + Blob = r.IsDBNull(9) ? null : (byte[])r.GetValue(9), + Big = r.GetInt64(10), + Body = r.IsDBNull(11) ? null : r.GetString(11), + Amount = r.IsDBNull(12) ? null : r.GetDecimal(12), + Stamp = r.IsDBNull(13) ? null : r.GetDateTime(13), + Tag = r.IsDBNull(14) ? null : r.GetString(14), + Ratio = r.GetDouble(15), + Flag = r.IsDBNull(16) ? null : r.GetBoolean(16), + Link = r.IsDBNull(17) ? null : r.GetGuid(17), + Count = r.GetInt64(18), + Summary = r.IsDBNull(19) ? null : r.GetString(19), + }; + + static GenRowD20 ReadCopy20(IDataRecord r) => new() + { + Id = r.GetInt32(0), + Created = r.GetDateTime(1), + Title = r.IsDBNull(2) ? null : r.GetString(2), + Price = r.GetDecimal(3), + Qty = r.IsDBNull(4) ? null : r.GetInt32(4), + IsActive = r.GetBoolean(5), + Key = r.GetGuid(6), + Score = r.GetDouble(7), + Note = r.IsDBNull(8) ? null : r.GetString(8), + Blob = r.IsDBNull(9) ? null : (byte[])r.GetValue(9), + Big = r.GetInt64(10), + Body = r.IsDBNull(11) ? null : r.GetString(11), + Amount = r.IsDBNull(12) ? null : r.GetDecimal(12), + Stamp = r.IsDBNull(13) ? null : r.GetDateTime(13), + Tag = r.IsDBNull(14) ? null : r.GetString(14), + Ratio = r.GetDouble(15), + Flag = r.IsDBNull(16) ? null : r.GetBoolean(16), + Link = r.IsDBNull(17) ? null : r.GetGuid(17), + Count = r.GetInt64(18), + Summary = r.IsDBNull(19) ? null : r.GetString(19), + }; +} diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/CostPerColumnBenchmarks.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/CostPerColumnBenchmarks.cs new file mode 100644 index 00000000..55dfb8a4 --- /dev/null +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/CostPerColumnBenchmarks.cs @@ -0,0 +1,121 @@ +using BenchmarkDotNet.Attributes; +using Dapper; +using Open.Database.Extensions.Benchmarks.Generated; +using Open.Database.Extensions.Generator.Models; +using Open.Database.Extensions.Synchronous; +using System.Collections; + +namespace Open.Database.Extensions.Benchmarks; + +/// +/// Cost per column: the same read at one column and at twenty, every method but the object arrays building the same row class +/// (, ), over the synthetic reader in default access. The cost of one more cell is the time at +/// twenty columns less the time at one, over the cells added. The baseline is the hand-written selector, with its A/A copy. +/// +/// +/// ODX_GATE_SOURCES=synthetic ODX_GATE_ROWS=1000 are the published conditions; each timed process sets up and checks only its own rung. +/// +[MemoryDiagnoser] +[Config(typeof(GateConfig))] +public class CostPerColumnBenchmarks : GateSource +{ + [Params(1, 20)] + public int Width { get; set; } + + ReadModeConnection _mode = null!; + List _expected = null!; + + void Prepare() + { + Connect(Width); + _expected = Oracle(SyntheticVariants.Hand(Width)); + _mode = In(false); + } + + void CheckRung(string rung, Func read, bool generated = false) + { + _mode.Reset(); + // In a counters build the generated rung must be served by generated code, or it would time the runtime map. + IList rows = generated ? RealParityBenchmarks.Generated(rung, read) : (IList)read(); + CheckMode(rung, _mode, sequential: false); + if (rows is List arrays) + { + if (arrays.Count != _expected.Count) throw new InvalidOperationException($"{rung}: {arrays.Count} rows, expected {_expected.Count}."); + for (int i = 0; i < arrays.Count; i++) + { + if (!arrays[i].Zip(_expected[i].Boxed(Width)).All(static p => SynRow.Same(p.First, p.Second))) + throw new InvalidOperationException($"{rung}: row {i} differs from plain ADO.NET."); + } + + return; + } + + RealParityBenchmarks.Same(rung, _expected, rows); + } + + public override void VerifyAll() + { + Prepare(); + CheckRung(nameof(CtlH), CtlH); + CheckRung(nameof(CtlH2), CtlH2); + CheckRung(nameof(GenD), GenD, generated: true); + CheckRung(nameof(ObjectArrays), ObjectArrays); + CheckRung(nameof(Dapper), Dapper); + } + + [GlobalSetup(Target = nameof(CtlH))] + public void SetupCtlH() { Prepare(); CheckRung(nameof(CtlH), CtlH); } + + [GlobalSetup(Target = nameof(CtlH2))] + public void SetupCtlH2() { Prepare(); CheckRung(nameof(CtlH2), CtlH2); } + + [GlobalSetup(Target = nameof(GenD))] + public void SetupGenD() { Prepare(); CheckRung(nameof(GenD), GenD, generated: true); } + + [GlobalSetup(Target = nameof(ObjectArrays))] + public void SetupObjectArrays() { Prepare(); CheckRung(nameof(ObjectArrays), ObjectArrays); } + + [GlobalSetup(Target = nameof(Dapper))] + public void SetupDapper() { Prepare(); CheckRung(nameof(Dapper), Dapper); } + + [Benchmark(Baseline = true)] + public object CtlH() + => Width == 1 + ? CostPerColumnReads.Hand1(_mode.Command(SyntheticVariants.Sql)) + : CostPerColumnReads.Hand20(_mode.Command(SyntheticVariants.Sql)); + + [Benchmark] + public object CtlH2() + => Width == 1 + ? CostPerColumnReads.HandCopy1(_mode.Command(SyntheticVariants.Sql)) + : CostPerColumnReads.HandCopy20(_mode.Command(SyntheticVariants.Sql)); + + [Benchmark] + public object GenD() + => Width == 1 + ? CostPerColumnReads.Generated1(_mode.Command(SyntheticVariants.Sql)) + : CostPerColumnReads.Generated20(_mode.Command(SyntheticVariants.Sql)); + + [Benchmark] + public object ObjectArrays() + => _mode.Command(SyntheticVariants.Sql).ExecuteReader(static reader => + { + var rows = new List(); + while (reader.Read()) + { + var row = new object[reader.FieldCount]; + reader.GetValues(row); + rows.Add(row); + } + + while (reader.NextResult()) { } + return rows; + }); + + // Dapper matches these columns by name, and they are named as the members are: [Column(Order)] only places the generated reader. + [Benchmark(Description = "Dapper (forced)")] + public object Dapper() + => Width == 1 + ? _mode.Query(SyntheticVariants.Sql).AsList() + : _mode.Query(SyntheticVariants.Sql).AsList(); +} diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/GateBenchmarks.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/GateBenchmarks.cs index 48e58302..8a25fff2 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks/GateBenchmarks.cs +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/GateBenchmarks.cs @@ -84,6 +84,19 @@ void Run(GateShapes gate) single.Cleanup(); cases++; } + + // The cost-per-column table is published over the synthetic reader only. + if (source != GateSource.Synthetic) continue; + foreach (int rows in GateShapes.RowValues) + { + foreach (int width in (int[])[1, 20]) + { + var column = new CostPerColumnBenchmarks { Source = source, Rows = rows, Width = width }; + column.VerifyAll(); + column.Cleanup(); + cases++; + } + } } Console.WriteLine($"// GATE-VERIFY {cases} cases: every rung read the right rows in its table's mode."); diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/Open.Database.Extensions.Benchmarks.csproj b/Benchmarks/Open.Database.Extensions.Benchmarks/Open.Database.Extensions.Benchmarks.csproj index ece6cde0..ce1048fd 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks/Open.Database.Extensions.Benchmarks.csproj +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/Open.Database.Extensions.Benchmarks.csproj @@ -11,8 +11,8 @@ false false true - $(NoWarn);CS1591;DAP219;DAP231;DAP241 - + $(NoWarn);CS1591;DAP043;DAP219;DAP231;DAP241 + $(InterceptorsNamespaces);Dapper.AOT $(InterceptorsPreviewNamespaces);Dapper.AOT From fdec9f079dbe1b6abc9267919bc2ccd51a3eae45 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Tue, 1 Dec 2026 07:48:15 -0800 Subject: [PATCH 2/2] Benchmarks: hand reader as a method group; object arrays through AsObjectArrays() The 20-column hand selector no longer adds a call per row, and the object-array rung reads the way the library reads rows without a model. Co-Authored-By: Claude Sonnet 5.5 --- .../CostPerColumnReads.cs | 4 ++-- .../CostPerColumnBenchmarks.cs | 17 +++-------------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/CostPerColumnReads.cs b/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/CostPerColumnReads.cs index 0197c05b..70555096 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/CostPerColumnReads.cs +++ b/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/CostPerColumnReads.cs @@ -22,11 +22,11 @@ public static List Generated1(IExecuteReader command) => command.Map().ToList(); public static List Hand20(IExecuteReader command) - => command.Map(static r => Read20(r)).ToList(); + => command.Map(Read20).ToList(); /// The A/A control of . public static List HandCopy20(IExecuteReader command) - => command.Map(static r => ReadCopy20(r)).ToList(); + => command.Map(ReadCopy20).ToList(); public static List Generated20(IExecuteReader command) => command.Map().ToList(); diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/CostPerColumnBenchmarks.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/CostPerColumnBenchmarks.cs index 55dfb8a4..fba49797 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks/CostPerColumnBenchmarks.cs +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/CostPerColumnBenchmarks.cs @@ -43,7 +43,7 @@ void CheckRung(string rung, Func read, bool generated = false) if (arrays.Count != _expected.Count) throw new InvalidOperationException($"{rung}: {arrays.Count} rows, expected {_expected.Count}."); for (int i = 0; i < arrays.Count; i++) { - if (!arrays[i].Zip(_expected[i].Boxed(Width)).All(static p => SynRow.Same(p.First, p.Second))) + if (!arrays[i].Zip(_expected[i].Boxed(Width)).All(static p => SynRow.Same(p.First ?? DBNull.Value, p.Second))) throw new InvalidOperationException($"{rung}: row {i} differs from plain ADO.NET."); } @@ -96,21 +96,10 @@ public object GenD() ? CostPerColumnReads.Generated1(_mode.Command(SyntheticVariants.Sql)) : CostPerColumnReads.Generated20(_mode.Command(SyntheticVariants.Sql)); + // The library's way to read rows without a model. [Benchmark] public object ObjectArrays() - => _mode.Command(SyntheticVariants.Sql).ExecuteReader(static reader => - { - var rows = new List(); - while (reader.Read()) - { - var row = new object[reader.FieldCount]; - reader.GetValues(row); - rows.Add(row); - } - - while (reader.NextResult()) { } - return rows; - }); + => _mode.Command(SyntheticVariants.Sql).AsObjectArrays().ToList(); // Dapper matches these columns by name, and they are named as the members are: [Column(Order)] only places the generated reader. [Benchmark(Description = "Dapper (forced)")]