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 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 Slide

  3. View Slide

  4. Rust

    View Slide

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

    View Slide

  6. C# as a high performance language

    View Slide

  7. C# as a high performance language

    View Slide

  8. Performance where it matters
    Fram
    eworks
    Libraries

    View Slide

  9. Always
    Measure

    View Slide

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

    View Slide

  11. How it
    works

    View Slide

  12. View Slide

  13. View Slide


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

    View Slide

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

    View Slide

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

    View Slide

  17. Code - behaviour

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  22. 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 Slide

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

    View Slide

  24. Watch out for hidden allocations

    View Slide

  25. Watch out for hidden allocations

    View Slide

  26. Watch out for hidden allocations

    View Slide

  27. Watch out for hidden allocations

    View Slide

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

    View Slide

  29. View Slide

  30. In Parameters

    View Slide

  31. 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 Slide

  32. Ref extension methods

    View Slide

  33. Ref locals
    ▸ Reference semantics with value types

    View Slide

  34. Ref locals
    ▸ Can be thought of as aliases

    View Slide

  35. Ref returns

    View Slide

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

    View Slide

  37. Ref Struct

    View Slide


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

    View Slide

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

    View Slide

  40. Span

    View Slide

  41. Span

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  46. Problem with pointers

    View Slide

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

    View Slide

  48. View Slide

  49. You can still pin

    View Slide

  50. You can still pin - in C# 7.3

    View Slide

  51. BenchmarkDotNet

    View Slide

  52. View Slide

  53. View Slide

  54. Results

    View Slide

  55. C# 8 - Slicing with Ranges
    demo

    View Slide

  56. View Slide

  57. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  62. 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 Slide

  63. Summary

    View Slide

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

    View Slide