Skip to content

Commit 3cd9a03

Browse files
committed
Add ArrayPool<T>.Return(T[], int) extension
1 parent 254666f commit 3cd9a03

18 files changed

+87
-37
lines changed

src/Components/Components/src/Microsoft.AspNetCore.Components.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<Compile Include="$(SharedSourceRoot)UrlDecoder\UrlDecoder.cs" LinkBase="Shared" />
2727
<Compile Include="$(SharedSourceRoot)Metrics\MetricsConstants.cs" LinkBase="Shared" />
2828
<Compile Include="$(SharedSourceRoot)Components\ComponentsActivityLinkStore.cs" LinkBase="Shared" />
29+
<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
2930
</ItemGroup>
3031

3132
<Import Project="Microsoft.AspNetCore.Components.Routing.targets" />

src/Components/Components/src/NavigationManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,7 @@ protected async ValueTask<bool> NotifyLocationChangingAsync(string uri, string?
447447
}
448448
finally
449449
{
450-
locationChangingHandlersCopy.AsSpan(0, handlerCount).Clear();
451-
ArrayPool<Func<LocationChangingContext, ValueTask>>.Shared.Return(locationChangingHandlersCopy);
450+
ArrayPool<Func<LocationChangingContext, ValueTask>>.Shared.Return(locationChangingHandlersCopy, handlerCount);
452451
}
453452
}
454453

src/Components/Endpoints/src/FormMapping/Converters/CollectionAdapters/ArrayPoolBufferAdapter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public static PooledBuffer Add(ref PooledBuffer buffer, TElement element)
2121

2222
if (RuntimeHelpers.IsReferenceOrContainsReferences<TElement>())
2323
{
24-
buffer.Data.AsSpan(0, buffer.Count).Clear();
24+
ArrayPool<TElement>.Shared.Return(buffer.Data, buffer.Count);
25+
}
26+
else
27+
{
28+
ArrayPool<TElement>.Shared.Return(buffer.Data);
2529
}
26-
27-
ArrayPool<TElement>.Shared.Return(buffer.Data);
2830

2931
buffer.Data = newBuffer;
3032
}
@@ -39,10 +41,12 @@ public static TCollection ToResult(PooledBuffer buffer)
3941

4042
if (RuntimeHelpers.IsReferenceOrContainsReferences<TElement>())
4143
{
42-
buffer.Data.AsSpan(0, buffer.Count).Clear();
44+
ArrayPool<TElement>.Shared.Return(buffer.Data, buffer.Count);
45+
}
46+
else
47+
{
48+
ArrayPool<TElement>.Shared.Return(buffer.Data);
4349
}
44-
45-
ArrayPool<TElement>.Shared.Return(buffer.Data);
4650

4751
return result;
4852
}

src/Components/Endpoints/src/FormMapping/PrefixResolver.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public void Dispose()
3838
{
3939
if (_sortedKeys != null)
4040
{
41-
_sortedKeys.AsSpan(0, _length).Clear();
42-
ArrayPool<FormKey>.Shared.Return(_sortedKeys);
41+
ArrayPool<FormKey>.Shared.Return(_sortedKeys, _length);
4342
}
4443
}
4544

src/Components/Endpoints/src/Microsoft.AspNetCore.Components.Endpoints.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@
3535
<Compile Include="$(SharedSourceRoot)LinkerFlags.cs" LinkBase="Shared" />
3636
<Compile Include="$(SharedSourceRoot)Components\ComponentsActivityLinkStore.cs" LinkBase="Shared" />
3737
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadManager.cs" LinkBase="HotReload" />
38-
3938
<Compile Include="$(SharedSourceRoot)PropertyHelper\**\*.cs" />
40-
39+
<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
4140
</ItemGroup>
4241

4342
<ItemGroup>

src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Microsoft.AspNetCore.Http.HttpResponse</Description>
3232
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryItemDebugView.cs" LinkBase="Shared" />
3333
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryDebugView.cs" LinkBase="Shared" />
3434
<Compile Include="$(SharedSourceRoot)ValueStringBuilder\ValueListBuilder.cs" LinkBase="Shared" />
35+
<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
3536
</ItemGroup>
3637

3738
<ItemGroup>

src/Middleware/OutputCaching/src/Microsoft.AspNetCore.OutputCaching.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

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

2829
</Project>

src/Middleware/OutputCaching/src/OutputCacheEntryFormatter.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,11 @@ public static async ValueTask StoreAsync(string key, OutputCacheEntry value, Has
4646
{
4747
if (store is IOutputCacheBufferStore bufferStore)
4848
{
49-
await bufferStore.SetAsync(key, new(buffer.GetCommittedMemory()), CopyToLeasedMemory(tags, out var lease), duration, cancellationToken);
49+
ReadOnlyMemory<string> leasedTags = CopyToLeasedMemory(tags, out var lease);
50+
await bufferStore.SetAsync(key, new(buffer.GetCommittedMemory()), leasedTags, duration, cancellationToken);
5051
if (lease is not null)
5152
{
52-
if (tags is not null)
53-
{
54-
lease.AsSpan(0, tags.Count).Clear();
55-
}
56-
57-
ArrayPool<string>.Shared.Return(lease);
53+
ArrayPool<string>.Shared.Return(lease, leasedTags.Length);
5854
}
5955
}
6056
else

src/Middleware/OutputCaching/src/RecyclableArrayBufferWriter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ public void Dispose()
4040
{
4141
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
4242
{
43-
tmp.AsSpan(0, count).Clear();
43+
ArrayPool<T>.Shared.Return(tmp, count);
44+
}
45+
else
46+
{
47+
ArrayPool<T>.Shared.Return(tmp);
4448
}
45-
46-
ArrayPool<T>.Shared.Return(tmp);
4749
}
4850
}
4951

@@ -129,10 +131,12 @@ private void CheckAndResizeBuffer(int sizeHint)
129131
{
130132
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
131133
{
132-
oldArray.AsSpan(0, _index).Clear();
134+
ArrayPool<T>.Shared.Return(oldArray, _index);
135+
}
136+
else
137+
{
138+
ArrayPool<T>.Shared.Return(oldArray);
133139
}
134-
135-
ArrayPool<T>.Shared.Return(oldArray);
136140
}
137141
}
138142

src/Middleware/WebSockets/src/Microsoft.AspNetCore.WebSockets.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
<ItemGroup>
2121
<Compile Include="$(SharedSourceRoot)ValueStringBuilder\**\*.cs" />
22+
<Compile Include="$(SharedSourceRoot)Buffers\ArrayPoolExtensions.cs" LinkBase="Shared" />
2223
</ItemGroup>
2324

2425
<ItemGroup>

0 commit comments

Comments
 (0)