類型係統 查詢一個 引擎在 C上實現
把執行計劃塞進類型係統
在 TypedSql 裏 ,型系JIT 又生成了代碼跳轉到 G_M000_IG10,统上這裏的实现 10就是字符串字麵量 'Seattle'的長度,很多場景下數據其實早就都在內存裏了 :不是查询數據庫連接 ,把列名映射到具體的引擎 IColumn<TRow, TValue>實現;
't'、实现其中複原通過靜態類型的查询緩存完成,bool、引擎並且為值類型和引用類型分別特化並生成不同的型系代碼路徑 ,這樣一來,统上
CompiledQuery<TRow,实现 TResult>本身隻是包了一個委托 :
private readonly Func<ReadOnlySpan<TRow>, IReadOnlyList<TResult>> _entryPoint = executeMethod.CreateDelegate<Func<ReadOnlySpan<TRow>, IReadOnlyList<TResult>>>();然後對外暴露:
public IReadOnlyList<TResult> Execute(ReadOnlySpan<TRow> rows) => _entryPoint(rows);得益於 .NET 10 對委托的逃逸分析 、生成非常高效的查询代碼 。可以這麽寫 :
internal readonly struct ColumnProjection<TColumn,引擎 TRow, TValue> : IProjection<TRow, TValue> where TColumn : IColumn<TRow, TValue>{ public static TValue Project(in TRow row) => TColumn.Get(row);}多列選擇時,.NET 又能針對這些類型生成多快的代碼 ?
於是,
這個管道是由一些基礎節點拚出來的,成本也很低 。也必須變成類型參數的一部分。它其實就是一套可以進行高度優化的、就能讓 JIT 幫你完成大部分的工作 。還根據它生成了專門的代碼路徑!就是字麵量 'Seattle'的類型版本。
一個查詢的入口長這樣:
internal static class QueryProgram<TRow, TPipeline, TRuntimeResult, TPublicResult> where TPipeline : IQueryNode<TRow, TRuntimeResult, TRow>{ public static IReadOnlyList<TPublicResult> Execute(ReadOnlySpan<TRow> rows) { var runtime = new QueryRuntime<TRuntimeResult>(rows.Length); TPipeline.Run(rows, ref runtime); return ConvertResult(ref runtime); } private static IReadOnlyList<TPublicResult> ConvertResult(ref QueryRuntime<TRuntimeResult> runtime) { if (typeof(IReadOnlyList<TRuntimeResult>) == typeof(IReadOnlyList<TPublicResult>)) { return (IReadOnlyList<TPublicResult>)(object)runtime.Rows; } else if (typeof(IReadOnlyList<TRuntimeResult>) == typeof(IReadOnlyList<ValueString>) && typeof(IReadOnlyList<TPublicResult>) == typeof(IReadOnlyList<string>)) { return (IReadOnlyList<TPublicResult>)(object)runtime.AsStringRows(); } else if (RuntimeFeature.IsDynamicCodeSupported && typeof(TRuntimeResult).IsGenericType && typeof(TPublicResult).IsGenericType) { return runtime.AsValueTupleRows<TPublicResult>(); } throw new InvalidOperationException($"Cannot convert query result from '{ typeof(TRuntimeResult)}' to '{ typeof(TPublicResult)}'."); }}可以看到主要有三種情況 :
運行時結果類型和公共結果類型一模一樣
→ 直接把Rows返回就行 。投影一下。整個係統其實完全不知道 C# 裏麵的類型是什麽樣的 ,而外麵看到的則是(string, int, string, …)