Skip to content

Change ty.Equals(typeof<'T>) to Type.op_Equality(typeof<'T>, ty) to please RyuJIT #8447

@NinoFloris

Description

@NinoFloris

There are some dynamic type checks here and there in FSharp.Core that all use Type.Equals instead of Type.op_Equality. Only the latter is optimized by the JIT for branch elimination (reference vs value type) and lookup table transformation for shared reference type checks. That's some easy free perf on the generic equality.

type GenericZeroDynamicImplTable<'T>() =
static let result : 'T =
// The dynamic implementation
let aty = typeof<'T>
if aty.Equals(typeof<sbyte>) then unboxPrim<'T> (box 0y)
elif aty.Equals(typeof<int16>) then unboxPrim<'T> (box 0s)
elif aty.Equals(typeof<int32>) then unboxPrim<'T> (box 0)
elif aty.Equals(typeof<int64>) then unboxPrim<'T> (box 0L)
elif aty.Equals(typeof<nativeint>) then unboxPrim<'T> (box 0n)
elif aty.Equals(typeof<byte>) then unboxPrim<'T> (box 0uy)
elif aty.Equals(typeof<uint16>) then unboxPrim<'T> (box 0us)
elif aty.Equals(typeof<uint32>) then unboxPrim<'T> (box 0u)
elif aty.Equals(typeof<uint64>) then unboxPrim<'T> (box 0UL)
elif aty.Equals(typeof<unativeint>) then unboxPrim<'T> (box 0un)
elif aty.Equals(typeof<decimal>) then unboxPrim<'T> (box 0M)
elif aty.Equals(typeof<float>) then unboxPrim<'T> (box 0.0)
elif aty.Equals(typeof<float32>) then unboxPrim<'T> (box 0.0f)

https://sharplab.io/#v2:DYLgZgzgNALiCGEC2AfA9gBwKYDsAEAygJ4QxZICwAUJroQBbwBOGAMvAEYB0ASgK44YASyRZq1GEWx4AwgAoAlHgC81POrwBtADwApITADiuLEyEBjOZOxow2oYIB8CxwF01G0vGHm8opBymhMBoAO7aAOQAKo6KKngeGhpI3ub0eNZYtpExeKEG6YlJeCgZRHn0dJJcAKIAjnzwwBBWUll2DjDOeAC0jngAjEVJpZIVVUS1DU0tmdkcaGjA3X14AEzDGqPloZX41fWNza02dhxEZCv9AMyb6qUA+r39ACx3CVRJOvpGJmaWcw6Thc7k+nhg3gsfnIgSYeAAYogYDlYkplB9islUulASi8gUMZiShlxvgom0uJgHocmgYiFYoGVTvZgc9Bu9RqS8OTsJSMNTpsA6Qyme1tAslld1hySbs6DysHyBUdhTBGbjzpclKtbmDMY82W8qEA=

Ideally I'd like to see a static optimization for (=) (x:Type, y:Type) to use Type.op_Equality @dsyme is this something you're ok with?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions