Repository navigation
Expand file tree
/
Copy pathImmutableArrayBenchmark.cs
More file actions
89 lines (80 loc) · 2.85 KB
/
Copy pathImmutableArrayBenchmark.cs
File metadata and controls
89 lines (80 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
namespace PracticalDotNetCollections.Benchmarks.Memory;
/// <summary>
/// Benchmarks evaluating <see cref="ImmutableArray{T}"/> performance footprints against a standard <see cref="List{T}"/>.
/// Measures item mutation loops, optimization paths using builders, and hot-path memory read indexing speeds.
/// </summary>
[MemoryDiagnoser]
public class ImmutableArrayBenchmark {
// Initialized using safe, struct-valid default states instead of reference nulls
private ImmutableArray<int> _immutableArray = ImmutableArray<int>.Empty;
private List<int> _list = null!;
private uint _readIndex;
/// <summary>
/// The number of entries processed inside the collections.
/// </summary>
[Params(1_000, 10_000)]
public int N;
/// <summary>
/// Sets up the populated data structures for read-only tracking loops.
/// </summary>
[GlobalSetup]
public void Setup() {
var data = Enumerable.Range(0, 100_000).ToArray();
_immutableArray = data.ToImmutableArray();
_list = data.ToList();
}
/// <summary>
/// Demonstrates the catastrophic loop anti-pattern: allocating a brand new array chunk on every single entry modification.
/// </summary>
[Benchmark]
public ImmutableArray<int> ImmutableArrayAdd() {
var array = ImmutableArray<int>.Empty;
for (int i = 0; i < N; i++) {
array = array.Add(i);
}
return array;
}
/// <summary>
/// Demonstrates the optimization fix: using an internal builder block to construct an immutable structure with linear performance.
/// </summary>
[Benchmark]
public ImmutableArray<int> ImmutableArrayWithBuilderAdd() {
var builder = ImmutableArray.CreateBuilder<int>();
for (int i = 0; i < N; i++) {
builder.Add(i);
}
return builder.ToImmutable();
}
/// <summary>
/// Measures baseline sequential entry mutations into a standard mutable List.
/// </summary>
[Benchmark]
public List<int> ListAdd() {
var list = new List<int>();
for (int i = 0; i < N; i++) {
list.Add(i);
}
return list;
}
/// <summary>
/// Measures lookup performance from an immutable array via direct, raw memory pointer index mappings.
/// </summary>
[Benchmark]
public int ImmutableArrayRead() {
int targetIndex = (int)(_readIndex++ % 100_000);
return _immutableArray[targetIndex];
}
/// <summary>
/// Measures lookup performance from a standard list across the exact same reading boundaries.
/// </summary>
[Benchmark]
public int ListRead() {
int targetIndex = (int)(_readIndex++ % 100_000);
return _list[targetIndex];
}
}