High-performance code design patterns in C#
summary
A practical checklist of concrete patterns and results: use Span<T> zero-copy and Memory<T> to slice without copying (used in Kestrel), rent buffers from ArrayPool/MemoryPool to cut allocations (benchmarks show almost 2x speed and near-zero allocations), prefer struct-of-arrays for hot loops (example yields ~10x speed), use stackalloc for tiny temporaries (≈33% faster, another ~10% if zeroing is skipped), and replace repeated string concatenation with buffer builders/stringbuilder (GC time drops ~60%→~15%, throughput rises ~9→150 rps).
AI-written summary · about
chapters (30)
- 0:00Introduction
- 1:48Design patterns in C
- 7:26Collection
- 13:50Polling
- 18:47Zero Copy
- 26:54Array ofstructs
- 27:21Array of classes
- 29:52Array of arrays
- 32:56Entity component system
- 34:10Avoid the managed heap
- 34:32Fastest allocation
- 35:05structs
- 36:08stack a lock
- 36:45stack overflow exception
- 38:25temporary data
- 39:04runtime guarantee
- 40:12zeroing memory
- 40:29faster code
- 41:53stock log
- 43:00fixed size buffers
- 43:33stockbased data
- 43:50buffered builder
- 44:42string builder
- 45:46load test
- 46:41throughput
- 47:18Buffer builder
- 48:27Buffer buffered builders
- 49:18Memory usage
- 49:31Other patterns
- 50:31Questions
related talks
01h 04mTurbocharged: Writing High-Performance C# and .NET Code
52mPerformance features in C#
56mState of the .NET Performance
01h 14mTurbocharged: Writing High-performance C# and .NET code
01h 01mTurbocharged: Writing High-Performance C# and .NET Code
59mWriting (in)efficient C#
59mPerformance tricks I learned from contributing to open source .NET packages
01h 00mMetaprogramming for the masses
