Skip to content

Commit e035feb

Browse files
authored
Merge branch 'main' into mst/pool-config
2 parents 135c450 + d0a888a commit e035feb

1 file changed

Lines changed: 38 additions & 17 deletions

File tree

src/HotChocolate/Core/src/Types/Execution/Processing/ValueCompletion.List.cs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Buffers;
12
using System.Collections;
23
using System.Text.Json;
34
using HotChocolate.Text.Json;
@@ -96,31 +97,51 @@ private static void CompleteListValue(
9697

9798
if (runtimeValue is IEnumerable enumerable)
9899
{
99-
var i = 0;
100-
var temp = new List<object?>();
100+
var count = 0;
101+
var buffer = ArrayPool<object?>.Shared.Rent(64);
101102

102-
foreach (var value in enumerable)
103+
try
103104
{
104-
temp.Add(value);
105-
}
105+
foreach (var value in enumerable)
106+
{
107+
if (count == buffer.Length)
108+
{
109+
var newBuffer = ArrayPool<object?>.Shared.Rent(buffer.Length * 2);
110+
var span = buffer.AsSpan(0, count);
111+
span.CopyTo(newBuffer);
112+
span.Clear();
113+
ArrayPool<object?>.Shared.Return(buffer);
114+
buffer = newBuffer;
115+
}
116+
117+
buffer[count++] = value;
118+
}
106119

107-
resultValue.SetArrayValue(temp.Count);
120+
resultValue.SetArrayValue(count);
108121

109-
foreach (var element in resultValue.EnumerateArray())
110-
{
111-
Complete(
112-
context,
113-
selection,
114-
elementType,
115-
element,
116-
temp[i++]);
122+
var i = 0;
117123

118-
// if we ran into an error that invalidated the result we abort.
119-
if (element.IsInvalidated)
124+
foreach (var element in resultValue.EnumerateArray())
120125
{
121-
return;
126+
Complete(
127+
context,
128+
selection,
129+
elementType,
130+
element,
131+
buffer[i++]);
132+
133+
// if we ran into an error that invalidated the result we abort.
134+
if (element.IsInvalidated)
135+
{
136+
return;
137+
}
122138
}
123139
}
140+
finally
141+
{
142+
buffer.AsSpan(0, count).Clear();
143+
ArrayPool<object?>.Shared.Return(buffer);
144+
}
124145

125146
return;
126147
}

0 commit comments

Comments
 (0)