Skip to content

Commit 8d75bb2

Browse files
committed
Added example for chunkedlist
1 parent 1134844 commit 8d75bb2

File tree

8 files changed

+161
-1
lines changed

8 files changed

+161
-1
lines changed

ChunkedList/Benchmarks.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using BenchmarkDotNet;
3+
using BenchmarkDotNet.Attributes;
4+
5+
namespace ChunkedList
6+
{
7+
public class Benchmarks
8+
{
9+
[Benchmark]
10+
public void Scenario1()
11+
{
12+
// Implement your benchmark here
13+
}
14+
15+
[Benchmark]
16+
public void Scenario2()
17+
{
18+
// Implement your benchmark here
19+
}
20+
}
21+
}

ChunkedList/ChunkedList.cs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
3+
public class ChunkedList<T>
4+
{
5+
private const int ChunkSize = 8000;
6+
private T[][] _chunks;
7+
8+
public ChunkedList()
9+
{
10+
_chunks = new T[1][];
11+
_chunks[0] = new T[ChunkSize];
12+
Count = 0;
13+
}
14+
15+
public T this[int index]
16+
{
17+
get
18+
{
19+
ArgumentOutOfRangeException.ThrowIfNegative(index);
20+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
21+
22+
var chunkIndex = index / ChunkSize;
23+
var innerIndex = index % ChunkSize;
24+
return _chunks[chunkIndex][innerIndex];
25+
}
26+
set
27+
{
28+
ArgumentOutOfRangeException.ThrowIfNegative(index);
29+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
30+
31+
var chunkIndex = index / ChunkSize;
32+
var innerIndex = index % ChunkSize;
33+
_chunks[chunkIndex][innerIndex] = value;
34+
}
35+
}
36+
37+
public void Add(T item)
38+
{
39+
if (Count == _chunks.Length * ChunkSize)
40+
{
41+
// Create a new larger set of chunks
42+
var newChunks = new T[_chunks.Length + 1][];
43+
Array.Copy(_chunks, newChunks, _chunks.Length);
44+
newChunks[_chunks.Length] = new T[ChunkSize];
45+
_chunks = newChunks;
46+
}
47+
48+
var addToChunk = Count / ChunkSize;
49+
var addToIndex = Count % ChunkSize;
50+
_chunks[addToChunk][addToIndex] = item;
51+
52+
Count++;
53+
}
54+
55+
public int Count { get; private set; }
56+
57+
public void Clear()
58+
{
59+
Count = 0;
60+
}
61+
}

ChunkedList/ChunkedList.csproj

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
</PropertyGroup>
6+
<PropertyGroup>
7+
<PlatformTarget>AnyCPU</PlatformTarget>
8+
<DebugType>pdbonly</DebugType>
9+
<DebugSymbols>true</DebugSymbols>
10+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
11+
<Optimize>true</Optimize>
12+
<Configuration>Release</Configuration>
13+
<IsPackable>false</IsPackable>
14+
</PropertyGroup>
15+
<ItemGroup>
16+
<PackageReference Include="BenchmarkDotNet" Version="0.13.7"/>
17+
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.7" Condition="'$(OS)' == 'Windows_NT'"/>
18+
</ItemGroup>
19+
</Project>

ChunkedList/ChunkedList.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChunkedList", "ChunkedList.csproj", "{8321266C-D577-4147-8486-6E1C591F4763}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{8321266C-D577-4147-8486-6E1C591F4763}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{8321266C-D577-4147-8486-6E1C591F4763}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{8321266C-D577-4147-8486-6E1C591F4763}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{8321266C-D577-4147-8486-6E1C591F4763}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

ChunkedList/Program.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Running;
4+
using ChunkedList;
5+
6+
BenchmarkRunner.Run<Benchmarks>();
7+
8+
[MemoryDiagnoser]
9+
public class ListBenchmarks
10+
{
11+
[Params(1000, 20_0000)] public int Items { get; set; }
12+
13+
[Benchmark(Baseline = true)]
14+
public List<int> List()
15+
{
16+
var list = new List<int>();
17+
for (var i = 0; i < Items; i++)
18+
{
19+
list.Add(i);
20+
}
21+
22+
return list;
23+
}
24+
25+
[Benchmark]
26+
public ChunkedList<int> ChunkedList()
27+
{
28+
var list = new ChunkedList<int>();
29+
for (var i = 0; i < Items; i++)
30+
{
31+
list.Add(i);
32+
}
33+
34+
return list;
35+
}
36+
}

ChunkedList/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Enabling List<T> to store large amounts of elements
2+
3+
`List<T>` is one of the most versatile collection types in .NET. As it is meant for general-purpose use, it is not optimized for any specific use case. So, if we look closely enough, we will find scenarios where it falls short. One of these scenarios is when you have lots of data. This article will look at precisely this.
4+
5+
6+
Found [here](https://steven-giesel.com/blogPost/37b32c2a-245f-4cb9-9e32-73a1091b025b)

FSharpCombined/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# .NET 8 Performance Edition
1+
# The combined power of F# and C#
22

33
Where C# is the most dominant language in the .NET world, other languages are built on top of the Framework that deserves their respective place. F# is strong when it comes down to functional programming! In this blog post, we will leverage the power of F# and C# to showcase where both excel!
44

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
44

55
| BlogPost | Publish Date |
66
| -------------------------------------------------------------------------------------------------------------- | ------------ |
7+
| [Enabling List<T> to store large amounts of elements](ChunkedList/) | 07.09.2023 |
78
| [The combined power of F# and C#](FSharpCombined/) | 30.07.2023 |
89
| [Gracefully Handling Entity Framework Exceptions with EntityFramework.Exceptions](EntityFrameworkExceptions/) | 25.07.2023 |
910
| [Create your own Mediator (like Mediatr)](Mediator/) | 21.06.2023 |

0 commit comments

Comments
 (0)