A presentation at .NET South West by Stuart Lang
Memory Management, C# 7.2 and Span<T>
HELLO! F# |> I ❤ 9 years of experience in .NET Organiser of Bristol F# meetup and DDD South West You can find me at @stuartblang & https://stu.dev
Rust
● FAST ● SAFE ● MAINTAINABLE Can C# have all 3?
C# as a high performance language
C# as a high performance language
Performance where it matters Fr am ew or ks Li br ar ie s
Always Measure
Measure with… ▸ ▸ ▸ ▸ Memory Performance Counters BenchmarkDotNet dotMemory Visual Studio
How it works
“ The Stack Is An Implementation Detail. - Eric Lippert Part One Part Two
Types ▸ Value types ▹ Structs ▹ Enums ▹ Bool ▹ Int, Float, Short, Long… ▹ Byte ▸ Reference types ▹ Class ▹ Interface ▹ Array ▹ String ▹ Delegate
Stack Code sharplab.io demo a=42 Stack Heap x(ref) object: array{1,2,3}
Code - behaviour
Stack Credit: http://www.i-programmer.info/ebooks/deep-c/363
Heap Small Object Heap Gen 0 Gen 1 Large Object Heap Gen 2
Heap Credit: https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/
Great Resource ▸ http://benhall.io/a-super-simplified-exp lanation-of-net-garbage-collection/
Stack vs Heap What? Lifetime When disposed? Time to dispose Stack Local values Local references (just the ref part) Stack frame Deterministic Constant (near instant) Heap Reference object instances Unrestricted GC non-deterministic It depends (non-trivial)
Watch out for hidden allocations ▸ Let’s see some examples
Watch out for hidden allocations
Watch out for hidden allocations
Watch out for hidden allocations
Watch out for hidden allocations
Plugins ▸ R# Heap Allocations Viewer ▸ Roslyn Clr Heap Allocation Analyzer
In Parameters
In Parameters - cont. ▸ Essentially readonly ref ▸ Where you want to want to pass by-ref for performance, with the safety and behaviour or by-value ▸ Watch out for copying with invoking methods on non-readonly structs
Ref extension methods
Ref locals ▸ Reference semantics with value types
Ref locals ▸ Can be thought of as aliases
Ref returns
Ref Struct ▸ Aka ref-like ▸ Stack-only! ▸ To support Span<T>
Ref Struct
“ The great Eric Lippert once wrote “The Stack Is An Implementation Detail”, and basically that’s not true anymore. - Jon Skeet
Span<T> ▸ Abstraction over arbitrary memory Span<T> Marshal.AllocHGlobal() stackalloc [] new []
Span<T>
Span<T>
Span<T> - Why stack-only? ▸ Safe lifetime - cannot outlive stack memory ▸ Safe concurrency - No struct tearing ▸ It’s fast!
Span<T> - Other properties ▸ Efficient representation ▸ GC tracking - refs will be maintained by GC
Use Cases ▸ Alternative to unsafe code ▸ Parsing text ▸ Sharing partial parts of memory
Problem with pointers ▸ ▸ ▸ ▸ Requires GC pinning (for heap memory) Mustn’t escape pointer (for stack) No bounds checking Unsafe code blocks
Problem with pointers
Credit: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/stackalloc
You can still pin
You can still pin - in C# 7.3
BenchmarkDotNet
Results
C# 8 - Slicing with Ranges demo
Span<T> Framework APIs Today netcoreapp2.0 ❌ netcoreapp2.1 ✅ netstandard2.0 ❌ Next netcoreapp2.2 ✅ netcoreapp2.3 ✅ netcoreapp3.0 ✅ netstandard2.1 ✅ ❓ net472 ❌ net48 ❌ net49 ❓
Related topics ▸ ReadOnlySpan<T> ▸ Memory<T> & ReadOnlyMemory<T> ▸ System.Runtime.CompilerServices.Unsafe ▸ MemoryMarshal.AsBytes(span) ▸ MemoryMarshal.Cast<TFrom,TTo>(span)
Libraries using Span<T> ( just some) ▸ Utf8Json ▸ SpanJson ▸ ZeroFormatter ▸ StackExchange.Redis
Case study JustEat.StatsD PRs Low hanging fruit: #55, #59, #63, #65 Master class by dv00d00: #104 - Zero Allocations
Good Reads Adam Sitnik - Span Marc Gravell - Spans and ref Parts 1 & 2 Span<T> spec Vladimir Sadov All About Span: Exploring a New .NET Mainstay Stephen Toub ▸ Maarten Balliauw ▸ ▸ ▸ ▸ ▸
Summary
THANKS! Any questions? You can find me at @stuartblang
Here’s what was said about this presentation on social media.
Best talk I’ve seen in Span<T> great job @stuartblang @dotnetsouthwest pic.twitter.com/RM2PVL4zD0
— Ben Hall (@benhall_io) October 1, 2018
Massive thanks to @stuartblang for tonight's .NET South West talk on .NET Memory Management, C# 7.2 and Span<T>. Was a really great talk with lots to take away.
— Joseph Woodward (@joe_mighty) October 1, 2018
You missed a trick @stuartblang 😂 Was a good clear talk - a refresher on .NET memory management and intro to the new high performance stuff in the latest .NET versions. I’d like to have seen some interop (perhaps just a single example calling Win32?) but small complaint really https://t.co/wfyV1yn4OC
— Matt Breckon (@mbreckon) October 1, 2018