fsharp-performance-types
Skill23 starsChanged 32 days agoMITView on GitHub
Choose F# collections and types for run time and allocation, and prove the result with a measurement. Use when F# code is slow or allocates too much, when a reviewer asks for fewer passes or allocations, or when you write a hot loop over list, array, seq, Map, Set, Dictionary, option, tuple, or sprintf.
In this repository16 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- fsharp-async-compositionSkill
- fsharp-binary-compatibilitySkill
- fsharp-env-capabilitiesSkill
- fsharp-failure-pathsSkill
- fsharp-idiom-reviewSkill
- fsharp-iwsam-errorsSkill
- fsharp-library-api-designSkill
- fsharp-native-aotSkill
- fsharp-nullness-interopSkill
- fsharp-serializationSkill
- fsharp-signature-filesSkill
- fsharp-verify-apiSkill
- optimize-fsharp-typecheck-graphSkill
- parse-dont-validateSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
14 rules. About 0 are common, 2 are distinctive, and 12 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
12
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 14
Use this skill only for code that a measurement shows to be hot, or that a reviewer or task names as hot.
- Very specific: line 21
Do not enumerate a seq more than once.
- Very specific: line 22
Do not use @ or List.append in a loop.
- Very specific: line 24
Keep the order that the contract defines.
- Very specific: line 31
Use Array.Parallel only when the work for each element is large compared with the cost of a task, and measure it.
- Very specific: line 36
Use struct (a, b) for a tuple that a non-inlined function returns in a hot loop, and for a Dictionary key.
- Very specific: line 37
Do not use a struct tuple as a sortBy key.
- Very specific: line 38
Use [<Struct>] for a small record or union of a few primitive fields.
- Very specific: line 42
Do not allocate on each element in a hot loop: no Some, tuple, closure, list cell, substring, or boxed value for each element.
- Very specific: line 43
Do not call sprintf in a hot loop.
- Very specific: line 44
Do not build a string with + in a loop or a fold.
- Very specific: line 45
Use inline collection functions such as Array.sumBy instead of Array.filter |> Array.length.
- Very specific: line 57
Check that the output is the same as before on the same inputs, under a culture with a comma decimal separator such as de-DE, with strings …
- Distinctive: line 62
Run the proof script from the skill root:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
F# performance types
Agents write F# collection code that makes several passes, allocates on each element, or uses a tree where a hash table fits. The compiler accepts this code. Human reviewers then ask for changes. Review evidence ranks collection efficiency among the strongest categories of human comments on agent F# code.
For readable shape (records, match, no let mutable accumulators), use fsharp-idiom-review. This skill does not repeat it.
Use this skill only for code that a measurement shows to be hot, or that a reviewer or task names as hot. Outside a hot path, prefer the readable form.
Rules
Collections. references/COLLECTIONS.md has the choice table and examples.
- Iterate an F#
listwithfor,List.fold, or pattern matching. Do not index it, and do not copy it to an array only to loop over it. - Do not enumerate a
seqmore than once. Each enumeration runs the source again. Materialize it once, or compute all results in one pass. - Do not use
@orList.appendin a loop. Cons onto the head and callList.revonce, or fill aResizeArray. - In a hot loop that updates a table, use a local
DictionaryorHashSet.Map.addallocates a new tree path on each update, andMap.tryFindallocates aSomeon each hit. Return an immutable result at the end if the contract requires one. ADictionaryuses .NET equality: passHashIdentity.Structuralwhen the key needs F# structural equality, for example an array key. - Keep the order that the contract defines.
List.sort,Array.sort,Map, andSetorder strings by ordinal.Array.Sort,List<T>.Sort, andSortedDictionaryuse the current culture unless you passStringComparer.Ordinal. - For a lookup table that is built once and read many times, use
FrozenDictionaryorFrozenSet.
Single pass.
- Replace a
filter |> map |> lengthchain over large input with onefoldor one loop that computes every result. - Track the previous value in the loop instead of
List.pairwiseorSeq.windowed, which allocate a tuple or an array for each element. - Use
Array.Parallelonly when the work for each element is large compared with the cost of a task, and measure it.
Value types. references/VALUE-TYPES.md has the costs and the exceptions.
- In a hot loop, return
voptioninstead ofoption, and mark a partial active pattern[<return: Struct>]. - Use
struct (a, b)for a tuple that a non-inlined function returns in a hot loop, and for aDictionarykey. A reference tuple key allocates on each lookup. - Do not use a struct tuple as a
sortBykey. F# generic comparison boxes it on each comparison. UsesortWithwithString.CompareOrdinalorcompareon each field. - Use
[<Struct>]for a small record or union of a few primitive fields. Do not use a struct for a large value, for a value stored asobjor an interface, or for a value that you change through a copy.
Allocation.
- Do not allocate on each element in a hot loop: no
Some, tuple, closure, list cell, substring, or boxed value for each element. - Do not call
sprintfin a hot loop. UseStringBuilder.AppendorString.ConcatwithToString(format, CultureInfo.InvariantCulture).sprintfand%.3fholes use the invariant culture.ToString("F3")and{x:F3}holes use the current culture, so the output changes on a machine with a comma decimal separator. - Do not build a string with
+in a loop or a fold. Use oneStringBuilder. - Use
inlinecollection functions such asArray.sumByinstead ofArray.filter |> Array.length. Mark your own small higher-order helperinlineonly when a measurement shows the closure cost.
Measurement. references/MEASURE.md has a harness and a BenchmarkDotNet setup.
- Measure allocated bytes with
GC.GetAllocatedBytesForCurrentThread()around one warm call in a Release build. Measure time as the median of several runs. - Compare before and after on the same input size as production. Report the numbers, not a claim.
Steps
- Find the hot functions. Build a production-size input and measure the allocated bytes and the median time of one call of each. Done when you have a number for each function.
- List each extra pass, each repeated
seqenumeration, each@or index in a loop, eachMaporSetupdate in a loop, and each allocation for each element. - Rewrite each item with the rules above. Keep the public types and signatures.
- Check that the output is the same as before on the same inputs, under a culture with a comma decimal separator such as
de-DE, with strings that differ only in case, and with a single-0.0value. - Measure again with the same harness. Report the numbers before and after. If an item does not change the numbers, revert it to the readable form.
Verify
Run the proof script from the skill root:
pwsh ./scripts/run.ps1The script runs in dotnet fsi --optimize+. It measures the allocation, order, and culture claims of this skill with GC.GetAllocatedBytesForCurrentThread(), a run count, or a time ratio.
It fails when a measured result differs from the claim. The FrozenDictionary, ImmutableArray, and Array.Parallel advice comes from the .NET documentation, and the script does not measure it.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

