Skip to content
Open
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 @@ -26,6 +26,7 @@
<Compile Include="$(SharedSourceRoot)UrlDecoder\UrlDecoder.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Metrics\MetricsConstants.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Components\ComponentsActivityLinkStore.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
</ItemGroup>

<Import Project="Microsoft.AspNetCore.Components.Routing.targets" />
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Components/src/NavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ protected async ValueTask<bool> NotifyLocationChangingAsync(string uri, string?
}
finally
{
ArrayPool<Func<LocationChangingContext, ValueTask>>.Shared.Return(locationChangingHandlersCopy);
ArrayPool<Func<LocationChangingContext, ValueTask>>.Shared.Return(locationChangingHandlersCopy, handlerCount);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static PooledBuffer Add(ref PooledBuffer buffer, TElement element)
{
var newBuffer = ArrayPool<TElement>.Shared.Rent(buffer.Data.Length * 2);
Array.Copy(buffer.Data, newBuffer, buffer.Data.Length);
ArrayPool<TElement>.Shared.Return(buffer.Data);
ArrayPool<TElement>.Shared.ReturnAndClearReferences(buffer.Data, buffer.Count);
buffer.Data = newBuffer;
}

Expand All @@ -28,7 +28,7 @@ public static PooledBuffer Add(ref PooledBuffer buffer, TElement element)
public static TCollection ToResult(PooledBuffer buffer)
{
var result = TCollectionFactory.ToResultCore(buffer.Data, buffer.Count);
ArrayPool<TElement>.Shared.Return(buffer.Data);
ArrayPool<TElement>.Shared.ReturnAndClearReferences(buffer.Data, buffer.Count);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Dispose()
{
if (_sortedKeys != null)
{
ArrayPool<FormKey>.Shared.Return(_sortedKeys);
ArrayPool<FormKey>.Shared.Return(_sortedKeys, _length);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
<Compile Include="$(SharedSourceRoot)LinkerFlags.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Components\ComponentsActivityLinkStore.cs" LinkBase="Shared" />
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadManager.cs" LinkBase="HotReload" />

<Compile Include="$(SharedSourceRoot)PropertyHelper\**\*.cs" />

<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Microsoft.AspNetCore.Http.HttpResponse</Description>
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryItemDebugView.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryDebugView.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)ValueStringBuilder\ValueListBuilder.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<ItemGroup>
<Compile Include="$(RepoRoot)src\Shared\TaskToApm.cs" Link="Streams\TaskToApm.cs" />
<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public static async ValueTask StoreAsync(string key, OutputCacheEntry value, Has
{
if (store is IOutputCacheBufferStore bufferStore)
{
await bufferStore.SetAsync(key, new(buffer.GetCommittedMemory()), CopyToLeasedMemory(tags, out var lease), duration, cancellationToken);
ReadOnlyMemory<string> leasedTags = CopyToLeasedMemory(tags, out var lease);
await bufferStore.SetAsync(key, new(buffer.GetCommittedMemory()), leasedTags, duration, cancellationToken);
if (lease is not null)
{
ArrayPool<string>.Shared.Return(lease);
ArrayPool<string>.Shared.Return(lease, leasedTags.Length);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public RecyclableArrayBufferWriter()
public void Dispose()
{
var tmp = _buffer;
var count = _index;
_index = 0;
_buffer = Array.Empty<T>();
if (tmp.Length != 0)
{
ArrayPool<T>.Shared.Return(tmp);
ArrayPool<T>.Shared.ReturnAndClearReferences(tmp, count);
}
}

Expand Down Expand Up @@ -120,7 +121,7 @@ private void CheckAndResizeBuffer(int sizeHint)
oldArray.AsSpan(0, _index).CopyTo(_buffer);
if (oldArray.Length != 0)
{
ArrayPool<T>.Shared.Return(oldArray);
ArrayPool<T>.Shared.ReturnAndClearReferences(oldArray, _index);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<ItemGroup>
<Compile Include="$(SharedSourceRoot)ValueStringBuilder\**\*.cs" />
<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Mvc/Mvc.NewtonsoftJson/src/JsonArrayPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public void Return(T[]? array)
{
ArgumentNullException.ThrowIfNull(array);

_inner.Return(array);
_inner.ReturnAndClearReferences(array, array.Length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="..\..\Mvc.Core\src\Infrastructure\AsyncEnumerableReader.cs" />
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
Expand Down
40 changes: 40 additions & 0 deletions src/Shared/Buffers/ArrayPoolExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;

namespace System.Buffers;

internal static class ArrayPoolExtensions
{
/// <summary>
/// Clears the specified range and returns the array to the pool.
/// </summary>
public static void Return<T>(this ArrayPool<T> pool, T[] array, int lengthToClear)
Copy link
Member

Choose a reason for hiding this comment

The 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 ReturnAndClearReferences one?

Copy link
Member

@JamesNK JamesNK Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for cases were it's known that T is a reference type.

Do you think ReturnAndClearReferences should be used everywhere? I assume RuntimeHelpers.IsReferenceOrContainsReferences<T>() check is optimized away by JIT so they end up being the same anyway.

{
array.AsSpan(0, lengthToClear).Clear();
pool.Return(array);
}

/// <summary>
/// Clears the specified range if <typeparamref name="T"/> is a reference type or
/// contains references and returns the array to the pool.
/// </summary>
/// <remarks>
/// For .NET Framework, falls back to checking if <typeparamref name="T"/> is not a primitive type
/// where <c>RuntimeHelpers.IsReferenceOrContainsReferences&lt;T&gt;()</c> is not available.
/// </remarks>
public static void ReturnAndClearReferences<T>(this ArrayPool<T> pool, T[] array, int lengthToClear)
{
#if NET
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
#else
if (!typeof(T).IsPrimitive)
#endif
{
array.AsSpan(0, lengthToClear).Clear();
}

pool.Return(array);
}
}
4 changes: 2 additions & 2 deletions src/Shared/ValueStringBuilder/ValueListBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void Dispose()
if (toReturn != null)
{
_arrayFromPool = null;
ArrayPool<T>.Shared.Return(toReturn);
ArrayPool<T>.Shared.ReturnAndClearReferences(toReturn, _pos);
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ private void Grow(int additionalCapacityRequired = 1)
_span = _arrayFromPool = array;
if (toReturn != null)
{
ArrayPool<T>.Shared.Return(toReturn);
ArrayPool<T>.Shared.ReturnAndClearReferences(toReturn, _pos);
}
}
}
Loading
Loading