Skip to content

Commit

Permalink
Avoid double lookups, use isNull instead of = null (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorium authored Sep 2, 2024
1 parent 5829eaf commit 5514d18
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
30 changes: 15 additions & 15 deletions src/FSharp.Data.GraphQL.Client.DesignTime/ProvidedTypesHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -497,15 +497,15 @@ module internal Provider =
let rootWrapper = generateWrapper "Types"
wrappersByPath.Add([], rootWrapper)
let rec getWrapper (path : FieldStringPath) =
if wrappersByPath.ContainsKey path
then wrappersByPath.[path]
else
match wrappersByPath.TryGetValue path with
| true, wrappedPath -> wrappedPath
| false, _ ->
let wrapper = generateWrapper (path.Head.FirstCharUpper() + "Fields")
let upperWrapper =
let path = path.Tail
if wrappersByPath.ContainsKey(path)
then wrappersByPath.[path]
else getWrapper path
match wrappersByPath.TryGetValue path with
| true, wpath -> wpath
| false, _ -> getWrapper path
upperWrapper.AddMember(wrapper)
wrappersByPath.Add(path, wrapper)
wrapper
Expand All @@ -528,9 +528,9 @@ module internal Provider =
then TypeMapping.makeOption providedTypes.[path, tref.Name.Value]
else
let getIntrospectionFields typeName =
if schemaTypes.ContainsKey(typeName)
then schemaTypes.[typeName].Fields |> Option.defaultValue [||]
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."""
match schemaTypes.TryGetValue typeName with
| true, schematype -> schematype.Fields |> Option.defaultValue [||]
| 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."""
let getPropertyMetadata typeName (info : AstFieldInfo) : RecordPropertyMetadata =
let ifield =
match getIntrospectionFields typeName |> Array.tryFind(fun f -> f.Name = info.Name) with
Expand Down Expand Up @@ -564,9 +564,9 @@ module internal Provider =
ProvidedRecord.makeProvidedType(tdef, baseProperties, explicitOptionalParameters)
let createFragmentType (typeName, properties) =
let itype =
if schemaTypes.ContainsKey(typeName)
then schemaTypes.[typeName]
else failwithf "Could not find schema type based on the query. Type \"%s\" does not exist on the schema definition." typeName
match schemaTypes.TryGetValue typeName with
| true, schematype -> schematype
| false, _ -> failwithf "Could not find schema type based on the query. Type \"%s\" does not exist on the schema definition." typeName
let metadata : ProvidedTypeMetadata = { Name = itype.Name; Description = itype.Description }
let tdef = ProvidedRecord.preBuildProvidedType(metadata, Some (upcast baseType))
providedTypes.Add((path, typeName), tdef)
Expand Down Expand Up @@ -643,9 +643,9 @@ module internal Provider =
|> makeOption
| _ -> 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."
and resolveProvidedType (itype : IntrospectionType) : ProvidedTypeDefinition =
if providedTypes.Value.ContainsKey(itype.Name)
then providedTypes.Value.[itype.Name]
else
match providedTypes.Value.TryGetValue itype.Name with
| true, ptval -> ptval
| false, _ ->
let metadata = { Name = itype.Name; Description = itype.Description }
match itype.Kind with
| TypeKind.OBJECT ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ let executeOnUnsubscribeAndDispose (id : SubscriptionId) (subscription : Subscri
unsubscriber.Dispose ()

let removeSubscription (id : SubscriptionId) (subscriptions : SubscriptionsDict) =
if subscriptions.ContainsKey (id) then
subscriptions.[id] |> executeOnUnsubscribeAndDispose id
match subscriptions.TryGetValue id with
| true, sub ->
sub |> executeOnUnsubscribeAndDispose id
subscriptions.Remove (id) |> ignore
| false, _ -> ()

let removeAllSubscriptions (subscriptions : SubscriptionsDict) =
subscriptions
Expand Down
8 changes: 4 additions & 4 deletions src/FSharp.Data.GraphQL.Shared/Helpers/Reflection.fs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module internal ReflectionHelper =
let some =
let createSome = optionType.GetDeclaredMethod "Some"
fun value ->
if value <> null
if not (isNull value)
then
let valueType = value.GetType().GetTypeInfo()
if valueType = optionType
Expand All @@ -91,7 +91,7 @@ module internal ReflectionHelper =
let value =
let x = optionType.GetDeclaredProperty "Value"
fun input ->
if input <> null
if not (isNull input)
then
let valueType = input.GetType().GetTypeInfo()
if valueType = optionType
Expand All @@ -113,7 +113,7 @@ module internal ReflectionHelper =
let some =
let createSome = optionType.GetDeclaredMethod "Some"
fun value ->
if value <> null
if not (isNull value)
then
let valueType = value.GetType().GetTypeInfo()
if valueType = optionType
Expand All @@ -125,7 +125,7 @@ module internal ReflectionHelper =
let value =
let x = optionType.GetDeclaredProperty "Value"
fun input ->
if input <> null
if not (isNull input)
then
let valueType = input.GetType().GetTypeInfo()
if valueType = optionType
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Data.GraphQL.Shared/SchemaDefinitions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ module SchemaDefinitions =

/// Check if provided obj value is an Option and extract its wrapped value as object if possible
let private (|Option|_|) (x : obj) =
if x = null then None
if isNull x then None
else
let t = x.GetType().GetTypeInfo()
if t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<option<_>> then
Expand Down
6 changes: 3 additions & 3 deletions src/FSharp.Data.GraphQL.Shared/TypeSystem.fs
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,9 @@ and FieldExecuteMap (compiler : FieldExecuteCompiler) =
/// <param name="fieldName">The field name of the object that has the field that needs to be executed.</param>
member _.GetExecute (typeName : string, fieldName : string) =
let key = getKey typeName fieldName
if map.ContainsKey (key) then
fst map.[key]
else
match map.TryGetValue key with
| true, mapv -> fst mapv
| false, _ ->
Unchecked.defaultof<ExecuteField>

/// <summary>
Expand Down

0 comments on commit 5514d18

Please sign in to comment.