Skip to content

Commit

Permalink
Optimized Field Merging (#6648)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Oct 31, 2023
1 parent a95df16 commit e92bc8d
Show file tree
Hide file tree
Showing 17 changed files with 357 additions and 146 deletions.
4 changes: 2 additions & 2 deletions src/HotChocolate/Core/src/Types/IReadOnlySchemaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public interface IReadOnlySchemaOptions

/// <inheritdoc cref="SchemaOptions.StripLeadingIFromInterface"/>
bool StripLeadingIFromInterface { get; }

/// <inheritdoc cref="SchemaOptions.EnableTrueNullability"/>
bool EnableTrueNullability { get; }

/// <inheritdoc cref="SchemaOptions.EnableTag"/>
bool EnableTag { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ internal static bool IsType(this IType type, TypeKind kind)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsType(this IType type, TypeKind kind1, TypeKind kind2)
internal static bool IsType(this IType type, TypeKind kind1, TypeKind kind2)
{
if (type.Kind == kind1 || type.Kind == kind2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public IOutputType NonNullString

public IDictionary<string, object?> ContextData { get; set; } = default!;

public List<FieldInfoPair> CurrentFieldPairs { get; } = new();

public List<FieldInfoPair> NextFieldPairs { get; } = new();

public HashSet<FieldInfoPair> ProcessedFieldPairs { get; } = new();

public IList<FieldInfo> RentFieldInfoList()
{
var buffer = _buffers.Peek();
Expand Down Expand Up @@ -159,6 +165,9 @@ public void Clear()
InputFields.Clear();
_errors.Clear();
List.Clear();
CurrentFieldPairs.Clear();
NextFieldPairs.Clear();
ProcessedFieldPairs.Clear();
UnexpectedErrorsDetected = false;
Count = 0;
Max = 0;
Expand Down
40 changes: 38 additions & 2 deletions src/HotChocolate/Core/src/Validation/FieldInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using HotChocolate.Language;
using HotChocolate.Types;

Expand All @@ -7,7 +8,7 @@ namespace HotChocolate.Validation;
/// The validation field info provides access to the field node and the type
/// information of the referenced field.
/// </summary>
public readonly struct FieldInfo
public readonly struct FieldInfo : IEquatable<FieldInfo>
{
/// <summary>
/// Initializes a new instance of <see cref="FieldInfo"/>
Expand Down Expand Up @@ -41,4 +42,39 @@ public FieldInfo(IType declaringType, IType type, FieldNode field)
/// Gets the field selection.
/// </summary>
public FieldNode Field { get; }
}

/// <summary>
/// Compares this field info to another field info.
/// </summary>
/// <param name="other">
/// The other field info.
/// </param>
/// <returns>
/// <c>true</c> if the field infos are equal.
/// </returns>
public bool Equals(FieldInfo other)
=> Field.Equals(other.Field) &&
DeclaringType.Equals(other.DeclaringType) &&
Type.Equals(other.Type);

/// <summary>
/// Compares this field info to another object.
/// </summary>
/// <param name="obj">
/// The other object.
/// </param>
/// <returns>
/// <c>true</c> if the field infos are equal.
/// </returns>
public override bool Equals(object? obj)
=> obj is FieldInfo other && Equals(other);

/// <summary>
/// Returns the hash code of this instance.
/// </summary>
/// <returns>
/// The hash code of this instance.
/// </returns>
public override int GetHashCode()
=> HashCode.Combine(Field, DeclaringType, Type);
}
67 changes: 67 additions & 0 deletions src/HotChocolate/Core/src/Validation/FieldInfoPair.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;

namespace HotChocolate.Validation;

/// <summary>
/// Represents a pair of field infos.
/// </summary>
public readonly struct FieldInfoPair : IEquatable<FieldInfoPair>
{
/// <summary>
/// Initializes a new instance of <see cref="FieldInfoPair"/>.
/// </summary>
/// <param name="fieldA">
/// The first field info.
/// </param>
/// <param name="fieldB">
/// The second field info.
/// </param>
public FieldInfoPair(FieldInfo fieldA, FieldInfo fieldB)
{
FieldA = fieldA;
FieldB = fieldB;
}

/// <summary>
/// Gets the first field info.
/// </summary>
public FieldInfo FieldA { get; }

/// <summary>
/// Gets the second field info.
/// </summary>
public FieldInfo FieldB { get; }

/// <summary>
/// Compares this field info pair to another field info pair.
/// </summary>
/// <param name="other">
/// The other field info pair.
/// </param>
/// <returns>
/// <c>true</c> if the field info pairs are equal.
/// </returns>
public bool Equals(FieldInfoPair other)
=> FieldA.Equals(other.FieldA) && FieldB.Equals(other.FieldB);

/// <summary>
/// Compares this field info pair to another object.
/// </summary>
/// <param name="obj">
/// The other object.
/// </param>
/// <returns>
/// <c>true</c> if the field info pairs are equal.
/// </returns>
public override bool Equals(object? obj)
=> obj is FieldInfoPair other && Equals(other);

/// <summary>
/// Returns the hash code for this field info pair.
/// </summary>
/// <returns>
/// The hash code for this field info pair.
/// </returns>
public override int GetHashCode()
=> HashCode.Combine(FieldA, FieldB);
}
15 changes: 15 additions & 0 deletions src/HotChocolate/Core/src/Validation/IDocumentValidatorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ public interface IDocumentValidatorContext : ISyntaxVisitorContext
/// </summary>
IDictionary<string, object?> ContextData { get; }

/// <summary>
/// When processing field merging this list holds the field pairs that are processed.
/// </summary>
List<FieldInfoPair> CurrentFieldPairs { get; }

/// <summary>
/// When processing field merging this list holds the field pairs that are processed next.
/// </summary>
List<FieldInfoPair> NextFieldPairs { get; }

/// <summary>
/// When processing field merging this set represents the already processed field pairs.
/// </summary>
HashSet<FieldInfoPair> ProcessedFieldPairs { get; }

/// <summary>
/// Rents a list of field infos.
/// </summary>
Expand Down
Loading

0 comments on commit e92bc8d

Please sign in to comment.