Skip to content

Commit 075debf

Browse files
committed
Backport of operation compiler optimization.
1 parent 2e3b436 commit 075debf

File tree

42 files changed

+613
-562
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+613
-562
lines changed

src/HotChocolate/Core/src/Abstractions/packages.lock.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@
284284
"net8.0": {
285285
"Microsoft.NET.ILLink.Tasks": {
286286
"type": "Direct",
287-
"requested": "[8.0.3, )",
288-
"resolved": "8.0.3",
289-
"contentHash": "0kwNg0LBIvVTx9A2mo9Mnw4wLGtaeQgjSz5P13bOOwdWPPLe9HzI+XTkwiMhS7iQCM6X4LAbFR76xScaMw0MrA=="
287+
"requested": "[8.0.7, )",
288+
"resolved": "8.0.7",
289+
"contentHash": "iI52ptEKby2ymQ6B7h4TWbFmm85T4VvLgc/HvS45Yr3lgi4IIFbQtjON3bQbX/Vc94jXNSLvrDOp5Kh7SJyFYQ=="
290290
},
291291
"Microsoft.SourceLink.GitHub": {
292292
"type": "Direct",

src/HotChocolate/Core/src/Execution/Processing/OperationCompiler.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public sealed partial class OperationCompiler
3838
private readonly List<Selection> _selections = [];
3939
private readonly HashSet<string> _directiveNames = new(Ordinal);
4040
private readonly List<FieldMiddleware> _pipelineComponents = [];
41-
private IncludeCondition[] _includeConditions = Array.Empty<IncludeCondition>();
41+
private readonly HashSet<int> _enqueuedSelectionSets = new();
42+
private IncludeCondition[] _includeConditions = [];
4243
private CompilerContext? _deferContext;
4344
private int _nextSelectionId;
4445
private int _nextSelectionSetRefId;
@@ -160,8 +161,9 @@ public IOperation Compile(
160161
_selections.Clear();
161162
_directiveNames.Clear();
162163
_pipelineComponents.Clear();
164+
_enqueuedSelectionSets.Clear();
163165

164-
_includeConditions = Array.Empty<IncludeCondition>();
166+
_includeConditions = [];
165167
_deferContext = null;
166168
}
167169
}
@@ -355,18 +357,15 @@ private void CompleteSelectionSet(CompilerContext context)
355357

356358
var selectionPath = context.Path.Append(selection.ResponseName);
357359
selectionSetId = GetOrCreateSelectionSetRefId(selection.SelectionSet, selectionPath);
358-
var selectionVariants = GetOrCreateSelectionVariants(selectionSetId);
359360
var possibleTypes = context.Schema.GetPossibleTypes(fieldType);
360361

361-
for (var i = possibleTypes.Count - 1; i >= 0; i--)
362+
if (_enqueuedSelectionSets.Add(selectionSetId))
362363
{
363-
var objectType = possibleTypes[i];
364-
365-
if (!selectionVariants.ContainsSelectionSet(objectType))
364+
for (var i = possibleTypes.Count - 1; i >= 0; i--)
366365
{
367366
_backlog.Push(
368367
new BacklogItem(
369-
objectType,
368+
possibleTypes[i],
370369
selectionSetId,
371370
selection,
372371
selectionPath,
@@ -451,21 +450,21 @@ private void ResolveFields(
451450
case SyntaxKind.Field:
452451
ResolveField(
453452
context,
454-
(FieldNode) selection,
453+
(FieldNode)selection,
455454
includeCondition);
456455
break;
457456

458457
case SyntaxKind.InlineFragment:
459458
ResolveInlineFragment(
460459
context,
461-
(InlineFragmentNode) selection,
460+
(InlineFragmentNode)selection,
462461
includeCondition);
463462
break;
464463

465464
case SyntaxKind.FragmentSpread:
466465
ResolveFragmentSpread(
467466
context,
468-
(FragmentSpreadNode) selection,
467+
(FragmentSpreadNode)selection,
469468
includeCondition);
470469
break;
471470
}
@@ -579,9 +578,9 @@ private void ResolveFragment(
579578
IReadOnlyList<DirectiveNode> directives,
580579
long includeCondition)
581580
{
582-
if (typeCondition is null ||
583-
(context.Schema.TryGetTypeFromAst(typeCondition, out IType typeCon) &&
584-
DoesTypeApply(typeCon, context.Type)))
581+
if (typeCondition is null
582+
|| (context.Schema.TryGetTypeFromAst(typeCondition, out IType typeCon)
583+
&& DoesTypeApply(typeCon, context.Type)))
585584
{
586585
includeCondition = GetSelectionIncludeCondition(selection, includeCondition);
587586

@@ -644,8 +643,8 @@ private static bool DoesTypeApply(IType typeCondition, IObjectType current)
644643
=> typeCondition.Kind switch
645644
{
646645
TypeKind.Object => ReferenceEquals(typeCondition, current),
647-
TypeKind.Interface => current.IsImplementing((InterfaceType) typeCondition),
648-
TypeKind.Union => ((UnionType) typeCondition).Types.ContainsKey(current.Name),
646+
TypeKind.Interface => current.IsImplementing((InterfaceType)typeCondition),
647+
TypeKind.Union => ((UnionType)typeCondition).Types.ContainsKey(current.Name),
649648
_ => false
650649
};
651650

@@ -661,8 +660,8 @@ private FragmentDefinitionNode GetFragmentDefinition(
661660

662661
for (var i = 0; i < document.Definitions.Count; i++)
663662
{
664-
if (document.Definitions[i] is FragmentDefinitionNode fragmentDefinition &&
665-
fragmentDefinition.Name.Value.EqualsOrdinal(fragmentName))
663+
if (document.Definitions[i] is FragmentDefinitionNode fragmentDefinition
664+
&& fragmentDefinition.Name.Value.EqualsOrdinal(fragmentName))
666665
{
667666
value = fragmentDefinition;
668667
_fragmentDefinitions.Add(fragmentName, value);
@@ -704,6 +703,7 @@ private SelectionVariants GetOrCreateSelectionVariants(int selectionSetId)
704703
variants = new SelectionVariants(selectionSetId);
705704
_selectionVariants.Add(selectionSetId, variants);
706705
}
706+
707707
return variants;
708708
}
709709

@@ -847,26 +847,25 @@ internal void RegisterNewSelection(Selection newSelection)
847847
}
848848
}
849849

850-
private readonly struct SelectionSetRef : IEquatable<SelectionSetRef>
850+
private readonly struct SelectionSetRef(
851+
SelectionSetNode selectionSet,
852+
SelectionPath path)
853+
: IEquatable<SelectionSetRef>
851854
{
852-
public SelectionSetRef(SelectionSetNode selectionSet, SelectionPath path)
853-
{
854-
SelectionSet = selectionSet;
855-
Path = path;
856-
}
857-
858-
public SelectionSetNode SelectionSet { get; }
855+
public SelectionSetNode SelectionSet { get; } = selectionSet;
859856

860-
public SelectionPath Path { get; }
857+
public SelectionPath Path { get; } = path;
861858

862859
public bool Equals(SelectionSetRef other)
863-
=> SelectionSet.Equals(other.SelectionSet, SyntaxComparison.Syntax) &&
864-
Path.Equals(other.Path);
860+
=> SyntaxComparer.BySyntax.Equals(SelectionSet, other.SelectionSet)
861+
&& Path.Equals(other.Path);
865862

866863
public override bool Equals(object? obj)
867864
=> obj is SelectionSetRef other && Equals(other);
868865

869866
public override int GetHashCode()
870-
=> HashCode.Combine(SelectionSet, Path);
867+
=> HashCode.Combine(
868+
SyntaxComparer.BySyntax.GetHashCode(SelectionSet),
869+
Path.GetHashCode());
871870
}
872-
}
871+
}

src/HotChocolate/Core/src/Execution/Processing/SelectionPath.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text;
23
using HotChocolate.Utilities;
34

45
namespace HotChocolate.Execution.Processing;
@@ -67,7 +68,12 @@ public bool Equals(SelectionPath? other)
6768
return true;
6869
}
6970

70-
return Equals(Parent, other.Parent);
71+
if (ReferenceEquals(Parent, null))
72+
{
73+
return false;
74+
}
75+
76+
return Parent.Equals(other.Parent);
7177
}
7278

7379
return false;
@@ -92,4 +98,25 @@ public override bool Equals(object? obj)
9298
/// <returns></returns>
9399
public override int GetHashCode()
94100
=> HashCode.Combine(Name, Parent);
101+
102+
/// <summary>
103+
/// Returns a string that represents the current path.
104+
/// </summary>
105+
/// <returns>
106+
/// A string that represents the current path.
107+
/// </returns>
108+
public override string ToString()
109+
{
110+
var path = new StringBuilder();
111+
var current = this;
112+
113+
do
114+
{
115+
path.Insert(0, current.Name);
116+
path.Insert(0, '/');
117+
current = current.Parent;
118+
} while (current != null);
119+
120+
return path.ToString();
121+
}
95122
}

src/HotChocolate/Core/src/Types.Shared/packages.lock.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@
158158
"net8.0": {
159159
"Microsoft.NET.ILLink.Tasks": {
160160
"type": "Direct",
161-
"requested": "[8.0.3, )",
162-
"resolved": "8.0.3",
163-
"contentHash": "0kwNg0LBIvVTx9A2mo9Mnw4wLGtaeQgjSz5P13bOOwdWPPLe9HzI+XTkwiMhS7iQCM6X4LAbFR76xScaMw0MrA=="
161+
"requested": "[8.0.7, )",
162+
"resolved": "8.0.7",
163+
"contentHash": "iI52ptEKby2ymQ6B7h4TWbFmm85T4VvLgc/HvS45Yr3lgi4IIFbQtjON3bQbX/Vc94jXNSLvrDOp5Kh7SJyFYQ=="
164164
},
165165
"Microsoft.SourceLink.GitHub": {
166166
"type": "Direct",

0 commit comments

Comments
 (0)