Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear rented buffers before using them. #4592

Merged
merged 4 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ private static void CleanMiddlewareDefinitions<T>(
if (nonRepeatable > 1)
{
var keys = ArrayPool<string>.Shared.Rent(nonRepeatable);

// we clear the section of the array we need before we are using it.
keys.AsSpan().Slice(0, nonRepeatable).Clear();
int i = 0, ki = 0;

do
Expand Down Expand Up @@ -375,12 +378,6 @@ private static void CleanMiddlewareDefinitions<T>(

} while (i < count);

if (ki > 0)
{
// we clear the section of the array we used.
keys.AsSpan().Slice(0, ki).Clear();
}

ArrayPool<string>.Shared.Return(keys);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public T CreateDefinition()
rented[i].Configure(Context);
}

rented.AsSpan().Slice(0, length).Clear();
ArrayPool<CreateConfiguration>.Shared.Return(rented, true);
}

Expand Down
7 changes: 6 additions & 1 deletion src/HotChocolate/Core/src/Types/Types/InputParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,16 @@ private object ParseObject(
{
if (resultValue.Kind == SyntaxKind.ObjectValue)
{
var processedCount = 0;
bool[]? processedBuffer = null;
Span<bool> processed = stack <= 256 && type.Fields.Count <= 32
? stackalloc bool[type.Fields.Count]
: processedBuffer = ArrayPool<bool>.Shared.Rent(type.Fields.Count);
var processedCount = 0;

if(processedBuffer is not null)
{
processed.Clear();
}

if (processedBuffer is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private static void CreateNodesField(
{
ListValueNode list = context.ArgumentLiteral<ListValueNode>(Ids);
Task<object?>[] tasks = ArrayPool<Task<object?>>.Shared.Rent(list.Items.Count);
tasks.AsSpan().Slice(0, list.Items.Count).Clear();
var result = new object?[list.Items.Count];

try
Expand Down