Code perf: optimize typeof equality in inlined code
- 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
Assessment
This issue has not been assessed yet.