Skip to content

Commit e69a759

Browse files
Benchmarks re-measured on the release candidate: 2.4 to 2.8 times faster than Dapper on multi-row reads, records cost the same as classes, and ODE's time per row is 15 to 25% above the old numbers (unchanged by the recent work) (#83)
* Benchmarks: a Record scenario (the 12-column Post as a positional record) against Dapper's constructor mapping RecordList and RecordFirst at 1, 100 and 1000 rows. Setup checks that both libraries return equal records. Built, not run: running it needs the owner's go. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * Benchmarks: Array, First and List re-measured on the release candidate; a Records section Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 41af17c commit e69a759

3 files changed

Lines changed: 73 additions & 10 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Configs;
3+
using Dapper;
4+
5+
namespace Open.Database.Extensions.Benchmarks;
6+
7+
/// <summary>The 12-column <see cref="Post"/> as a positional record: both libraries build it through its constructor.</summary>
8+
public sealed record PostRecord(int Id, string Title, string? Body, DateTime Created, decimal Price, bool IsPublished, int? ViewCount, double Score, Guid Key, short Rank, long Big, string? Tag);
9+
10+
// A positional record read through its constructor: Dapper (the baseline) and Open.Database.Extensions on the same rows.
11+
// Ratio < 1 means ODE is faster. The allocation columns are the tie-breaker.
12+
[MemoryDiagnoser]
13+
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
14+
[CategoriesColumn]
15+
public class RecordBenchmarks
16+
{
17+
const string Sql = "select * from posts";
18+
19+
[Params(1, 100, 1000)]
20+
public int Rows { get; set; }
21+
22+
BenchConnection _conn = null!;
23+
24+
[GlobalSetup]
25+
public void Setup()
26+
{
27+
_conn = new BenchConnection(BenchTable.Posts(Rows));
28+
29+
// Both libraries must return the same records, or the comparison measures nothing.
30+
List<PostRecord> dapper = RecordList_Dapper();
31+
List<PostRecord> ode = RecordList_ODE();
32+
if (dapper.Count != Rows || !dapper.SequenceEqual(ode))
33+
throw new InvalidOperationException("Dapper and Open.Database.Extensions returned different records.");
34+
}
35+
36+
[BenchmarkCategory("RecordList"), Benchmark(Baseline = true)]
37+
public List<PostRecord> RecordList_Dapper() => _conn.Query<PostRecord>(Sql).AsList();
38+
39+
[BenchmarkCategory("RecordList"), Benchmark]
40+
public List<PostRecord> RecordList_ODE() => _conn.Command(Sql).Map<PostRecord>().ToList();
41+
42+
[BenchmarkCategory("RecordFirst"), Benchmark(Baseline = true)]
43+
public PostRecord? RecordFirst_Dapper() => _conn.QueryFirstOrDefault<PostRecord>(Sql);
44+
45+
[BenchmarkCategory("RecordFirst"), Benchmark]
46+
public PostRecord? RecordFirst_ODE() => _conn.Command(Sql).Map<PostRecord>().FirstOrDefault();
47+
}

‎Benchmarks/RESULTS.md‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ DateTime, Guid, strings, three of them nullable).
88
- **Ratio** is ODE time / Dapper time (below 1 is faster). **Alloc** is ODE bytes / Dapper bytes (below 1 is less memory).
99
- Reproduce: `dotnet run -c Release --project Benchmarks/Open.Database.Extensions.Benchmarks -- --filter "*"`
1010
- Not measured here: real network/database time (the provider is in-memory). Cross-check against a real provider before quoting these as end-to-end gains.
11+
- **Re-measured 2026-11-08** on the release candidate code (master 41af17c): the `Array`, `First` and `List` rows below and the new `Record` rows, as scoped runs with nothing else running on the machine. The other rows are earlier measurements and were not re-run. Against the earlier `Array` and `List` rows, ODE's time at 100 or more rows is 15 to 25% higher (a 100-row `List` took 3248 ns, now 4030 ns); it is the same as on the last commit before constructor binding (a 100-row `Array` took 4166 ns there, 4254 ns now), so the last changes did not move it. Allocations are the same or lower (`First` 312 B to 280 B, a 1-row `List` 400 B to 368 B). Ratios against Dapper moved from about 0.3 to about 0.4, which is still 2.4 to 2.8 times faster.
1112

1213
| Scenario | Rows | Dapper | ODE | Ratio | Dapper alloc | ODE alloc | Alloc |
1314
|---|---:|---:|---:|---:|---:|---:|---:|
14-
| Array | 1 | 329 ns | 122 ns | 0.37 | 888 B | 344 B | 0.39 |
15-
| Array | 100 | 11594 ns | 3368 ns | 0.29 | 37264 B | 12224 B | 0.33 |
16-
| Array | 1000 | 106683 ns | 33787 ns | 0.32 | 363072 B | 120224 B | 0.33 |
15+
| Array | 1 | 301 ns | 131 ns | 0.44 | 888 B | 312 B | 0.35 |
16+
| Array | 100 | 10242 ns | 4254 ns | 0.42 | 37264 B | 12192 B | 0.33 |
17+
| Array | 1000 | 101615 ns | 39642 ns | 0.39 | 363073 B | 120192 B | 0.33 |
1718
| Column | 1 | 201 ns | 91 ns | 0.45 | 536 B | 368 B | 0.69 |
1819
| Column | 100 | 1203 ns | 823 ns | 0.68 | 4024 B | 2592 B | 0.64 |
1920
| Column | 1000 | 9772 ns | 6626 ns | 0.68 | 32864 B | 17072 B | 0.52 |
@@ -23,12 +24,12 @@ DateTime, Guid, strings, three of them nullable).
2324
| Execute | 1 | 66 ns | 37 ns | 0.56 | 136 B | 168 B | 1.24 |
2425
| Execute | 100 | 70 ns | 38 ns | 0.55 | 136 B | 168 B | 1.24 |
2526
| Execute | 1000 | 67 ns | 38 ns | 0.56 | 136 B | 168 B | 1.24 |
26-
| First | 1 | 281 ns | 105 ns | 0.37 | 576 B | 312 B | 0.54 |
27-
| First | 100 | 352 ns | 103 ns | 0.29 | 576 B | 312 B | 0.54 |
28-
| First | 1000 | 866 ns | 102 ns | 0.12 | 576 B | 312 B | 0.54 |
29-
| List | 1 | 314 ns | 117 ns | 0.37 | 856 B | 400 B | 0.47 |
30-
| List | 100 | 10571 ns | 3248 ns | 0.31 | 36440 B | 13592 B | 0.37 |
31-
| List | 1000 | 106826 ns | 31179 ns | 0.29 | 355048 B | 128800 B | 0.36 |
27+
| First | 1 | 279 ns | 106 ns | 0.38 | 576 B | 280 B | 0.49 |
28+
| First | 100 | 349 ns | 107 ns | 0.31 | 576 B | 280 B | 0.49 |
29+
| First | 1000 | 876 ns | 107 ns | 0.12 | 576 B | 280 B | 0.49 |
30+
| List | 1 | 292 ns | 123 ns | 0.42 | 856 B | 368 B | 0.43 |
31+
| List | 100 | 11339 ns | 4030 ns | 0.36 | 36440 B | 13560 B | 0.37 |
32+
| List | 1000 | 100275 ns | 38959 ns | 0.39 | 355048 B | 128768 B | 0.36 |
3233
| Params | 1 | 334 ns | 153 ns | 0.46 | 1016 B | 752 B | 0.74 |
3334
| Params | 100 | 10410 ns | 3731 ns | 0.36 | 36600 B | 13944 B | 0.38 |
3435
| Params | 1000 | 108841 ns | 31949 ns | 0.29 | 355208 B | 129152 B | 0.36 |
@@ -66,6 +67,21 @@ DateTime, Guid, strings, three of them nullable).
6667
| ScalarAsync | 100 | 80 ns | 78 ns | 0.98 | 296 B | 312 B | 1.05 |
6768
| ScalarAsync | 1000 | 79 ns | 78 ns | 0.98 | 296 B | 312 B | 1.05 |
6869

70+
## Records (constructor binding)
71+
72+
The same 12-column `Post` as a positional record (`PostRecord`), read through its constructor by `Map<T>()`, against Dapper's constructor mapping (`RecordBenchmarks.cs`; re-measured 2026-11-08, master 41af17c).
73+
74+
| Scenario | Rows | Dapper | ODE | Ratio | Dapper alloc | ODE alloc | Alloc |
75+
|---|---:|---:|---:|---:|---:|---:|---:|
76+
| RecordFirst | 1 | 280 ns | 110 ns | 0.39 | 576 B | 280 B | 0.49 |
77+
| RecordFirst | 100 | 350 ns | 111 ns | 0.32 | 576 B | 280 B | 0.49 |
78+
| RecordFirst | 1000 | 889 ns | 112 ns | 0.13 | 576 B | 280 B | 0.49 |
79+
| RecordList | 1 | 322 ns | 124 ns | 0.39 | 856 B | 368 B | 0.43 |
80+
| RecordList | 100 | 10910 ns | 4339 ns | 0.40 | 36440 B | 13560 B | 0.37 |
81+
| RecordList | 1000 | 107866 ns | 40648 ns | 0.38 | 355048 B | 128768 B | 0.36 |
82+
83+
A record allocates exactly what the same model as a class allocates (13,560 B for 100 rows), so constructor binding costs no memory.
84+
6985
## Reading the table
7086

7187
- **Speed:** ODE is at or below Dapper's time in all 54 rows (worst: async scalar at 0.98, i.e. a tie). Mapped results are 2-3x faster at 100+ rows and 2-9x faster for `First`/`Single`.

‎docsite/articles/whats-new-11.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Version 11 is a performance release and a tidy-up release. Result mapping is rew
77

88
## Highlights
99

10-
- **Faster, allocation-light mapping.** The mapping for a result-set shape is compiled once and cached, and values are read with typed getters, so nothing is boxed on the common path. Before, every `Map<T>()` compiled a setter per property (about 0.94 ms per query in the library's benchmark). In that benchmark (in-memory provider, 12-column rows, Dapper 2.1.89), reading 100 or more rows into a list, array or stream takes 0.29 to 0.32 times Dapper's time and 0.33 to 0.37 times its allocations (the library's own benchmark, `Benchmarks/RESULTS.md`; the independent preview01 re-run quoted in the README and [Performance](performance.md) is more conservative: 2 to 3 times faster, 0.33 to 0.38 of the allocations). Against SQL Server LocalDB the database dominates: times are 0.92 to 1.04 times Dapper's in that benchmark (0.89 to 1.08 in the re-run), within the noise, and allocations 0.55 to 0.6 times on multi-row results.
10+
- **Faster, allocation-light mapping.** The mapping for a result-set shape is compiled once and cached, and values are read with typed getters, so nothing is boxed on the common path. Before, every `Map<T>()` compiled a setter per property (about 0.94 ms per query in the library's benchmark). In that benchmark (in-memory provider, 12-column rows, Dapper 2.1.89), reading 100 or more rows into a list or array takes 0.36 to 0.42 times Dapper's time (2.4 to 2.8 times faster) and 0.33 to 0.37 times its allocations (`Benchmarks/RESULTS.md`, re-measured on the release candidate; a positional record costs the same). Against SQL Server LocalDB the database dominates: times are 0.92 to 1.04 times Dapper's in that benchmark (0.89 to 1.08 in the re-run), within the noise, and allocations 0.55 to 0.6 times on multi-row results.
1111
- **`RowMap<T>`: declare a column mapping once.** `public static RowMap<Post> PostMap => field ??= [new(nameof(Post.Id), "post_id"), ...];`, passed as the `map` argument of every method that maps rows to a model. It takes one pair, several or none, and a read through it allocates what `Map<T>()` allocates. With no map, a column matches a property exactly, else ignoring case, else ignoring case and underscores (`first_name` fills `FirstName`). Only public instance properties are mapped. See [Columns named differently](mapping.md#columns-named-differently) and [snake_case columns map to PascalCase properties](mapping.md#snake_case-columns-map-to-pascalcase-properties).
1212
- **`MappedQuery<T>`: direct read loops, and one token for the whole call.** `Map<T>()` returns a deferred query. `ToList`, `ToArray`, `ToImmutableArray`, `ToHashSet`, `ToDictionary`, `FirstOrDefault`, `SingleOrDefault`, `Read` and their async twins read straight from the reader; `Skip` advances without mapping and `Take` stops reading once it is met. They are instance methods, so the query struct is not boxed; the `IMappedQuery<T>` extensions remain for generic code. The token passed to an async terminal is used together with the command's `UseCancellationToken` token, and either one cancels the whole call: opening the connection, looking up parameter names, executing and reading. Readers open with `SequentialAccess | SingleResult` (`Map(selector)` skips `SequentialAccess`, because a selector may read columns in any order). See [Terminals](expressive-commands.md#terminals).
1313
- **Less overhead around the query.** `IExecuteReader` has state-passing `ExecuteReader<TState, TResult>` overloads, so a `static` lambda allocates no closure. A command created from a connection allocates no pool wrapper, `Params` is created on first use, and async execution skips awaiting when the connection is already open.

0 commit comments

Comments
 (0)