-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add missing clearing of pooled arrays #63710
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
254666f
3cd9a03
a667a09
ad06bc4
6661e27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Buffers; | ||
|
||
internal static class ArrayPoolExtensions | ||
{ | ||
public static void Return<T>(this ArrayPool<T> pool, T[] array, int lengthToClear) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the point of this overload? Shouldn't we just always use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's for cases were it's known that Do you think |
||
{ | ||
array.AsSpan(0, lengthToClear).Clear(); | ||
pool.Return(array); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Buffers; | ||
|
||
public sealed class ArrayPoolExtensionsTests | ||
{ | ||
[Fact] | ||
public void Return_PartiallyClearsArray_WithSpecifiedLengthToClear() | ||
{ | ||
ArrayPool<int> pool = ArrayPool<int>.Create(); | ||
int[] array = pool.Rent(64); | ||
|
||
array.AsSpan().Fill(int.MaxValue); | ||
|
||
pool.Return(array, 42); | ||
|
||
Assert.True(array.AsSpan(0, 42).IndexOfAnyExcept(0) < 0); | ||
Assert.True(array.AsSpan(42).IndexOfAnyExcept(int.MaxValue) < 0); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is repeated many times.
What do you think of adding an extra helper method to
ArrayPoolExtensions
.In the helper method have a comment that references are cleared from the pool so they can be GCed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that totally makes sense 👍