Skip to content

Commit 119d33c

Browse files
committed
source が書き変わっちゃうことを認める代わりにアロケーション0のGroupBy
GroupByとかToLookupとかの GC コストが結構高くて困っており。 sourceが「直前で作ったばかりで、他に誰も使わず、書き換えても問題ない」みたいな状況が結構あったので、「書き換わる前提の Span 限定の GroupBy」を実装。 Sortして、連続して同じkeyが並んでる区間を列挙。
1 parent 510542c commit 119d33c

File tree

12 files changed

+241
-0
lines changed

12 files changed

+241
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[*]
2+
charset = utf-8
3+
end_of_line = lf
4+
5+
csharp_style_namespace_declarations=file_scoped:suggestion
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\InPlaceGroupBy\InPlaceGroupBy.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Running;
3+
using InPlaceGroupBy;
4+
using System.Runtime.CompilerServices;
5+
6+
BenchmarkRunner.Run<InPlaceGroupByBenchmark>();
7+
8+
[MemoryDiagnoser]
9+
public class InPlaceGroupByBenchmark
10+
{
11+
public static (string key, int value)[] Data =
12+
[
13+
("a", 1), ("ab", 2), ("abc", 3), ("a", 4), ("ab", 5),
14+
("a", 6), ("a", 7), ("abc", 8), ("a", 9), ("a", 10),
15+
("a", 11), ("ab", 12), ("abc", 13), ("b", 14), ("ab", 15),
16+
("a", 16), ("a", 17), ("bc", 18), ("a", 19), ("b", 20),
17+
("a", 21), ("ab", 22), ("ac", 23), ("a", 24), ("ab", 25),
18+
("a", 26), ("d", 27), ("abc", 28), ("a", 29), ("ac", 30),
19+
];
20+
21+
[Benchmark]
22+
public void Linq()
23+
{
24+
var values = (stackalloc int[30]);
25+
var i = 0;
26+
27+
foreach (var g in Data.GroupBy(x => x.key))
28+
{
29+
values[i++] = g.Sum(x => x.value);
30+
}
31+
}
32+
33+
[InlineArray(32)]
34+
private struct Buffer<T>
35+
{
36+
private T _value;
37+
}
38+
39+
[Benchmark]
40+
public void InPlaceSpan()
41+
{
42+
var buffer = new Buffer<(string key, int value)>();
43+
Span<(string key, int value)> span = buffer;
44+
Data.CopyTo(span); // needs copy
45+
46+
var values = (stackalloc int[30]);
47+
var i = 0;
48+
49+
foreach (var g in span.GroupBy((x, y) => x.key.AsSpan().CompareTo(y.key, StringComparison.Ordinal)))
50+
{
51+
values[i++] = g.Sum(x => x.value);
52+
}
53+
}
54+
}
55+
56+
file static class Ex
57+
{
58+
public static int Sum<T>(this Span<T> span, Func<T, int> selector)
59+
{
60+
var sum = 0;
61+
foreach (var item in span) sum += selector(item);
62+
return sum;
63+
}
64+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Solution>
2+
<Project Path="BenchmarkInPlaceGroupBy/BenchmarkInPlaceGroupBy.csproj" Id="6964974f-b77a-42f7-8c80-5ad9a4595a43" />
3+
<Project Path="InPlaceGroupBy/InPlaceGroupBy.csproj" Id="852225a0-e903-4d69-bec2-c9ea24607280" />
4+
<Project Path="TestInPlaceGroupBy/TestInPlaceGroupBy.csproj" Id="cb9d56bd-0431-484e-9414-37bf232801f3" />
5+
</Solution>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace InPlaceGroupBy;
2+
3+
public static class InPlaceSpanExtensions
4+
{
5+
public static SortedSpanGrouping<T> GroupBy<T>(this Span<T> span, Comparison<T> comparison)
6+
{
7+
span.Sort(comparison);
8+
return new(span, comparison);
9+
}
10+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace InPlaceGroupBy;
2+
3+
public readonly ref struct SortedSpanGrouping<T>(Span<T> span, Comparison<T> comparison)
4+
{
5+
private readonly Span<T> _span = span;
6+
private readonly Comparison<T> _comparison = comparison;
7+
8+
public Enumerator GetEnumerator() => new(this);
9+
10+
public ref struct Enumerator(SortedSpanGrouping<T> source)
11+
{
12+
private readonly SortedSpanGrouping<T> _source = source;
13+
private int _start;
14+
private int _end;
15+
16+
public bool MoveNext()
17+
{
18+
if (_end >= _source._span.Length) return false;
19+
20+
var first = _source._span[_end];
21+
_start = _end;
22+
while (true)
23+
{
24+
++_end;
25+
if (_end >= _source._span.Length
26+
|| _source._comparison(first, _source._span[_end]) != 0) return true;
27+
}
28+
}
29+
30+
public readonly Span<T> Current => _source._span[_start.._end];
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace TestInPlaceGroupBy;
2+
3+
class Common
4+
{
5+
public static (string key, int value)[] Data = [
6+
("a", 1), ("ab", 2), ("abc", 3), ("a", 4), ("ab", 5),
7+
("a", 6), ("a", 7), ("abc", 8), ("a", 9), ("a", 10),
8+
("a", 11), ("ab", 12), ("abc", 13), ("b", 14), ("ab", 15),
9+
("a", 16), ("a", 17), ("bc", 18), ("a", 19), ("b", 20),
10+
("a", 21), ("ab", 22), ("ac", 23), ("a", 24), ("ab", 25),
11+
("a", 26), ("d", 27), ("abc", 28), ("a", 29), ("ac", 30),
12+
];
13+
14+
public static int Compare((string key, int value) x, (string key, int value) y)
15+
{
16+
return x.key.AsSpan().CompareTo(y.key, StringComparison.Ordinal);
17+
}
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using InPlaceGroupBy;
2+
3+
namespace TestInPlaceGroupBy;
4+
5+
using static Common;
6+
7+
public class InPlaceGroupByTest
8+
{
9+
[Fact]
10+
public void EquivalentToLinq()
11+
{
12+
var g1 = Data.GroupBy(x => x.key);
13+
14+
var data = Data.ToArray().AsSpan();
15+
var g2 = data.GroupBy(Compare);
16+
17+
Equals(g1, g2);
18+
}
19+
20+
private static void Equals(IEnumerable<IGrouping<string, (string key, int value)>> g1, SortedSpanGrouping<(string key, int value)> g2)
21+
{
22+
// Same count
23+
var count = 0;
24+
{
25+
var e = g2.GetEnumerator();
26+
while (e.MoveNext()) count++;
27+
}
28+
Assert.Equal(g1.Count(), count);
29+
30+
var d = g1.ToDictionary(x => x.Key);
31+
32+
foreach (var g in g2)
33+
{
34+
var key = g[0].key;
35+
36+
// Same keys
37+
Assert.True(d.TryGetValue(key, out var g1Item));
38+
39+
// Same values but order is not guaranteed
40+
Assert.Equal(
41+
g1Item.Select(x => x.value).Order(),
42+
g.ToArray().Select(x => x.value).Order());
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)