Skip to content

Commit 8e3bb47

Browse files
authored
Minor perf opt: (#18541)
- isNull is inline also in FSharp.Core - Lazy load of ILAssemblyRefs - Avoid looping 120 libraries through on most cases
1 parent d67ac13 commit 8e3bb47

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

tests/service/data/TestTP/ProvidedTypes.fs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ open Microsoft.FSharp.Core.CompilerServices
2424
[<AutoOpen>]
2525
module Utils =
2626
let K x = (fun () -> x)
27-
let isNull x = match x with null -> true | _ -> false
28-
let isNil x = match x with [] -> true | _ -> false
29-
let isEmpty x = match x with [| |] -> true | _ -> false
27+
let inline isNull x = match x with null -> true | _ -> false
28+
let inline isNil x = match x with [] -> true | _ -> false
29+
let inline isEmpty x = match x with [| |] -> true | _ -> false
3030

3131
module Option =
3232
let toObj x = match x with None -> null | Some x -> x
@@ -6253,11 +6253,11 @@ module internal AssemblyReader =
62536253

62546254

62556255
let ilModule = seekReadModule (ilMetadataVersion) 1
6256-
let ilAssemblyRefs = [ for i in 1 .. getNumRows ILTableNames.AssemblyRef do yield seekReadAssemblyRef i ]
6256+
let ilAssemblyRefs = lazy [ for i in 1 .. getNumRows ILTableNames.AssemblyRef do yield seekReadAssemblyRef i ]
62576257

62586258
member __.ILGlobals = ilg
62596259
member __.ILModuleDef = ilModule
6260-
member __.ILAssemblyRefs = ilAssemblyRefs
6260+
member __.ILAssemblyRefs = ilAssemblyRefs.Force()
62616261

62626262
let sigptr_get_byte (bytes: byte[]) sigptr =
62636263
int bytes[sigptr], sigptr + 1
@@ -8988,31 +8988,42 @@ namespace ProviderImplementation.ProvidedTypes
89888988
let asms = (if toTgt then getTargetAssemblies() else getSourceAssemblies())
89898989
let fullName = fixName t.FullName
89908990

8991-
// TODO: this linear search through all available source/target assemblies feels as if it must be too slow in some cases.
8992-
// However, we store type translations in various tables (typeTableFwd and typeTableBwd) so perhaps it is not a problem
8993-
let rec loop i =
8994-
if i < 0 then
8995-
let msg =
8996-
if toTgt then sprintf "The design-time type '%O' utilized by a type provider was not found in the target reference assembly set '%A'. You may be referencing a profile which contains fewer types than those needed by the type provider you are using." t (getTargetAssemblies() |> Seq.toList)
8997-
elif getSourceAssemblies() |> Seq.isEmpty then sprintf "A failure occurred while determining compilation references"
8998-
else sprintf "The target type '%O' utilized by a type provider was not found in the design-time assembly set '%A'. Please report this problem to the project site for the type provider." t (getSourceAssemblies() |> Seq.toList)
8999-
failwith msg
9000-
else
9001-
match tryGetTypeFromAssembly toTgt t.Assembly.FullName fullName asms[i] with
9002-
| Some (newT, canSave) ->
9003-
if canSave then table[t] <- newT
9004-
newT
9005-
| None -> loop (i - 1)
9006-
loop (asms.Count - 1)
8991+
let bestGuess =
8992+
asms |> Seq.tryFind(fun a -> a.FullName = t.Assembly.FullName)
8993+
|> Option.bind(fun a -> tryGetTypeFromAssembly toTgt t.Assembly.FullName fullName a)
8994+
8995+
match bestGuess with
8996+
| Some (newT, canSave) ->
8997+
if canSave then table.[t] <- newT
8998+
newT
8999+
| None ->
9000+
9001+
// TODO: this linear search through all available source/target assemblies feels as if it must be too slow in some cases.
9002+
// However, we store type translations in various tables (typeTableFwd and typeTableBwd) so perhaps it is not a problem
9003+
let rec loop i =
9004+
if i < 0 then
9005+
let msg =
9006+
if toTgt then sprintf "The design-time type '%O' utilized by a type provider was not found in the target reference assembly set '%A'. You may be referencing a profile which contains fewer types than those needed by the type provider you are using." t (getTargetAssemblies() |> Seq.toList)
9007+
elif getSourceAssemblies() |> Seq.isEmpty then sprintf "A failure occurred while determining compilation references"
9008+
else sprintf "The target type '%O' utilized by a type provider was not found in the design-time assembly set '%A'. Please report this problem to the project site for the type provider." t (getSourceAssemblies() |> Seq.toList)
9009+
failwith msg
9010+
else
9011+
match tryGetTypeFromAssembly toTgt t.Assembly.FullName fullName asms[i] with
9012+
| Some (newT, canSave) ->
9013+
if canSave then table[t] <- newT
9014+
newT
9015+
| None -> loop (i - 1)
9016+
loop (asms.Count - 1)
90079017

90089018
and convType toTgt (t:Type) =
90099019
let table = (if toTgt then typeTableFwd else typeTableBwd)
90109020
match table.TryGetValue(t) with
90119021
| true, newT -> newT
90129022
| false, _ ->
9013-
if t :? ProvidedTypeSymbol && (t :?> ProvidedTypeSymbol).IsFSharpTypeAbbreviation then t
9023+
let isSymbol = t :? ProvidedTypeSymbol
9024+
if isSymbol && (t :?> ProvidedTypeSymbol).IsFSharpTypeAbbreviation then t
90149025
// Types annotated with units-of-measure
9015-
elif t :? ProvidedTypeSymbol && (t :?> ProvidedTypeSymbol).IsFSharpUnitAnnotated then
9026+
elif isSymbol && (t :?> ProvidedTypeSymbol).IsFSharpUnitAnnotated then
90169027
let genericType = t.GetGenericTypeDefinition()
90179028
let newT = convTypeRef toTgt genericType
90189029
let typeArguments = t.GetGenericArguments() |> Array.map (convType toTgt) |> Array.toList

0 commit comments

Comments
 (0)