Skip to content

Commit

Permalink
Use frozen dictionary in field cllection. (#6889)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Feb 13, 2024
1 parent 9d44f09 commit b343a2b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
17 changes: 12 additions & 5 deletions src/HotChocolate/Core/src/Types/Schema.Initialization.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
#if NET8_0_OR_GREATER
using System.Collections.Frozen;
#endif
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Configuration;
Expand Down Expand Up @@ -30,7 +33,7 @@ protected sealed override SchemaTypeDefinition CreateDefinition(ITypeDiscoveryCo
var descriptor = SchemaTypeDescriptor.New(context.DescriptorContext, GetType());

_configure(descriptor);

context.DescriptorContext.ApplySchemaConfigurations(descriptor);

return descriptor.CreateDefinition();
Expand Down Expand Up @@ -105,7 +108,11 @@ internal void CompleteSchema(SchemaTypesDefinition schemaTypesDefinition)

DirectiveTypes = schemaTypesDefinition.DirectiveTypes;
_types = new SchemaTypes(schemaTypesDefinition);
_directiveTypes = DirectiveTypes.ToDictionary(t => t.Name);
#if NET8_0_OR_GREATER
_directiveTypes = DirectiveTypes.ToFrozenDictionary(t => t.Name, StringComparer.Ordinal);
#else
_directiveTypes = DirectiveTypes.ToDictionary(t => t.Name, StringComparer.Ordinal);
#endif
_sealed = true;
}
}
Expand Down Expand Up @@ -138,7 +145,7 @@ public static void AddSchemaConfiguration(
options = (List<Action<ISchemaTypeDescriptor>>)value!;
options.Add(configure);
}

public static void AddSchemaConfiguration(
this IDescriptorContext context,
Action<ISchemaTypeDescriptor> configure)
Expand All @@ -165,7 +172,7 @@ public static void AddSchemaConfiguration(
options = (List<Action<ISchemaTypeDescriptor>>)value!;
options.Add(configure);
}

public static void ApplySchemaConfigurations(
this IDescriptorContext context,
ISchemaTypeDescriptor descriptor)
Expand All @@ -189,4 +196,4 @@ public static void ApplySchemaConfigurations(
}
}
}
}
}
7 changes: 7 additions & 0 deletions src/HotChocolate/Core/src/Types/Schema.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
#if NET8_0_OR_GREATER
using System.Collections.Frozen;
#endif
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Language;
Expand All @@ -20,7 +23,11 @@ public partial class Schema
, ISchema
{
private SchemaTypes _types = default!;
#if NET8_0_OR_GREATER
private FrozenDictionary<string, DirectiveType> _directiveTypes = default!;
#else
private Dictionary<string, DirectiveType> _directiveTypes = default!;
#endif

/// <summary>
/// Gets the schema directives.
Expand Down
17 changes: 15 additions & 2 deletions src/HotChocolate/Core/src/Types/SchemaTypes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable enable

using System;
#if NET8_0_OR_GREATER
using System.Collections.Frozen;
#endif
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand All @@ -11,8 +14,13 @@ namespace HotChocolate;

internal sealed class SchemaTypes
{
#if NET8_0_OR_GREATER
private readonly FrozenDictionary<string, INamedType> _types;
private readonly FrozenDictionary<string, List<ObjectType>> _possibleTypes;
#else
private readonly Dictionary<string, INamedType> _types;
private readonly Dictionary<string, List<ObjectType>> _possibleTypes;
#endif

public SchemaTypes(SchemaTypesDefinition definition)
{
Expand All @@ -28,8 +36,13 @@ public SchemaTypes(SchemaTypesDefinition definition)
nameof(definition));
}

_types = definition.Types.ToDictionary(t => t.Name);
#if NET8_0_OR_GREATER
_types = definition.Types.ToFrozenDictionary(t => t.Name, StringComparer.Ordinal);
_possibleTypes = CreatePossibleTypeLookup(definition.Types).ToFrozenDictionary(StringComparer.Ordinal);
#else
_types = definition.Types.ToDictionary(t => t.Name, StringComparer.Ordinal);
_possibleTypes = CreatePossibleTypeLookup(definition.Types);
#endif
QueryType = definition.QueryType!;
MutationType = definition.MutationType;
SubscriptionType = definition.SubscriptionType;
Expand Down Expand Up @@ -104,7 +117,7 @@ public bool TryGetPossibleTypes(
private static Dictionary<string, List<ObjectType>> CreatePossibleTypeLookup(
IReadOnlyCollection<INamedType> types)
{
var possibleTypes = new Dictionary<string, List<ObjectType>>();
var possibleTypes = new Dictionary<string, List<ObjectType>>(StringComparer.Ordinal);

foreach (var objectType in types.OfType<ObjectType>())
{
Expand Down
29 changes: 19 additions & 10 deletions src/HotChocolate/Core/src/Types/Types/FieldCollection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections;
#if NET8_0_OR_GREATER
using System.Collections.Frozen;
#endif
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
Expand All @@ -10,23 +13,35 @@ namespace HotChocolate.Types;

public sealed class FieldCollection<T> : IFieldCollection<T> where T : class, IField
{
#if NET8_0_OR_GREATER
private readonly FrozenDictionary<string, T> _fieldsLookup;
#else
private readonly Dictionary<string, T> _fieldsLookup;
#endif
private readonly T[] _fields;

internal FieldCollection(T[] fields)
{
_fields = fields ?? throw new ArgumentNullException(nameof(fields));
#if NET8_0_OR_GREATER
_fieldsLookup = _fields.ToFrozenDictionary(t => t.Name, StringComparer.Ordinal);
#else
_fieldsLookup = new Dictionary<string, T>(_fields.Length, StringComparer.Ordinal);

foreach (var field in _fields)
{
_fieldsLookup.Add(field.Name, field);
}
#endif
}

private FieldCollection(Dictionary<string, T> fieldsLookup, T[] fields)
{
#if NET8_0_OR_GREATER
_fieldsLookup = fieldsLookup.ToFrozenDictionary(StringComparer.Ordinal);
#else
_fieldsLookup = fieldsLookup;
#endif
_fields = fields;
}

Expand Down Expand Up @@ -100,7 +115,7 @@ internal static FieldCollection<T> TryCreate(T[] fields, out IReadOnlyCollection
(duplicates ??= []).Add(field.Name);
continue;
}

internalLookup.Add(field.Name, field);
#endif
}
Expand All @@ -115,16 +130,10 @@ internal static FieldCollection<T> TryCreate(T[] fields, out IReadOnlyCollection
return new FieldCollection<T>(internalLookup, fields);
}

private sealed class FieldEnumerator : IEnumerator<T>
private sealed class FieldEnumerator(T[] fields) : IEnumerator<T>
{
private readonly T[] _fields;
private int _index = -1;

public FieldEnumerator(T[] fields)
{
_fields = fields;
}

public T Current { get; private set; } = default!;

object IEnumerator.Current => Current;
Expand All @@ -133,9 +142,9 @@ public bool MoveNext()
{
_index++;

if (_index < _fields.Length)
if (_index < fields.Length)
{
Current = _fields[_index];
Current = fields[_index];
return true;
}

Expand Down

0 comments on commit b343a2b

Please sign in to comment.