Presentation used for the DotNetZuid meeting 'Writing High Performance C# 7 Code' on 18-sept-2018
https://www.dotnetzuid.nl/Meeting/31/writing-high-performance-c-7x-code
2. 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
3. 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
4. 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
5. 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
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