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
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static object ControlT20(IExecuteReader command)

// CtlH_T: the same reads as ReadT20, written by hand on DbDataReader as a `static` lambda (a closed delegate, as the generated reader is), entering through
// the GeneratedRowMap hook, so the entry (Map(RowMap)), the access mode and the delegate form are Gen_T's and only the generated code differs. The generated
// code also has two things this control leaves out, both read as part of a gap between them: its row reader is NoInlining, and its bind checks the
// code also has one thing this control leaves out, read as part of a gap between them: its bind checks the
// result's columns once per result set (a fixed cost per query, visible with few rows).
static readonly Func<System.Data.Common.DbDataReader, GenRowT20> ReadT20Db = static r => new()
{
Expand Down
8 changes: 4 additions & 4 deletions Benchmarks/Open.Database.Extensions.Benchmarks/GenParity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Open.Database.Extensions.Benchmarks;

// The ladder of the generated reader's row shape, at width 20, through the same Map(Func<IDataRecord, SynRow>) entry V2 uses (SyntheticVariants.V2).
// WHAT THE GENERATED READER DOES NOW: it enters through Map(RowMap) (Core's GeneratedRowMap hook); its row delegate is the reads and nothing else: a
// non-nullable member read UNCHECKED (no IsDBNull), a nullable member tested for NULL, NO try region; Core's loop reports a failure (one try/catch around
// the loop, as GP7 places it). The GP steps below are HISTORICAL steps toward V2n and toward former row shapes; they decompose the cost of those shapes,
// non-nullable member read UNCHECKED (no IsDBNull), a nullable member tested for NULL, NO try region; Core's loop reports a failure (a try around its
// call of the row reader). The GP steps below are HISTORICAL steps toward V2n and toward former row shapes; they decompose the cost of those shapes,
// not of today's Gen.
// V2n V2 plus IsDBNull on EVERY member (the former NULL rule: a NULL leaves the member's default), constant ordinals: the hand-written equal of a
// check-every-member reader (named GP1 in the first decomposition run). GP2 to GP5 are V2n plus one thing each.
Expand All @@ -29,8 +29,8 @@ namespace Open.Database.Extensions.Benchmarks;
// cache line if queries ran in parallel); the row a failure reports is cumulative across queries, not the row of its own query as the generated
// reader's per-binding counter gives. Nothing here checks the reported index.
// GP7 the raw loop (as V10r): ExecuteReader's callback, while (db.Read()) list.Add(Read(db)), V2's read at constant ordinals on DbDataReader,
// with ONE try/catch around the whole loop (the row number a loop local): the placement the generator now gets from Core's loop through
// the GeneratedRowMap hook. GP7 and GP7c open the reader with the default behavior (as V10r, V2 and every
// with ONE try/catch around the whole loop (the row number a loop local): a placement Core's loop had before it
// caught around the row reader's call alone. GP7 and GP7c open the reader with the default behavior (as V10r, V2 and every
// library read do), so GP7 against V2 or GP6 changes only the loop placement. GP7 against GP7c isolates the try.
// GP7c GP7 with no try: the bar of the loop placement.
//
Expand Down
69 changes: 12 additions & 57 deletions Source/Core/Core/RowErrors.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Globalization;

namespace Open.Database.Extensions;
Expand Down Expand Up @@ -55,77 +54,33 @@ InvalidCastException when singleValue
return new ValueMappingException(message, "", "", target, inner);
}

// The read loops' filter. A runtime row reader carries its own failure to the loop (RowMappingFailure), and lets only a DbException or a
// cancellation through. A generated row reader (GeneratedRowMap) has no try region at all, so whatever else comes out of a loop over a
// generated map may be its failure: Report decides, on the failure path only.
// The read loops' filter, around the map call alone, so what it sees was thrown by the row reader. A runtime row reader carries its own
// failure to the loop (RowMappingFailure), and lets only a DbException or a cancellation through. A generated row reader
// (GeneratedRowMap) has no try region at all: anything else it throws is its failure. A caller's selector is not reported.
/// <summary>
/// Returns whether a read loop over <paramref name="source"/> reports <paramref name="ex"/> for the row.
/// Returns whether a read loop over <paramref name="source"/> reports <paramref name="ex"/>, thrown by its row reader, for the row.
/// </summary>
/// <typeparam name="T">The type of a mapped row.</typeparam>
/// <param name="source">The mapping of the rows.</param>
/// <param name="ex">What the loop caught.</param>
/// <returns><see langword="true"/> to hand it to <see cref="Report{TReader, T}"/>; otherwise, <see langword="false"/> to let it through.</returns>
/// <param name="ex">What the row reader threw.</param>
/// <returns><see langword="true"/> to hand it to <see cref="Report{T}"/>; otherwise, <see langword="false"/> to let it through.</returns>
public static bool IsRowFailure<T>(IRowSource<T> source, Exception ex)
=> ex is RowMappingFailure
|| ex is not (DbException or OperationCanceledException) && source is RowMap<T> { IsGenerated: true };

/// <summary>
/// Returns <paramref name="map"/> if <paramref name="source"/> is a generated map; otherwise, <see langword="null"/>.
/// </summary>
// Only a generated reader's failure needs the reader to be known. A synchronous loop keeps this in a local of its own for its handler,
// so the handler does not read the loop's mapper (see RowReader.Fill).
public static Func<TReader, T>? GeneratedReader<TReader, T>(IRowSource<T> source, Func<TReader, T> map)
=> source is RowMap<T> { IsGenerated: true } ? map : null;

/// <summary>
/// Returns the exception a read loop throws for a failure <see cref="IsRowFailure{T}"/> accepted, or <see langword="null"/> to rethrow it unchanged.
/// Returns the exception a read loop throws for a failure <see cref="IsRowFailure{T}"/> accepted: it names the type and the row.
/// </summary>
/// <remarks>
/// A runtime row reader's failure names the row. Any other failure is the generated row reader's when it was thrown from inside that reader
/// (its own frame is on the exception's stack trace): it is reported as the runtime reports its own typed read, the model and the row, the
/// exception inside. A failure of the loop's own <c>Read</c> or of the collection it fills is not the row's, and is rethrown as it is.
/// A generated row reader's failure is reported as the runtime reports its own typed read: the model and the row, the exception inside.
/// Nothing is read from the reader here.
/// </remarks>
/// <typeparam name="TReader">The type of the reader.</typeparam>
/// <typeparam name="T">The type of a mapped row.</typeparam>
/// <param name="ex">What the loop caught.</param>
/// <param name="map">The row reader the loop was calling; may be <see langword="null"/> when the map is not generated.</param>
/// <param name="ex">What the row reader threw.</param>
/// <param name="row">The 0-based row in the result set.</param>
/// <returns>The exception to throw, or <see langword="null"/>.</returns>
public static Exception? Report<TReader, T>(Exception ex, Func<TReader, T>? map, int row)
=> ex is RowMappingFailure failure ? failure.At(row)
: map is not null && ThrownBy(ex, map.Method) ? Create(ex, typeof(T), false, row)
: null;

// Best effort, on the failure path only: the loop's try region also holds its Read and its Add, and only the row reader's own failure is
// attributed. The generated row readers are emitted NoInlining, so their frame is never folded into the loop by an inlining JIT.
// A stack trace without metadata (trimmed for NativeAOT) cannot tell: the failure is then attributed to the row.
/// <summary>
/// Returns whether <paramref name="method"/> is on the stack trace of <paramref name="ex"/>.
/// </summary>
static bool ThrownBy(Exception ex, MethodInfo method)
{
// A runtime-compiled reader (a declined layout) is not a generated one, and its method has no declaring type and may refuse its metadata token.
// Its own failures arrive as RowMappingFailure, so this one is the loop's.
if (method.DeclaringType is null) return false;

StackFrame[]? frames = new StackTrace(ex, false).GetFrames();
if (frames is null) return true;

Module module = method.Module;
int token = method.MetadataToken;
bool described = false;
foreach (StackFrame? frame in frames)
{
if (frame?.GetMethod() is not { } m) continue;

described = true;
// The module first: a frame of a compiled expression is in no module of the generated code.
if (m.Module == module && m.MetadataToken == token) return true;
}

return !described;
}
/// <returns>The exception to throw.</returns>
public static ValueMappingException Report<T>(Exception ex, int row)
=> ex is RowMappingFailure failure ? failure.At(row) : Create(ex, typeof(T), false, row);

/// <summary>
/// Returns the message for a NULL scalar read as a non-nullable value type.
Expand Down
Loading