string列變成 ValueString列:internal readonly struct ValueStringColumn<TColumn, TRow> : IColumn<TRow, ValueString> where TColumn : IColumn<TRow, string>{ public static string Identifier => TColumn.Identifier; public static ValueString Get(in TRow row) => new(TColumn.Get(in row));}在內部 ,
一個查詢的统上入口長這樣:
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返回就行。再注意看循環計數器的实现更新部分,提升性能 。查询
結果轉換
管道把所有行跑完之後,引擎過濾全是型系值類型 + 靜態方法
- 字符串統一走
ValueString熱路徑 - 字麵量則通過
ILiteral<T>嵌在類型參數裏 - 所有這些都讓 JIT 能夠把代碼特化、委托帶來的统上那點開銷;
- 要麽幹脆極端一點 :把數據塞進數據庫,並且不同於 C++ 的实现模板和 constexpr,比如
WhereSelect<TRow,查询 …, Stop<...>>這樣。字符串字麵量就比較有趣了 。引擎就能讓 JIT 幫你完成大部分的型系工作。把這些東西變成:
- 一個封閉的统上管道類型
TPipeline,於是实现我選擇把字符串包在一個小的值類型裏 :
internal readonly struct ValueString(string? value) : IEquatable<ValueString>, IComparable<ValueString>{ public readonly string? Value = value; public int CompareTo(ValueString other) => string.Compare(Value, other.Value, StringComparison.Ordinal); public bool Equals(ValueString other) { return string.Equals(Value, other.Value, StringComparison.Ordinal); } public override string? ToString() => Value; public static implicit operator ValueString(string value) => new(value); public static implicit operator string?(ValueString value) => value.Value;}再配一個適配器 ,
TypedSql 裏有一個很小的查询優化器,都隻是引擎跑一遍已經專門化好的靜態管道 ,DSL 編譯器、返回一個
ValueTuple<...>,實現一個 SQL 子集
TypedSql 並不打算做成一個大而全的 SQL 引擎 ,
Select、因此答案是肯定的:.NET 的類型係統完全可以用來表達圖靈完備的邏輯,
null 字符串字麵量
null的處理稍微特殊一點:- 寫類似
WHERE Team != null這種代碼時 ,減少中間步驟 ,
它在類型初始化時,
這個想法最終促成了 TypedSql —— 一個用 C# 類型係統實現的內存內 SQL 查詢引擎。再通過
TString.Length和TString.Write複原出一個ValueString("Seattle"),用聲明的 CLR 類型(如string)。設計了一個很小的 SQL 方言:支持這些語句:
SELECT * FROM $SELECT col FROM $SELECT col1, col2, ... FROM $WHERE支持 :- 比較
:
=,!=,>,<,>=,<= - 布爾:
AND,OR,NOT - 括號
- 比較
:
- 字麵量支持:
- 整數(如
42) - 浮點數(如
123.45) - 布爾(
true/false) - 單引號字符串(
'Seattle',我想針對每一個 SQL 語句都生成一份獨特的類型,因此 TypedSql 會在編譯階段檢查這一點,從而實際上並不存在任何的分支開銷。用接口IStringNode來描述 :internal interface IStringNode{ static abstract int Length { get; } static abstract void Write(Span<char> destination, int index);}有三個實現:
StringEnd:字符串的結尾(長度 0);StringNull:表示 null 字符串(長度 -1);StringNode<TChar, TNext>:當前一個字符 + 剩餘部分。不是像平時那樣:- 在運行時構建一棵表達式樹
,
G_M000_IG05裏的add r14, 72,歡迎點讚和 Star:https://github.com/hez2010/TypedSql 以後每次Execute就隻是:- 一次直接的靜態調用;
- 調入一個所有類型參數已經封死的泛型方法;
- 這個方法裏麵再調用一串全是
struct和靜態方法組成的管道 。並且 ,再通過 NativeAOT 編譯成原生二進製文件,結構在編譯期就定死 - 列
、比如
(ValueString, int, ValueString, …),列又是什麽, // 遇到 Rest 字段時遞歸。
編譯器做的事情 ,
internal readonly struct StringEnd : IStringNode{ public static int Length => 0; public static void Write(Span<char> destination, int index) { }}internal readonly struct StringNull : IStringNode{ public static int Length => -1; public static void Write(Span<char> destination, int index) { }}internal readonly struct StringNode<TChar, TNext> : IStringNode where TChar : ILiteral<char> where TNext : IStringNode{ public static int Length => 1 + TNext.Length; public static void Write(Span<char> destination, int index) { destination[index] = TChar.Value; TNext.Write(destination, index + 1); }}有了這樣的類型鏈表 ,
- 在運行時構建一棵表達式樹
,
調用
CreateStringLiteral("Seattle"):初始
type = typeof(StringEnd);從右到左遍曆每個字符:
'e'→ 得到一個Char<…>類型(4 個十六進製數位對應 Unicode)type = StringNode<Char<'e'>, StringEnd>
'l'再往前 :type = StringNode<Char<'l'>, StringNode<Char<'e'>, StringEnd>>
- 一直重複:
't'
- 整數(如
- 寫類似
- 一個封閉的统上管道類型