A small library for compiling code quotations using the F# compiler service. Its primary functionality is transforming quotation trees to untyped ASTs used by the F# compiler. Since code is generated using the F# compiler proper, the end result is fully efficient and optimized.
Package available on NuGet.
#r "QuotationCompiler.dll" open QuotationCompiler let hello : unit -> unit = QuotationCompiler.ToFunc <@ printfn "Hello, world!" @> hello ()
As can be expected, performance exceeds all other quotation evaluation libraries. Here is a benchmark for a tail recursive algorithm:
[<ReflectedDefinition>] let sqrt (x : float) = let epsilon = 1e-10 let rec approximate (y : float) = let y' = (y + x / y) / 2. if abs (y - y') < epsilon then y' else approximate y' approximate x
|Library||Compilation time (cold)||Compilation time (warm)|
Executing the compiled functions 10^6 times produced the following results:
|Library||Execution time||GC gen0,1,2|
The library currently has a few limitations
- No support for dynamic assemblies. Quotations inherently belong to the runtime and ASTs are inherently static. It is impossible to compile quotations that reference types/code defined in F# interactive.
- F# metadata issues. Things may break when referencing generic values, extension methods or CompiledName types.
master), Build & Unit tests