Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Memory Management, C# 7.2 and Span<T>

slang25
March 25, 2018

Memory Management, C# 7.2 and Span<T>

A tour of .NET memory management, new C# features and Span

slang25

March 25, 2018
Tweet

More Decks by slang25

Other Decks in Technology

Transcript

  1. Memory
    Management,
    C# 7.2 and
    Span

    View full-size slide

  2. HELLO!
    F# |> I ❤
    9 years of experience in .NET
    Organiser of Bristol F# meetup and DDD South West
    You can find me at @stuartblang & http://stuartlang.uk

    View full-size slide

  3. ● FAST
    ● SAFE
    ● MAINTAINABLE
    Can C# have all 3?

    View full-size slide

  4. C# as a high performance language

    View full-size slide

  5. C# as a high performance language

    View full-size slide

  6. Performance where it matters
    Fram
    eworks
    Libraries

    View full-size slide

  7. Always
    Measure

    View full-size slide

  8. Measure with...
    ▸ Memory Performance Counters
    ▸ BenchmarkDotNet
    ▸ dotMemory
    ▸ Visual Studio

    View full-size slide


  9. The Stack Is An Implementation Detail.
    - Eric Lippert
    Part One
    Part Two

    View full-size slide

  10. Types
    ▸ Value types
    ▹ Structs
    ▹ Enums
    ▹ Bool
    ▹ Int, Float, Short, Long…
    ▹ Byte
    ▸ Reference types
    ▹ Class
    ▹ Interface
    ▹ Array
    ▹ String
    ▹ Delegate

    View full-size slide

  11. Code
    Stack
    a=42
    Stack
    x(ref)
    Heap
    object:
    array{1,2,3}
    sharplab.io demo

    View full-size slide

  12. Code - behaviour

    View full-size slide

  13. Stack
    Credit: http://www.i-programmer.info/ebooks/deep-c/363

    View full-size slide

  14. Heap
    Small Object Heap Large Object Heap
    Gen 0 Gen 1 Gen 2

    View full-size slide

  15. Heap
    Credit:
    https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/

    View full-size slide

  16. Great Resource
    ▸ http://benhall.io/a-super-simplified-exp
    lanation-of-net-garbage-collection/

    View full-size slide

  17. 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)

    View full-size slide

  18. Watch out for hidden allocations
    ▸ Let’s see some examples

    View full-size slide

  19. Watch out for hidden allocations

    View full-size slide

  20. Watch out for hidden allocations

    View full-size slide

  21. Watch out for hidden allocations

    View full-size slide

  22. Watch out for hidden allocations

    View full-size slide

  23. Plugins
    ▸ R# Heap Allocations Viewer
    ▸ Roslyn Clr Heap Allocation Analyzer

    View full-size slide

  24. In Parameters

    View full-size slide

  25. 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

    View full-size slide

  26. Ref extension methods

    View full-size slide

  27. Ref locals
    ▸ Reference semantics with value types

    View full-size slide

  28. Ref locals
    ▸ Can be thought of as aliases

    View full-size slide

  29. Ref Struct
    ▸ Aka ref-like
    ▸ Stack-only!
    ▸ To support Span

    View full-size slide


  30. The great Eric Lippert once wrote
    “The Stack Is An Implementation
    Detail”, and basically that’s not
    true anymore.
    - Jon Skeet

    View full-size slide

  31. Span
    ▸ Abstraction over arbitrary memory
    Span
    Marshal.AllocHGlobal() stackalloc [] new []

    View full-size slide

  32. Span - Why stack-only?
    ▸ Safe lifetime - cannot outlive stack memory
    ▸ Safe concurrency - No struct tearing
    ▸ It’s fast!

    View full-size slide

  33. Span - Other properties
    ▸ Efficient representation
    ▸ GC tracking - refs will be maintained by GC

    View full-size slide

  34. Use Cases
    ▸ Alternative to unsafe code
    ▸ Parsing text
    ▸ Sharing partial parts of memory

    View full-size slide

  35. Problem with pointers
    ▸ Requires GC pinning (for heap memory)
    ▸ Mustn’t escape pointer (for stack)
    ▸ No bounds checking
    ▸ Unsafe code blocks

    View full-size slide

  36. Problem with pointers

    View full-size slide

  37. Credit: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/stackalloc

    View full-size slide

  38. You can still pin

    View full-size slide

  39. You can still pin - in C# 7.3

    View full-size slide

  40. BenchmarkDotNet

    View full-size slide

  41. C# 8 - Slicing with Ranges
    demo

    View full-size slide

  42. Span Framework APIs
    netstandard2.0 ❌ netstandard2.1 ✅
    Today Next
    netcoreapp2.1 ✅
    netcoreapp2.0 ❌
    netcoreapp2.2 ✅
    netcoreapp2.3 ✅
    netcoreapp3.0 ✅
    net472 ❌ net48 ❌ net49 ❓

    View full-size slide

  43. Related topics
    ▸ ReadOnlySpan
    ▸ Memory & ReadOnlyMemory
    ▸ System.Runtime.CompilerServices.Unsafe
    ▸ MemoryMarshal.AsBytes(span)
    ▸ MemoryMarshal.Cast(span)

    View full-size slide

  44. Libraries using Span (just some)
    ▸ Utf8Json
    ▸ SpanJson
    ▸ ZeroFormatter
    ▸ StackExchange.Redis

    View full-size slide

  45. Case study
    JustEat.StatsD PRs
    Low hanging fruit:
    #55, #59, #63, #65
    Master class by dv00d00:
    #104 - Zero Allocations

    View full-size slide

  46. Good Reads
    ▸ Adam Sitnik - Span
    ▸ Marc Gravell - Spans and ref Parts 1 & 2
    ▸ Span spec
    ▸ Vladimir Sadov
    ▸ All About Span: Exploring a New .NET Mainstay -
    Stephen Toub
    ▸ Maarten Balliauw

    View full-size slide

  47. THANKS!
    Any questions?
    You can find me at @stuartblang

    View full-size slide