Skip to content

Commit 6f69bfa

Browse files
authored
Introduced Priority Queue to Type Discovery (#8955)
1 parent db55eac commit 6f69bfa

File tree

7 files changed

+119
-162
lines changed

7 files changed

+119
-162
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using HotChocolate.Types;
2+
using HotChocolate.Types.Descriptors;
3+
4+
namespace HotChocolate.Configuration;
5+
6+
internal static class BuiltInDirectiveTypeReferences
7+
{
8+
internal static void Enqueue(
9+
PriorityQueue<TypeReference, (TypeReferenceStrength, int)> backlog,
10+
IDescriptorContext context,
11+
ref int nextIndex)
12+
{
13+
var typeInspector = context.TypeInspector;
14+
15+
if (context.Options.EnableOneOf)
16+
{
17+
EnqueueTypeRef(backlog, typeInspector.GetTypeRef(typeof(OneOfDirectiveType)), nextIndex++);
18+
}
19+
20+
if (context.Options.EnableDefer)
21+
{
22+
EnqueueTypeRef(backlog, typeInspector.GetTypeRef(typeof(DeferDirectiveType)), nextIndex++);
23+
}
24+
25+
if (context.Options.EnableStream)
26+
{
27+
EnqueueTypeRef(backlog, typeInspector.GetTypeRef(typeof(StreamDirectiveType)), nextIndex++);
28+
}
29+
30+
if (context.Options.EnableSemanticNonNull)
31+
{
32+
EnqueueTypeRef(backlog, typeInspector.GetTypeRef(typeof(SemanticNonNullDirective)), nextIndex++);
33+
}
34+
35+
if (context.Options.EnableTag)
36+
{
37+
EnqueueTypeRef(backlog, typeInspector.GetTypeRef(typeof(Tag)), nextIndex++);
38+
}
39+
40+
EnqueueTypeRef(backlog, typeInspector.GetTypeRef(typeof(SkipDirectiveType)), nextIndex++);
41+
EnqueueTypeRef(backlog, typeInspector.GetTypeRef(typeof(IncludeDirectiveType)), nextIndex++);
42+
EnqueueTypeRef(backlog, typeInspector.GetTypeRef(typeof(DeprecatedDirectiveType)), nextIndex++);
43+
44+
static void EnqueueTypeRef(
45+
PriorityQueue<TypeReference, (TypeReferenceStrength, int)> backlog,
46+
TypeReference typeRef,
47+
int index)
48+
=> backlog.Enqueue(typeRef, (TypeReferenceStrength.System, index));
49+
}
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using HotChocolate.Types.Descriptors;
2+
using HotChocolate.Types.Introspection;
3+
4+
namespace HotChocolate.Configuration;
5+
6+
internal static class IntrospectionTypeReferences
7+
{
8+
internal static void Enqueue(
9+
PriorityQueue<TypeReference, (TypeReferenceStrength, int)> backlog,
10+
IDescriptorContext context,
11+
ref int nextIndex)
12+
{
13+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__Directive)), nextIndex++);
14+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__DirectiveLocation)), nextIndex++);
15+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__EnumValue)), nextIndex++);
16+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__Field)), nextIndex++);
17+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__InputValue)), nextIndex++);
18+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__Schema)), nextIndex++);
19+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__Type)), nextIndex++);
20+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__TypeKind)), nextIndex++);
21+
22+
if (context.Options.EnableDirectiveIntrospection)
23+
{
24+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__AppliedDirective)), nextIndex++);
25+
EnqueueTypeRef(backlog, context.TypeInspector.GetTypeRef(typeof(__DirectiveArgument)), nextIndex++);
26+
}
27+
28+
static void EnqueueTypeRef(
29+
PriorityQueue<TypeReference, (TypeReferenceStrength, int)> backlog,
30+
TypeReference typeRef,
31+
int index)
32+
=> backlog.Enqueue(typeRef, (TypeReferenceStrength.System, index));
33+
}
34+
}

src/HotChocolate/Core/src/Types/Configuration/TypeDiscoverer.cs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace HotChocolate.Configuration;
77

88
internal sealed class TypeDiscoverer
99
{
10-
private readonly List<TypeReference> _unregistered = [];
1110
private readonly List<ISchemaError> _errors = [];
1211
private readonly List<TypeReference> _resolved = [];
1312
private readonly IDescriptorContext _context;
@@ -16,6 +15,9 @@ internal sealed class TypeDiscoverer
1615
private readonly ITypeRegistrarHandler[] _handlers;
1716
private readonly TypeInterceptor _interceptor;
1817

18+
private readonly PriorityQueue<TypeReference, (TypeReferenceStrength, int)> _unregistered = new();
19+
private int _nextTypeRefIndex;
20+
1921
public TypeDiscoverer(
2022
IDescriptorContext context,
2123
TypeRegistry typeRegistry,
@@ -35,53 +37,41 @@ public TypeDiscoverer(
3537

3638
if (includeSystemTypes)
3739
{
38-
_unregistered.AddRange(IntrospectionTypes.CreateReferences(context));
39-
_unregistered.AddRange(Directives.CreateReferences(context));
40+
IntrospectionTypeReferences.Enqueue(_unregistered, context, ref _nextTypeRefIndex);
41+
BuiltInDirectiveTypeReferences.Enqueue(_unregistered, context, ref _nextTypeRefIndex);
4042
}
4143

42-
var first = new List<TypeReference>();
43-
var second = new List<TypeReference>();
44-
var third = new List<TypeReference>();
45-
var fourth = new List<TypeReference>();
46-
4744
foreach (var typeRef in typeRegistry.GetTypeRefs().Concat(initialTypes.Distinct()))
4845
{
4946
switch (typeRef)
5047
{
5148
case ExtendedTypeReference { Type.IsSchemaType: true } extendedTypeRef:
52-
if (typeof(ScalarType).IsAssignableFrom(extendedTypeRef.Type.Type))
53-
{
54-
first.Add(typeRef);
55-
}
56-
else
57-
{
58-
second.Add(typeRef);
59-
}
49+
_unregistered.Enqueue(
50+
typeRef,
51+
(typeof(ScalarType).IsAssignableFrom(extendedTypeRef.Type.Type)
52+
? TypeReferenceStrength.VeryStrong
53+
: TypeReferenceStrength.Strong,
54+
_nextTypeRefIndex++));
6055
break;
6156

6257
case ExtendedTypeReference:
63-
third.Add(typeRef);
58+
_unregistered.Enqueue(typeRef, (TypeReferenceStrength.Weak, _nextTypeRefIndex++));
6459
break;
6560

6661
case SchemaTypeReference { Type: ScalarType }:
67-
first.Add(typeRef);
62+
_unregistered.Enqueue(typeRef, (TypeReferenceStrength.VeryStrong, _nextTypeRefIndex++));
6863
break;
6964

7065
case SchemaTypeReference:
71-
second.Add(typeRef);
66+
_unregistered.Enqueue(typeRef, (TypeReferenceStrength.Strong, _nextTypeRefIndex++));
7267
break;
7368

7469
default:
75-
fourth.Add(typeRef);
70+
_unregistered.Enqueue(typeRef, (TypeReferenceStrength.VeryWeak, _nextTypeRefIndex++));
7671
break;
7772
}
7873
}
7974

80-
_unregistered.AddRange(first);
81-
_unregistered.AddRange(second);
82-
_unregistered.AddRange(third);
83-
_unregistered.AddRange(fourth);
84-
8575
_typeRegistrar = new TypeRegistrar(context, typeRegistry, typeLookup, interceptor);
8676

8777
_handlers =
@@ -137,7 +127,7 @@ public IReadOnlyList<ISchemaError> DiscoverTypes()
137127
{
138128
if (processed.Add(typeReference))
139129
{
140-
_unregistered.Add(typeReference);
130+
_unregistered.Enqueue(typeReference, (TypeReferenceStrength.VeryWeak, _nextTypeRefIndex++));
141131
}
142132
}
143133

@@ -161,18 +151,18 @@ private void RegisterTypes()
161151
{
162152
while (_unregistered.Count > 0)
163153
{
164-
foreach (var typeRef in _unregistered)
154+
while (_unregistered.TryDequeue(out var typeRef, out _))
165155
{
166156
var index = (int)typeRef.Kind;
167-
168157
if (_handlers.Length > index)
169158
{
170159
_handlers[index].Handle(_typeRegistrar, typeRef);
171160
}
172161
}
173162

174-
_unregistered.Clear();
175-
_unregistered.AddRange(_typeRegistrar.GetUnhandled());
163+
_unregistered.EnqueueRange(
164+
_typeRegistrar.GetUnhandled().Select(
165+
typeRef => (t: typeRef, (TypeReferenceStrength.VeryWeak, _nextTypeRefIndex++))));
176166
}
177167
}
178168

@@ -188,7 +178,7 @@ private bool TryInferTypes()
188178
&& _typeRegistry.RuntimeTypeRefs.TryGetValue(extendedTypeRef, out var typeReference))
189179
{
190180
inferred = true;
191-
_unregistered.Add(typeReference);
181+
_unregistered.Enqueue(typeReference, (TypeReferenceStrength.VeryWeak, _nextTypeRefIndex++));
192182
_resolved.Add(unresolvedTypeRef);
193183
continue;
194184
}
@@ -201,11 +191,11 @@ private bool TryInferTypes()
201191

202192
foreach (var schemaTypeRef in schemaTypeRefs)
203193
{
204-
_unregistered.Add(schemaTypeRef);
194+
_unregistered.Enqueue(schemaTypeRef, (TypeReferenceStrength.VeryWeak, _nextTypeRefIndex++));
205195

206196
if (unresolvedTypeRef is ExtendedTypeReference typeRef)
207197
{
208-
// we normalize the type context so that we can correctly lookup
198+
// we normalize the type context so that we can correctly look up
209199
// if a type is already registered.
210200
typeRef = typeRef.WithContext(schemaTypeRef.Context);
211201
_typeRegistry.TryRegister(typeRef, schemaTypeRef);
@@ -263,7 +253,7 @@ private void CollectErrors()
263253
{
264254
builder
265255
.SetTypeSystemObject(types[0].Type)
266-
.SetExtension("involvedTypes", types.Select(t => t.Type).ToList());
256+
.SetExtension("involvedTypes", types.ConvertAll(t => t.Type));
267257
}
268258

269259
_errors.Add(builder.Build());
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace HotChocolate.Configuration;
2+
3+
internal enum TypeReferenceStrength
4+
{
5+
System = 0,
6+
VeryStrong = 1,
7+
Strong = 2,
8+
Weak = 3,
9+
VeryWeak = 4
10+
}

src/HotChocolate/Core/src/Types/Types/Directives/Directives.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/HotChocolate/Core/src/Types/Types/Introspection/IntrospectionTypes.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/HotChocolate/Spatial/test/Data.Filters.SqlServer.Tests/PostgisConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public override void Configure(ContainerResourceBuilder builder)
99
builder
1010
.WaitTimeout(120)
1111
.Name("postgis")
12-
.Image("postgis/postgis:latest")
12+
.Image("postgis/postgis:16-3.4-alpine")
1313
.Username("postgis")
1414
.Password(Guid.NewGuid().ToString("N")[12..])
1515
.InternalPort(5432);

0 commit comments

Comments
 (0)