Skip to content

Commit 5514d18

Browse files
authored
Avoid double lookups, use isNull instead of = null (#489)
1 parent 5829eaf commit 5514d18

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

src/FSharp.Data.GraphQL.Client.DesignTime/ProvidedTypesHelper.fs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -497,15 +497,15 @@ module internal Provider =
497497
let rootWrapper = generateWrapper "Types"
498498
wrappersByPath.Add([], rootWrapper)
499499
let rec getWrapper (path : FieldStringPath) =
500-
if wrappersByPath.ContainsKey path
501-
then wrappersByPath.[path]
502-
else
500+
match wrappersByPath.TryGetValue path with
501+
| true, wrappedPath -> wrappedPath
502+
| false, _ ->
503503
let wrapper = generateWrapper (path.Head.FirstCharUpper() + "Fields")
504504
let upperWrapper =
505505
let path = path.Tail
506-
if wrappersByPath.ContainsKey(path)
507-
then wrappersByPath.[path]
508-
else getWrapper path
506+
match wrappersByPath.TryGetValue path with
507+
| true, wpath -> wpath
508+
| false, _ -> getWrapper path
509509
upperWrapper.AddMember(wrapper)
510510
wrappersByPath.Add(path, wrapper)
511511
wrapper
@@ -528,9 +528,9 @@ module internal Provider =
528528
then TypeMapping.makeOption providedTypes.[path, tref.Name.Value]
529529
else
530530
let getIntrospectionFields typeName =
531-
if schemaTypes.ContainsKey(typeName)
532-
then schemaTypes.[typeName].Fields |> Option.defaultValue [||]
533-
else failwith $"""Could not find a schema type based on a type reference. The reference is to a "%s{typeName}" type, but that type was not found in the schema types."""
531+
match schemaTypes.TryGetValue typeName with
532+
| true, schematype -> schematype.Fields |> Option.defaultValue [||]
533+
| false, _ -> failwith $"""Could not find a schema type based on a type reference. The reference is to a "%s{typeName}" type, but that type was not found in the schema types."""
534534
let getPropertyMetadata typeName (info : AstFieldInfo) : RecordPropertyMetadata =
535535
let ifield =
536536
match getIntrospectionFields typeName |> Array.tryFind(fun f -> f.Name = info.Name) with
@@ -564,9 +564,9 @@ module internal Provider =
564564
ProvidedRecord.makeProvidedType(tdef, baseProperties, explicitOptionalParameters)
565565
let createFragmentType (typeName, properties) =
566566
let itype =
567-
if schemaTypes.ContainsKey(typeName)
568-
then schemaTypes.[typeName]
569-
else failwithf "Could not find schema type based on the query. Type \"%s\" does not exist on the schema definition." typeName
567+
match schemaTypes.TryGetValue typeName with
568+
| true, schematype -> schematype
569+
| false, _ -> failwithf "Could not find schema type based on the query. Type \"%s\" does not exist on the schema definition." typeName
570570
let metadata : ProvidedTypeMetadata = { Name = itype.Name; Description = itype.Description }
571571
let tdef = ProvidedRecord.preBuildProvidedType(metadata, Some (upcast baseType))
572572
providedTypes.Add((path, typeName), tdef)
@@ -643,9 +643,9 @@ module internal Provider =
643643
|> makeOption
644644
| _ -> failwith "Could not find a schema type based on a type reference. The reference has an invalid or unsupported combination of Name, Kind and OfType fields."
645645
and resolveProvidedType (itype : IntrospectionType) : ProvidedTypeDefinition =
646-
if providedTypes.Value.ContainsKey(itype.Name)
647-
then providedTypes.Value.[itype.Name]
648-
else
646+
match providedTypes.Value.TryGetValue itype.Name with
647+
| true, ptval -> ptval
648+
| false, _ ->
649649
let metadata = { Name = itype.Name; Description = itype.Description }
650650
match itype.Kind with
651651
| TypeKind.OBJECT ->

src/FSharp.Data.GraphQL.Server.AspNetCore/GraphQLSubscriptionsManagement.fs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ let executeOnUnsubscribeAndDispose (id : SubscriptionId) (subscription : Subscri
1919
unsubscriber.Dispose ()
2020

2121
let removeSubscription (id : SubscriptionId) (subscriptions : SubscriptionsDict) =
22-
if subscriptions.ContainsKey (id) then
23-
subscriptions.[id] |> executeOnUnsubscribeAndDispose id
22+
match subscriptions.TryGetValue id with
23+
| true, sub ->
24+
sub |> executeOnUnsubscribeAndDispose id
2425
subscriptions.Remove (id) |> ignore
26+
| false, _ -> ()
2527

2628
let removeAllSubscriptions (subscriptions : SubscriptionsDict) =
2729
subscriptions

src/FSharp.Data.GraphQL.Shared/Helpers/Reflection.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module internal ReflectionHelper =
7979
let some =
8080
let createSome = optionType.GetDeclaredMethod "Some"
8181
fun value ->
82-
if value <> null
82+
if not (isNull value)
8383
then
8484
let valueType = value.GetType().GetTypeInfo()
8585
if valueType = optionType
@@ -91,7 +91,7 @@ module internal ReflectionHelper =
9191
let value =
9292
let x = optionType.GetDeclaredProperty "Value"
9393
fun input ->
94-
if input <> null
94+
if not (isNull input)
9595
then
9696
let valueType = input.GetType().GetTypeInfo()
9797
if valueType = optionType
@@ -113,7 +113,7 @@ module internal ReflectionHelper =
113113
let some =
114114
let createSome = optionType.GetDeclaredMethod "Some"
115115
fun value ->
116-
if value <> null
116+
if not (isNull value)
117117
then
118118
let valueType = value.GetType().GetTypeInfo()
119119
if valueType = optionType
@@ -125,7 +125,7 @@ module internal ReflectionHelper =
125125
let value =
126126
let x = optionType.GetDeclaredProperty "Value"
127127
fun input ->
128-
if input <> null
128+
if not (isNull input)
129129
then
130130
let valueType = input.GetType().GetTypeInfo()
131131
if valueType = optionType

src/FSharp.Data.GraphQL.Shared/SchemaDefinitions.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ module SchemaDefinitions =
169169

170170
/// Check if provided obj value is an Option and extract its wrapped value as object if possible
171171
let private (|Option|_|) (x : obj) =
172-
if x = null then None
172+
if isNull x then None
173173
else
174174
let t = x.GetType().GetTypeInfo()
175175
if t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<option<_>> then

src/FSharp.Data.GraphQL.Shared/TypeSystem.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,9 @@ and FieldExecuteMap (compiler : FieldExecuteCompiler) =
515515
/// <param name="fieldName">The field name of the object that has the field that needs to be executed.</param>
516516
member _.GetExecute (typeName : string, fieldName : string) =
517517
let key = getKey typeName fieldName
518-
if map.ContainsKey (key) then
519-
fst map.[key]
520-
else
518+
match map.TryGetValue key with
519+
| true, mapv -> fst mapv
520+
| false, _ ->
521521
Unchecked.defaultof<ExecuteField>
522522

523523
/// <summary>

0 commit comments

Comments
 (0)