Skip to content

Commit 3b22420

Browse files
authored
Merge pull request #417 from Thorium/designtime-perf-opt
Minor perf opt
2 parents 23b588d + 1f9af4a commit 3b22420

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

src/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
@@ -9120,31 +9120,42 @@ namespace ProviderImplementation.ProvidedTypes
91209120
let asms = (if toTgt then getTargetAssemblies() else getSourceAssemblies())
91219121
let fullName = fixName t.FullName
91229122

9123-
// TODO: this linear search through all available source/target assemblies feels as if it must be too slow in some cases.
9124-
// However, we store type translations in various tables (typeTableFwd and typeTableBwd) so perhaps it is not a problem
9125-
let rec loop i =
9126-
if i < 0 then
9127-
let msg =
9128-
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)
9129-
elif getSourceAssemblies() |> Seq.isEmpty then sprintf "A failure occured while determining compilation references"
9130-
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)
9131-
failwith msg
9132-
else
9133-
match tryGetTypeFromAssembly toTgt t.Assembly.FullName fullName asms.[i] with
9134-
| Some (newT, canSave) ->
9135-
if canSave then table.[t] <- newT
9136-
newT
9137-
| None -> loop (i - 1)
9138-
loop (asms.Count - 1)
9123+
let bestGuess =
9124+
asms |> Seq.tryFind(fun a -> a.FullName = t.Assembly.FullName)
9125+
|> Option.bind(fun a -> tryGetTypeFromAssembly toTgt t.Assembly.FullName fullName a)
9126+
9127+
match bestGuess with
9128+
| Some (newT, canSave) ->
9129+
if canSave then table.[t] <- newT
9130+
newT
9131+
| None ->
9132+
9133+
// TODO: this linear search through all available source/target assemblies feels as if it must be too slow in some cases.
9134+
// However, we store type translations in various tables (typeTableFwd and typeTableBwd) so perhaps it is not a problem
9135+
let rec loop i =
9136+
if i < 0 then
9137+
let msg =
9138+
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)
9139+
elif getSourceAssemblies() |> Seq.isEmpty then sprintf "A failure occured while determining compilation references"
9140+
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)
9141+
failwith msg
9142+
else
9143+
match tryGetTypeFromAssembly toTgt t.Assembly.FullName fullName asms.[i] with
9144+
| Some (newT, canSave) ->
9145+
if canSave then table.[t] <- newT
9146+
newT
9147+
| None -> loop (i - 1)
9148+
loop (asms.Count - 1)
91399149

91409150
and convType toTgt (t:Type) =
91419151
let table = (if toTgt then typeTableFwd else typeTableBwd)
91429152
match table.TryGetValue(t) with
91439153
| true, newT -> newT
91449154
| false, _ ->
9145-
if t :? ProvidedTypeSymbol && (t :?> ProvidedTypeSymbol).IsFSharpTypeAbbreviation then t
9155+
let isSymbol = t :? ProvidedTypeSymbol
9156+
if isSymbol && (t :?> ProvidedTypeSymbol).IsFSharpTypeAbbreviation then t
91469157
// Types annotated with units-of-measure
9147-
elif t :? ProvidedTypeSymbol && (t :?> ProvidedTypeSymbol).IsFSharpUnitAnnotated then
9158+
elif isSymbol && (t :?> ProvidedTypeSymbol).IsFSharpUnitAnnotated then
91489159
let genericType = t.GetGenericTypeDefinition()
91499160
let newT = convTypeRef toTgt genericType
91509161
let typeArguments = t.GetGenericArguments() |> Array.map (convType toTgt) |> Array.toList

0 commit comments

Comments
 (0)