Turbocharged: Writing High-Performance C# and .NET Code - Steve Gordon - NDC Oslo 2024

Learn how to write high-performance C# and .NET code with proven techniques for optimizing memory usage, reducing allocations, and improving throughput in production.

Key takeaways
  • Always measure and benchmark performance before attempting optimizations to establish baselines and identify hot paths
  • Use Span<T> for efficient, low-allocation processing of contiguous memory regions (strings, arrays, etc.) without creating copies
  • ArrayPool can help reduce allocations by reusing arrays instead of creating new ones for short-lived operations
  • System.Text.Json provides better performance than Newtonsoft.Json with built-in support for modern APIs like Span<T>
  • Focus optimization efforts on frequently called code paths that handle large amounts of data or have high throughput requirements
  • Benchmark.NET is the preferred tool for accurate micro-benchmarking, providing detailed metrics about execution time and allocations
  • Consider readability vs performance tradeoffs - highly optimized code can be more complex and harder to maintain
  • Production monitoring and metrics are essential to validate that optimizations translate to real-world improvements
  • Memory allocations can significantly impact performance through increased GC pressure, especially in high-throughput scenarios
  • System.IO.Pipelines offers high-performance APIs for streaming scenarios with efficient buffer management