Writing High Performance C# 7 Code
Fons Sonnemans
@fonssonnemans
Fons Sonnemans
• Software Development Consultant
• Programming Languages
• Clipper, Smalltalk, Visual Basic, C#
• Platforms
• Windows Forms, ASP.NET, XAML (Silverlight, WPF, Windows Phone, Windows 10, Blend)
• Databases
• MS SQL Server, Oracle
• Role
• Trainer, Coach, Advisor, Architect, Designer, Windows App Developer
• www.reflectionit.nl/training
• www.reflectionit.nl/apps
2
Audience
• Who is not a Software Developer
• Who is not a C# developer
• Who is not using C# 7
• Who is not using C# 7.3
• Who is not using .NET Core 2.0 or 2.1
• Who is not creating 64 bits apps
• Who is not using ReSharper
• Who is not using (or writing) Code Analyzers
• Who does not understand the .NET Garbage Collector (GC)
• Who is not having Performance Issues
• Who is not writing Structs
3
How to solve Performance Issues
• Choose fast algorithms
• Choose optimal data structures
• Optimize memory usage
• Implement caching
• Think over architecture
• Layers, Tiers, UI, (Web) API, Message Queues, Cloud, Security, Transactions
(locking), Threading (async)
• Optimize networking (HTTP, SQL)
• Work chunky not chatty
• Optimize I/O
• Where are you writing your Trace information, files, databases, cloud or ETW?
• https://www.youtube.com/watch?v=XfkpjlP0Nq4
4
What’s new in C# 7
• C# 7.0
• Out Variables
• Binary literals & Digit separators
• Pattern Matching
• Local Functions
• Tuples
• Deconstruction
• Discards
• More expression bodied members
• Throw expressions
• Generalized async return types
• Ref Returns and Ref Locals
• C# 7.1
• Default Expressions
• async Main
• Infer tuple names
• Pattern-matching with Generics
• C# 7.2
• Reference semantics with value types
• In parameters, ref readonly, readonly struct, ref struct
• private protected access modifier
• Non-trailing named arguments
• Ref & In Extension Methods
• Conditional Ref Operator
• C# 7.3
• new generic constraints
• Attributes on backing fields
• Ref local re-assignment
• Stackalloc initializers
• Expression variables in initializers
• Tuple comparison
• Improved overload candidates
Topics
• Measuring & Tools
• Stopwatch
• Diagnostic Tools
• Performance Profiler
• Benchmark.NET
• ILSpy & SharpLab.io
• Garbage Collection
• Large Object Heap
• Collection Capacity
• ArrayPool<T>
• Writing Structs
• C# 7.0: Anonymous Types vs ValueTuples
• BigStruct: ByValue & ByRef
• StuctLayout
• Boxing & Unboxing
• CLR Heap Allocation Analyzer
• Equals & GetHashCode
• Defensive Copy
• C# 7.x
• In parameters,
• Ref (Readonly) Return & Ref (Readonly) Local
• Readonly structs
• ErrorProne.NET.Structs
• Ref structs, Span<T>, Stackalloc
6
Heap
Reference Types + Value Types storedin Fields of Reference Types
.NET Memory - Stack & Heap
7
Stack
Value Types + Pointers
1MB on 32Bit
4MB on 64Bit
Thread
1
CPU Cache
L1 -> L2 -> L3
Thread
2
Thread
N
Large Object Heap
Objects larger than 85KB,
mostly arrays
Gen 2 + Not Compacted
?
Small Object Heap
Gen 0
Short-lived
~256KB
Gen 1
“Buffer”
~2MB
Gen 2
Long-lived
~10MB
GC GC
L2
Code
Heap
Process
Heap
Value Types vs. Reference Types
• Memory Overhead
• http://adamsitnik.com/Value-Types-vs-Reference-Types/
• Value Type (PointStruct)
• Reference Type (PointClass)
8
X Y
X
Y
Total: 8 Bytes
Total: 8 Bytes
Total: 16 Bytes
Total: 24 Bytes
@fonssonnemans
fons.sonnemans@reflectionit.nl
fonssonnemans
reflectionit.nl/blog
9
https://reflectionit.nl/training/csharp
10

Writing High Peformance C# 7 Code

  • 1.
    Writing High PerformanceC# 7 Code Fons Sonnemans @fonssonnemans
  • 2.
    Fons Sonnemans • SoftwareDevelopment Consultant • Programming Languages • Clipper, Smalltalk, Visual Basic, C# • Platforms • Windows Forms, ASP.NET, XAML (Silverlight, WPF, Windows Phone, Windows 10, Blend) • Databases • MS SQL Server, Oracle • Role • Trainer, Coach, Advisor, Architect, Designer, Windows App Developer • www.reflectionit.nl/training • www.reflectionit.nl/apps 2
  • 3.
    Audience • Who isnot a Software Developer • Who is not a C# developer • Who is not using C# 7 • Who is not using C# 7.3 • Who is not using .NET Core 2.0 or 2.1 • Who is not creating 64 bits apps • Who is not using ReSharper • Who is not using (or writing) Code Analyzers • Who does not understand the .NET Garbage Collector (GC) • Who is not having Performance Issues • Who is not writing Structs 3
  • 4.
    How to solvePerformance Issues • Choose fast algorithms • Choose optimal data structures • Optimize memory usage • Implement caching • Think over architecture • Layers, Tiers, UI, (Web) API, Message Queues, Cloud, Security, Transactions (locking), Threading (async) • Optimize networking (HTTP, SQL) • Work chunky not chatty • Optimize I/O • Where are you writing your Trace information, files, databases, cloud or ETW? • https://www.youtube.com/watch?v=XfkpjlP0Nq4 4
  • 5.
    What’s new inC# 7 • C# 7.0 • Out Variables • Binary literals & Digit separators • Pattern Matching • Local Functions • Tuples • Deconstruction • Discards • More expression bodied members • Throw expressions • Generalized async return types • Ref Returns and Ref Locals • C# 7.1 • Default Expressions • async Main • Infer tuple names • Pattern-matching with Generics • C# 7.2 • Reference semantics with value types • In parameters, ref readonly, readonly struct, ref struct • private protected access modifier • Non-trailing named arguments • Ref & In Extension Methods • Conditional Ref Operator • C# 7.3 • new generic constraints • Attributes on backing fields • Ref local re-assignment • Stackalloc initializers • Expression variables in initializers • Tuple comparison • Improved overload candidates
  • 6.
    Topics • Measuring &Tools • Stopwatch • Diagnostic Tools • Performance Profiler • Benchmark.NET • ILSpy & SharpLab.io • Garbage Collection • Large Object Heap • Collection Capacity • ArrayPool<T> • Writing Structs • C# 7.0: Anonymous Types vs ValueTuples • BigStruct: ByValue & ByRef • StuctLayout • Boxing & Unboxing • CLR Heap Allocation Analyzer • Equals & GetHashCode • Defensive Copy • C# 7.x • In parameters, • Ref (Readonly) Return & Ref (Readonly) Local • Readonly structs • ErrorProne.NET.Structs • Ref structs, Span<T>, Stackalloc 6
  • 7.
    Heap Reference Types +Value Types storedin Fields of Reference Types .NET Memory - Stack & Heap 7 Stack Value Types + Pointers 1MB on 32Bit 4MB on 64Bit Thread 1 CPU Cache L1 -> L2 -> L3 Thread 2 Thread N Large Object Heap Objects larger than 85KB, mostly arrays Gen 2 + Not Compacted ? Small Object Heap Gen 0 Short-lived ~256KB Gen 1 “Buffer” ~2MB Gen 2 Long-lived ~10MB GC GC L2 Code Heap Process Heap
  • 8.
    Value Types vs.Reference Types • Memory Overhead • http://adamsitnik.com/Value-Types-vs-Reference-Types/ • Value Type (PointStruct) • Reference Type (PointClass) 8 X Y X Y Total: 8 Bytes Total: 8 Bytes Total: 16 Bytes Total: 24 Bytes
  • 9.
  • 10.