Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d062934

Browse files
Alexjkotas
authored andcommitted
Use stackalloc in string.Split (#15435)
* Use stackalloc in string.Split * Added initial usage of ValueListBuilder * Added usage of list builder to string separator Split overloads
1 parent 9fd31bd commit d062934

File tree

4 files changed

+196
-129
lines changed

4 files changed

+196
-129
lines changed

‎src/mscorlib/System.Private.CoreLib.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,4 +670,4 @@
670670
</ItemGroup>
671671

672672
<Import Project="GenerateCompilerResponseFile.targets" />
673-
</Project>
673+
</Project>

‎src/mscorlib/shared/System.Private.CoreLib.Shared.projitems‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\KeyNotFoundException.cs" />
7474
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\KeyValuePair.cs" />
7575
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\NonRandomizedStringEqualityComparer.cs" />
76+
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\ValueListBuilder.cs" />
7677
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\List.cs" />
7778
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\HashHelpers.cs" />
7879
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\ICollection.cs" />
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Buffers;
6+
using System.Diagnostics;
7+
using System.Runtime.CompilerServices;
8+
9+
namespace System.Collections.Generic
10+
{
11+
internal ref struct ValueListBuilder<T>
12+
{
13+
private Span<T> _span;
14+
private T[] _arrayFromPool;
15+
private int _pos;
16+
17+
public ValueListBuilder(Span<T> initialSpan)
18+
{
19+
_span = initialSpan;
20+
_arrayFromPool = null;
21+
_pos = 0;
22+
}
23+
24+
public int Length => _pos;
25+
26+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
27+
public void Append(T item)
28+
{
29+
int pos = _pos;
30+
if (pos >= _span.Length)
31+
Grow();
32+
33+
_span[pos] = item;
34+
_pos = pos + 1;
35+
}
36+
37+
public ReadOnlySpan<T> AsReadOnlySpan()
38+
{
39+
return _span.Slice(0, _pos);
40+
}
41+
42+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43+
public void Dispose()
44+
{
45+
if (_arrayFromPool != null)
46+
{
47+
ArrayPool<T>.Shared.Return(_arrayFromPool);
48+
_arrayFromPool = null;
49+
}
50+
}
51+
52+
private void Grow()
53+
{
54+
T[] array = ArrayPool<T>.Shared.Rent(_span.Length * 2);
55+
56+
bool success = _span.TryCopyTo(array);
57+
Debug.Assert(success);
58+
59+
T[] toReturn = _arrayFromPool;
60+
_span = _arrayFromPool = array;
61+
if (toReturn != null)
62+
{
63+
ArrayPool<T>.Shared.Return(toReturn);
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)