dotnet / dotnet/fsharp

Code perf: optimize typeof equality in inlined code

Open
#6,228 3 comments 4 reactions 0 assignees View on GitHub
Area-Compiler-Optimization Feature Improvement Theme-Performance
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

This is is recording a potential simple to our optimizations in Optimizer.fs

The .NET CLRs (both Framework and .NET Core) have an optimization where code like

if LanguagePrimitives.PhysicalEquality typeof<'T> typeof then
...
elif LanguagePrimitives.PhysicalEquality typeof<'T> typeof then
...

is optimized if the JIT is specialising the code for a struct type.

Here is a gist showing its use from F#: https://sharplab.io/#gist:3980a4de89ed3dc1d0d86d4120df2cbc. (UNfortunately the ASM is only shown for x86 desktop, I'd like to see it on RyuJIT for .NET Core 3.0 too). The construct `(# "" x : int #)` is being used to get an unsafe value coercion (as in prim-types.fs)

We should do the same optimization in the F# compiler itself when we inline and type-specialize code. It should be a simple addition to Optimizer.fs. This will provide a hook for numerics libraries that allow instantiation on a limited set of scalar types to an important range of optimizations.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.