Unpickle a quotation literal once, not on every evaluation
Maintainer antworten meist innerhalb von 3 Tagen
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 35/100
- Issue-Typ
- Feature
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- fsharp
- Bereich
- compilers
Rechercherichtung
Start by reading IlxGen.GenQuotation, Expr.Deserialize40, unpickleExpr, and fillHolesInRawExpr; the issue identifies these entry points and describes the repeated work. The proposed cache key and static field approach are examples, and the issue notes unresolved questions about generic instantiations and bound variable identity. Done means quotation literals reuse an unpickled tree while still filling captured values correctly, with benchmarks showing the cost change.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Is your feature request related to a problem? Please describe.
Every evaluation of a quotation literal costs tens of microseconds and tens of kilobytes, however often the same literal was evaluated before. A library that takes quotations as its input (a query translator, in my case) cannot avoid that with a cache of its own: it receives the tree only after it is built.
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open FSharp.Quotations
type Person = { Name : string; Age : int; City : string; Nick : string option; Tags : string list }
[<MemoryDiagnoser>]
type QuotationLiteral () =
let minAge = 18
let city = "Kyiv"
// Evaluated once: reading it afterwards costs nothing
let held : Expr<Person -> bool> = <@ fun p -> p.Age >= 18 @>
[<Benchmark(Description = "One condition, no captured value, held in a field")>]
member _.Held () : Expr<Person -> bool> = held
[<Benchmark(Description = "One condition, no captured value")>]
member _.Closed () : Expr<Person -> bool> = <@ fun p -> p.Age >= 18 @>
[<Benchmark(Description = "One condition, one captured value")>]
member _.OneCondition () : Expr<Person -> bool> = <@ fun p -> p.Age >= minAge @>
[<Benchmark(Description = "Four conditions, two captured values")>]
member _.FourConditions () : Expr<Person -> bool> =
<@ fun p -> p.Age >= minAge && p.City = city && p.Nick.IsSome && List.contains "vip" p.Tags @>
[<EntryPoint>]
let main arguments =
BenchmarkSwitcher.FromAssembly(typeof<QuotationLiteral>.Assembly).Run arguments |> ignore
0
| Method | Mean | Allocated |
|---|---|---|
| One condition, no captured value, held in a field | 0.5 ns | 0 |
| One condition, no captured value | 14.7 µs | 19.9 KB |
| One condition, one captured value | 22.0 µs | 20.5 KB |
| Four conditions, two captured values | 116 µs | 51.1 KB |
BenchmarkDotNet 0.15.8, .NET 10.0.12, FSharp.Core 10.1.401 (SDK 10.0.401), Windows 11 x64, Release. The machine was not idle, so the times are approximate (the last one has a standard deviation of 37 µs); the allocations are exact.
Where it goes, as far as I read the code at a274432:
IlxGen.GenQuotationpickles the tree at compile time and emits, in place of the literal, a call ofExpr.Deserialize40 (type, referencedTypes, spliceTypes, spliceExprs, bytes). The bytes are a new array on every evaluation (newarrandRuntimeHelpers.InitializeArray).Deserialize40runsunpickleExprover the bytes on every call and thenfillHolesInRawExprwith the spliced expressions, which are the captured values.- Unpickling resolves every type, method and property through reflection again;
bindMethodHelper, for one, callsType.GetMethod (name, flags, null, argTypes, null). - Nothing is kept between two evaluations. Only the bodies of
[<ReflectedDefinition>]members have a table (reflectedDefinitionTable).
Describe the solution you'd like
Unpickle a literal once and keep the tree with its holes. A later evaluation only fills the holes, and a literal without splices can give back the same instance.
The tree with holes depends on the pickled bytes, the referenced type definitions and the type splices, not on the spliced expressions. FSharp.Core has no key to cache by today, because the byte array is a new one each time, so the compiler would have to provide one: for example a static field per literal that holds the unpickled tree, passed to a new entry point next to Deserialize40. A literal with type splices, one inside generic code, needs one tree per instantiation.
I have not prototyped this, so I cannot say how much of the cost it removes. One thing to check is the identity of bound variables: two evaluations of one literal would share their Var objects, as two uses of one quotation value do today.
Describe alternatives you've considered
- Holding the quotation in a value that is evaluated once and passing what changes as arguments of the quoted lambda (the first row of the table). That works where the author of the quotation can restructure the code, not for the caller of a library who writes the quotation at the call.
- A cache inside the library that receives the quotation. It comes too late: the cost is paid before the library is called.
Additional context
Found while measuring the cost model of a quotation-based query translator for Azure Cosmos DB in fsprojects/FSharp.Azure.Cosmos. There a query round trip to the local emulator takes about 2 ms and everything the library does per cached query about 1 µs, so the literal is by far the largest cost on the client.
- Vorherrschende Sprache
- F#
- Sterne
- 4.3k
- Forks
- 881
- Ø Merge
- 3 T. 20 Std.
- Gemergte PRs (30 T.)
- 108
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus dotnet/fsharp
-
Documentation: Array.tryPick code example gives an errorEvtl. vergeben @T-Gro hat das vor 1 Tag übernommen. OffenArea-XmlDocs
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 78/100
dotnet/fsharp#20780 · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 3 Tagen
-
Semantic classification cache for opened documents is never populated (written to the unopened-documents cache)Evtl. vergeben @xperiandri hat das vor 43 Tagen übernommen. OffenNeeds-Triage
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
Maintainer antworten meist innerhalb von 3 Tagen
-
Needs-Triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
dotnet/fsharp#20265 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 3 Tagen
-
Bug Needs-Triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 3 Tagen
-
Visual Studio F# Rename corrupts record update expressionsEvtl. vergeben @T-Gro hat das vor 1 Tag übernommen. OffenArea-LangService-RenameSymbol Bug
dotnet/fsharp#20791 · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 3 Tagen
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag
-
A binding pattern element named `from` or `as` fails with "Expected destructuring pattern": `const [from] = [1]`, `const { a: from } = o`, `([from]) => from`Evtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offenbug engine spec compliance
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
frostney/GocciaScript#1737 ·
Maintainer antworten meist innerhalb von 1 Tag
-
build enhancement medium priority
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
bug frontend rt sev-3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100