Skip to content

Commit 32131a4

Browse files
authored
Simplify ExecuteSingleAsync when batching (#9440)
1 parent 6c2c814 commit 32131a4

1 file changed

Lines changed: 17 additions & 83 deletions

File tree

src/HotChocolate/Fusion/src/Fusion.Execution/Execution/Nodes/OperationBatchExecutionNode.cs

Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Buffers;
22
using System.Collections.Immutable;
3-
using System.Runtime.InteropServices;
43
using HotChocolate.Fusion.Execution.Clients;
54
using HotChocolate.Language;
65

@@ -81,10 +80,6 @@ private async ValueTask<ExecutionStatus> ExecuteSingleAsync(
8180
RequiresFileUpload = operation.RequiresFileUpload
8281
};
8382

84-
var index = 0;
85-
var bufferLength = 0;
86-
SourceSchemaResult[]? buffer = null;
87-
SourceSchemaResult? singleResult = null;
8883
var hasSomeErrors = false;
8984

9085
try
@@ -93,85 +88,32 @@ private async ValueTask<ExecutionStatus> ExecuteSingleAsync(
9388
var response = await client.ExecuteAsync(context, request, cancellationToken).ConfigureAwait(false);
9489
context.TrackSourceSchemaClientResponse(this, response);
9590

96-
var totalPathCount = variables.Length;
97-
98-
for (var i = 0; i < variables.Length; i++)
99-
{
100-
totalPathCount += variables[i].AdditionalPaths.Length;
101-
}
102-
103-
var initialBufferLength = Math.Max(totalPathCount, 4);
104-
10591
await foreach (var result in response.ReadAsResultStreamAsync(cancellationToken).ConfigureAwait(false))
10692
{
107-
if (index == 0)
93+
var hasErrors = result.Errors is not null;
94+
if (hasErrors)
10895
{
109-
singleResult = result;
110-
index = 1;
96+
hasSomeErrors = true;
11197
}
112-
else
98+
99+
try
113100
{
114-
if (buffer is null)
115-
{
116-
bufferLength = initialBufferLength;
117-
buffer = ArrayPool<SourceSchemaResult>.Shared.Rent(bufferLength);
118-
buffer[0] = singleResult!;
119-
}
120-
121-
buffer[index++] = result;
101+
context.AddPartialResult(
102+
operation.Source,
103+
result,
104+
operation.ResultSelectionSet,
105+
hasErrors);
122106
}
123-
124-
if (result.Errors is not null)
107+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
125108
{
126-
hasSomeErrors = true;
109+
return ExecutionStatus.Failed;
127110
}
128-
}
129-
}
130-
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
131-
{
132-
return ExecutionStatus.Failed;
133-
}
134-
catch (Exception exception)
135-
{
136-
diagnosticEvents.SourceSchemaTransportError(context, this, schemaName, exception);
137-
138-
if (buffer is not null && bufferLength > 0)
139-
{
140-
foreach (var result in buffer.AsSpan(0, index))
111+
catch (Exception exception)
141112
{
142-
result?.Dispose();
113+
diagnosticEvents.SourceSchemaStoreError(context, this, schemaName, exception);
114+
context.AddErrors(exception, variables, operation.ResultSelectionSet);
115+
return ExecutionStatus.Failed;
143116
}
144-
145-
buffer.AsSpan(0, index).Clear();
146-
ArrayPool<SourceSchemaResult>.Shared.Return(buffer);
147-
}
148-
else if (singleResult is not null)
149-
{
150-
singleResult.Dispose();
151-
}
152-
153-
context.AddErrors(exception, variables, operation.ResultSelectionSet);
154-
return ExecutionStatus.Failed;
155-
}
156-
157-
try
158-
{
159-
if (buffer is not null)
160-
{
161-
context.AddPartialResults(
162-
operation.Source,
163-
buffer.AsSpan(0, index),
164-
operation.ResultSelectionSet,
165-
hasSomeErrors);
166-
}
167-
else if (singleResult is not null)
168-
{
169-
var firstResult = singleResult;
170-
context.AddPartialResults(
171-
operation.Source,
172-
MemoryMarshal.CreateReadOnlySpan(ref firstResult, 1),
173-
operation.ResultSelectionSet,
174-
hasSomeErrors);
175117
}
176118
}
177119
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
@@ -180,18 +122,10 @@ private async ValueTask<ExecutionStatus> ExecuteSingleAsync(
180122
}
181123
catch (Exception exception)
182124
{
183-
diagnosticEvents.SourceSchemaStoreError(context, this, schemaName, exception);
125+
diagnosticEvents.SourceSchemaTransportError(context, this, schemaName, exception);
184126
context.AddErrors(exception, variables, operation.ResultSelectionSet);
185127
return ExecutionStatus.Failed;
186128
}
187-
finally
188-
{
189-
if (buffer is not null)
190-
{
191-
buffer.AsSpan(0, index).Clear();
192-
ArrayPool<SourceSchemaResult>.Shared.Return(buffer);
193-
}
194-
}
195129

196130
return hasSomeErrors ? ExecutionStatus.PartialSuccess : ExecutionStatus.Success;
197131
}

0 commit comments

Comments
 (0)