From d05cc334eed69982ec48ae5f5236a828783f36ec Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Wed, 2 Dec 2026 12:47:39 -0800 Subject: [PATCH] Tests: the allocation probes read a column of ints that does not box SyncToListAllocationProbe failed now and then (the chain 2,800,784 bytes, the list that grows 1,049,704). The difference is exactly 24 bytes a row: a DataTableReader boxes each int it reads until the JIT has optimized the read, and the probe measured the chain before the list, so when the optimization landed between the two measurements the chain paid for the boxes and the list did not. The pool and the library were not involved. Both probes that read a DataTable (the synchronous ToList and the reader's ToArrayAsync and ToImmutableArrayAsync) now read an in-memory reader of one int column whose typed getter allocates nothing, as a provider's does, so the count no longer depends on when the JIT tiers up. The threshold is unchanged. Over 100,000 rows the same counts come out with default settings, with TieredCompilation off and with tier-up delayed (the chain 400,136 bytes, the growing list 1,049,032). Co-Authored-By: Claude Sonnet 5.5 --- .../ReaderCollectAllocationProbe.cs | 36 +++++------ .../SyncToListAllocationProbe.cs | 28 ++++----- .../TestHelpers/Int32ColumnReader.cs | 63 +++++++++++++++++++ 3 files changed, 88 insertions(+), 39 deletions(-) create mode 100644 Tests/Open.Database.Extensions.Core.Tests/TestHelpers/Int32ColumnReader.cs diff --git a/Tests/Open.Database.Extensions.Core.Tests/ReaderCollectAllocationProbe.cs b/Tests/Open.Database.Extensions.Core.Tests/ReaderCollectAllocationProbe.cs index 4c4ae7d0..8b5fab01 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ReaderCollectAllocationProbe.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ReaderCollectAllocationProbe.cs @@ -6,8 +6,8 @@ namespace Open.Database.Extensions.Tests; // Bytes per call of the reader's ToArrayAsync and ToImmutableArrayAsync (pooled chunks, one copy) // against the same rows read into an ImmutableArray.Builder whose capacity is not known. -// A count of bytes, not a time. The reader is an in-memory DataTableReader, whose -// ReadAsync finishes at once and allocates nothing, so a thread-local counter sees everything. +// A count of bytes, not a time. The reader is an in-memory reader of one int column (Int32ColumnReader), whose +// ReadAsync finishes at once and whose typed getter allocates nothing, so a thread-local counter sees everything. // The read alone is measured too, so that what the rows themselves cost can be taken off. // Run it by itself, in Release: dotnet test --filter-method "*Probe_BytesPerCall" with the Explicit run option. [ExcludeFromCodeCoverage] @@ -16,25 +16,17 @@ public sealed class ReaderCollectAllocationProbe(ITestOutputHelper output) { const int Rounds = 15; - static DataTable Table(int rows) - { - var table = new DataTable(); - table.Columns.Add("Id", typeof(int)); - for (int i = 0; i < rows; i++) table.Rows.Add(i); - return table; - } - static ValueTask Id(IDataRecord r) => new(r.GetInt32(0)); // The smallest of several runs, each with a new reader made before the count starts. - static async Task Measure(DataTable table, Func call) + static async Task Measure(Int32Column column, Func call) { - for (int i = 0; i < 20; i++) await call(table.CreateDataReader()); + for (int i = 0; i < 20; i++) await call(new Int32ColumnReader(column)); long best = long.MaxValue; for (int i = 0; i < Rounds; i++) { - DbDataReader reader = table.CreateDataReader(); + DbDataReader reader = new Int32ColumnReader(column); GC.Collect(); GC.WaitForPendingFinalizers(); long before = GC.GetAllocatedBytesForCurrentThread(); @@ -52,11 +44,11 @@ static async Task Measure(DataTable table, Func call) [InlineData(100_000)] public async Task ToImmutableArrayAsync_AllocatesLessThanABuilderOfUnknownCapacity(int rows) { - DataTable table = Table(rows); + Int32Column column = Int32Column.Of(rows); - long chain = await Measure(table, async r + long chain = await Measure(column, async r => GC.KeepAlive(await r.ToImmutableArrayAsync(Id, TestContext.Current.CancellationToken))); - long builder = await Measure(table, async r => + long builder = await Measure(column, async r => { ImmutableArray.Builder b = ImmutableArray.CreateBuilder(); while (await r.ReadAsync(TestContext.Current.CancellationToken)) b.Add(r.GetInt32(0)); @@ -73,25 +65,25 @@ public async Task Probe_BytesPerCall() output.WriteLine("rows | read alone | ToImmutableArrayAsync | builder + ToImmutable | ToArrayAsync | List + ToArray | (bytes; net of the read alone in the next line)"); foreach (int rows in new[] { 0, 1, 1000, 100_000 }) { - DataTable table = Table(rows); + Int32Column column = Int32Column.Of(rows); - long read = await Measure(table, async r => + long read = await Measure(column, async r => { int sum = 0; while (await r.ReadAsync(TestContext.Current.CancellationToken)) sum += r.GetInt32(0); GC.KeepAlive(sum); }); - long chain = await Measure(table, async r + long chain = await Measure(column, async r => GC.KeepAlive(await r.ToImmutableArrayAsync(Id, TestContext.Current.CancellationToken))); - long builder = await Measure(table, async r => + long builder = await Measure(column, async r => { ImmutableArray.Builder b = ImmutableArray.CreateBuilder(); while (await r.ReadAsync(TestContext.Current.CancellationToken)) b.Add(r.GetInt32(0)); GC.KeepAlive(b.ToImmutable()); }); - long array = await Measure(table, async r + long array = await Measure(column, async r => GC.KeepAlive(await r.ToArrayAsync(Id, TestContext.Current.CancellationToken))); - long list = await Measure(table, async r => + long list = await Measure(column, async r => { var l = new List(); while (await r.ReadAsync(TestContext.Current.CancellationToken)) l.Add(r.GetInt32(0)); diff --git a/Tests/Open.Database.Extensions.Core.Tests/SyncToListAllocationProbe.cs b/Tests/Open.Database.Extensions.Core.Tests/SyncToListAllocationProbe.cs index 1a43449b..07868634 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/SyncToListAllocationProbe.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/SyncToListAllocationProbe.cs @@ -2,8 +2,10 @@ namespace Open.Database.Extensions.Tests; #nullable enable -// Bytes per call of the synchronous ToList of a mapped view, over an in-memory DataTable, -// against a List that grows (the old shape, written by hand over a DataTableReader). +// Bytes per call of the synchronous ToList of a mapped view, over an in-memory reader of one int column, +// against a List that grows (the old shape, written by hand over the same reader). +// The reader's typed getter allocates nothing (see Int32ColumnReader): a DataTableReader boxes each value +// until the JIT has optimized the read, which made this a race with the JIT's tier-up. // A count of bytes, not a time; the thread-local counter sees everything because nothing suspends. // Run it by itself, in Release, with the test executable and -explicit only. [ExcludeFromCodeCoverage] @@ -12,14 +14,6 @@ public sealed class SyncToListAllocationProbe(ITestOutputHelper output) { const int Rounds = 15; - static DataTable Table(int rows) - { - var table = new DataTable(); - table.Columns.Add("Id", typeof(int)); - for (int i = 0; i < rows; i++) table.Rows.Add(i); - return table; - } - // The smallest of several runs, after a warm-up. static long Measure(Action call) { @@ -45,12 +39,12 @@ static long Measure(Action call) [InlineData(100_000)] public void ToList_AllocatesLessThanAListThatGrows(int rows) { - DataTable table = Table(rows); + Int32Column column = Int32Column.Of(rows); - long chain = Measure(() => GC.KeepAlive(table.Map().ToList())); + long chain = Measure(() => GC.KeepAlive(new Int32ColumnReader(column).Map().ToList())); long grows = Measure(() => { - using DataTableReader r = table.CreateDataReader(); + using var r = new Int32ColumnReader(column); var l = new List(); while (r.Read()) l.Add(r.GetInt32(0)); GC.KeepAlive(l); @@ -66,17 +60,17 @@ public void Probe_BytesPerCall() output.WriteLine("rows | view.ToList() | List that grows (by hand) | view.ToArray() (bytes per call)"); foreach (int rows in new[] { 0, 1, 1000, 100_000 }) { - DataTable table = Table(rows); + Int32Column column = Int32Column.Of(rows); - long list = Measure(() => GC.KeepAlive(table.Map().ToList())); + long list = Measure(() => GC.KeepAlive(new Int32ColumnReader(column).Map().ToList())); long grows = Measure(() => { - using DataTableReader r = table.CreateDataReader(); + using var r = new Int32ColumnReader(column); var l = new List(); while (r.Read()) l.Add(r.GetInt32(0)); GC.KeepAlive(l); }); - long array = Measure(() => GC.KeepAlive(table.Map().ToArray())); + long array = Measure(() => GC.KeepAlive(new Int32ColumnReader(column).Map().ToArray())); output.WriteLine($"{rows,7} | {list,12} | {grows,12} | {array,12}"); } diff --git a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/Int32ColumnReader.cs b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/Int32ColumnReader.cs new file mode 100644 index 00000000..9aab8cd6 --- /dev/null +++ b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/Int32ColumnReader.cs @@ -0,0 +1,63 @@ +namespace Open.Database.Extensions.Tests; + +#nullable enable + +/// +/// The rows of a reader of one column, made once: a reader +/// over it allocates only itself, so the same column serves every call of an +/// allocation probe. +/// +/// The value of each row. +[ExcludeFromCodeCoverage] +internal sealed record Int32Column(int[] Values) +{ + /// The columns every reader reports. + public static readonly ImmutableArray Columns = [new("Id", typeof(int))]; + + /// One empty slot per row, which the base reader steps through. + public object?[][] Slots { get; } = new object?[Values.Length][]; + + /// Makes the column of rows: 0, 1, 2, .... + /// The number of rows. + /// The column. + public static Int32Column Of(int rows) + { + var values = new int[rows]; + for (int i = 0; i < rows; i++) values[i] = i; + return new(values); + } +} + +/// +/// An in-memory over one +/// column whose typed getter does not allocate, as a +/// provider's does not. A DataTableReader boxes each value until the +/// JIT has optimized the read, so an allocation probe over one counts the +/// JIT's timing as well as the library's. +/// +/// The rows. +[ExcludeFromCodeCoverage] +internal sealed class Int32ColumnReader(Int32Column column) + : TypedReader(Int32Column.Columns, column.Slots) +{ + int _at = -1; + + public override bool Read() + { + if (!base.Read()) return false; + ++_at; + return true; + } + + public override int GetInt32(int ordinal) + => column.Values[_at]; + + public override bool IsDBNull(int ordinal) + => false; + + public override object GetValue(int ordinal) + => column.Values[_at]; + + public override T GetFieldValue(int ordinal) + => (T)(object)column.Values[_at]; +}