From db98c94ba7fc8e57eb4b430b416525d58e1eaf94 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Wed, 7 Sep 2022 18:51:31 +0300 Subject: [PATCH] Optimize `IntrinsicFunctions.getTypeInfo`. It became generic, allowing it to be optimized by the JIT if it gets passed a value type, and `IsDefined` was used in certain cases instead of `GetCustomAttributes. --- src/FSharp.Core/prim-types.fs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/FSharp.Core/prim-types.fs b/src/FSharp.Core/prim-types.fs index 1178620d63b..a89932550da 100644 --- a/src/FSharp.Core/prim-types.fs +++ b/src/FSharp.Core/prim-types.fs @@ -601,20 +601,19 @@ namespace Microsoft.FSharp.Core // duplicated from above since we're using integers in this section let CompilationRepresentationFlags_PermitNull = 8 - let getTypeInfo (ty:Type) = - if ty.IsValueType + let private getTypeInfo<'T> = + if typeof<'T>.IsValueType then TypeNullnessSemantics_NullNever else - let mappingAttrs = ty.GetCustomAttributes(typeof, false) - if mappingAttrs.Length = 0 + if not (typeof<'T>.IsDefined(typeof, false)) then TypeNullnessSemantics_NullIsExtraValue - elif ty.Equals(typeof) then + elif typeof<'T>.Equals(typeof) then TypeNullnessSemantics_NullTrueValue - elif typeof.IsAssignableFrom(ty) then + elif typeof.IsAssignableFrom(typeof<'T>) then TypeNullnessSemantics_NullIsExtraValue - elif ty.GetCustomAttributes(typeof, false).Length > 0 then + elif typeof<'T>.IsDefined(typeof, false) then TypeNullnessSemantics_NullIsExtraValue else - let reprAttrs = ty.GetCustomAttributes(typeof, false) + let reprAttrs = typeof<'T>.GetCustomAttributes(typeof, false) if reprAttrs.Length = 0 then TypeNullnessSemantics_NullNotLiked else @@ -627,7 +626,7 @@ namespace Microsoft.FSharp.Core type TypeInfo<'T>() = // Compute an on-demand per-instantiation static field - static let info = getTypeInfo typeof<'T> + static let info = getTypeInfo<'T> // Publish the results of that computation static member TypeInfo = info