SlideShare a Scribd company logo
www.xedotnet.org
Mirco Vanini
@MircoVanini
Span<T>
Optimising code using Span<T>,
Memory<T>, ReadOnlySpan<T>, …
Agenda
• Back to Basics
• The problem
• What is Span<T>?
• What is Memory<T>?
20/09/2019 2
Back to Basics
• .NET is a managed platform, that means the memory access
and management is safe and automatic. All types are fully
managed by .NET, it allocates memory either on the
execution stacks, or managed heaps.
20/09/2019 3
Back to Basics
• .NET is a managed platform, that means the memory access
and management is safe and automatic. All types are fully
managed by .NET, it allocates memory either on the
execution stacks, or managed heaps.
• In .NET world, there are three types of memory:
• Managed heap memory, such as an array;
• Stack memory, such as objects created by stackalloc;
• Native memory, such as a native pointer reference.
20/09/2019 3
Back to Basics
20/09/2019 4
Reference Types
• class keyword
• Allocated on heap
• A variable for a reference type is a
reference to the thing on the heap
not the object
• Passed around by reference
Assignment is a copy of the
reference,
Value Types
• struct keyword
• Allocated on stack or embedded
into a reference object
• A variable for a value type is the
value itself, e.g. integer
• Passed around by value (i.e.
copied)
• Assignment is a copy of the whole
value
Back to Basics
20/09/2019 5
Staff on the Stack
Back to Basics
20/09/2019 5
Staff on the Stack
Staff on the Managed Heap
Back to Basics
In C# by default Value Types are passed to
methods by value
20/09/2019 6
Back to Basics
In C# all parameters are passed to methods
by value by default
20/09/2019 6
DEMO
20/09/2019 7
ref returns and ref locals
The problem
• In many applications, the most CPU consuming operations
are string operations. If you run a profiler session against
your application, you may find the fact that 95% of the CPU
time is used to call string and related functions.
20/09/2019 8
The problem
• In many applications, the most CPU consuming operations
are string operations. If you run a profiler session against
your application, you may find the fact that 95% of the CPU
time is used to call string and related functions.
• Trim() or SubString() returns a new string object that is part of the original string,
this is unnecessary if there is a way to slice and return a portion of the original
string to save one copy.
20/09/2019 8
The problem
• In many applications, the most CPU consuming operations
are string operations. If you run a profiler session against
your application, you may find the fact that 95% of the CPU
time is used to call string and related functions.
• Trim() or SubString() returns a new string object that is part of the original string,
this is unnecessary if there is a way to slice and return a portion of the original
string to save one copy.
• IsNullOrWhiteSpace() takes a string object that needs a memory copy (because
string is immutable.)
20/09/2019 8
The problem
• In many applications, the most CPU consuming operations
are string operations. If you run a profiler session against
your application, you may find the fact that 95% of the CPU
time is used to call string and related functions.
• Trim() or SubString() returns a new string object that is part of the original string,
this is unnecessary if there is a way to slice and return a portion of the original
string to save one copy.
• IsNullOrWhiteSpace() takes a string object that needs a memory copy (because
string is immutable.)
• Specifically, string concatenation is expensive, it takes n string objects,
makes n copy, generate n - 1 temporary string objects, and return a final string
object, the n – 1 copies can be eliminated if there is a way to get direct access to
the return string memory and perform sequential writes.
20/09/2019 8
GC Managed Heap (Workstation mode)
• Mark
• Starting from the roots, mark all reachable objects as alive
• In Background
• Sweep
• Turning all non-reachable objects into a free space
• Blocking
• Compact
• To prevent fragmentation
• Not always
• Blocking
20/09/2019 9
GC Managed Heap (Workstation mode)
• Mark
• Starting from the roots, mark all reachable objects as alive
• In Background
• Sweep
• Turning all non-reachable objects into a free space
• Blocking
• Compact
• To prevent fragmentation
• Not always
• Blocking
20/09/2019 9
GC Managed Heap (Workstation mode)
• Mark
• Starting from the roots, mark all reachable objects as alive
• In Background
• Sweep
• Turning all non-reachable objects into a free space
• Blocking
• Compact
• To prevent fragmentation
• Not always
• Blocking
20/09/2019 9
GC Managed Heap (Workstation mode)
• Mark
• Starting from the roots, mark all reachable objects as alive
• In Background
• Sweep
• Turning all non-reachable objects into a free space
• Blocking
• Compact
• To prevent fragmentation
• Not always
• Blocking
20/09/2019 9
GC Managed Heap (Workstation mode)
• Mark
• Starting from the roots, mark all reachable objects as alive
• In Background
• Sweep
• Turning all non-reachable objects into a free space
• Blocking
• Compact
• To prevent fragmentation
• Not always
• Blocking
20/09/2019 9
Middle Ground between Server and Workstation GC by Maoni
Running with Server GC in a Small Container Scenario Part 0 by Maoni
Reference Semantics with Value Types (C# 7.2)
• inparameters
• Pass value type by reference. Called method cannot modify it
• ref locals and ref returns (C# 7.0)
• ref readonly returns
• Return a read only value type by reference
• readonly struct
• Immutable value types
• ref struct
• Stack only value types
20/09/2019 10
Write safe and efficient C# code
What is Span<T>?
20/09/2019 11
“System.Span<T> is a new value type at the
heart of .NET [that] enables the representation
of contiguous regions of arbitrary memory.”
“Span<T> is a small, but critical, building block
for a much larger effort to provide .NET APIs to
enable development of high scalability server
applications.”
Stephen Toub, MSDN Magazine, January 2018
dotnet/corefxlab
What is Span<T>?
20/09/2019 12
“System.Span<T> is a new value type at the
heart of .NET [that] enables the representation
of contiguous regions of arbitrary memory.”
What is Span<T>?
20/09/2019 12
“System.Span<T> is a new value type at the
heart of .NET [that] enables the representation
of contiguous regions of arbitrary memory.”
• Struct
• Part of System.Memory, available on Nuget
• .NET Standard 1.1 so can be used in .NET 4.5+
• Slow Span (.NET Standard)
• Fast Span (.NET Core)
• C# 7.2-ish .NET API Catalog
What is Span<T>?
20/09/2019 13
“System.Span<T> is a new value type at the
heart of .NET [that] enables the representation
of contiguous regions of arbitrary memory.”
What is Span<T>?
20/09/2019 13
“System.Span<T> is a new value type at the
heart of .NET [that] enables the representation
of contiguous regions of arbitrary memory.”
• High performance O(1), low (no) overhead
• Framework, CLR, JIT and GC support
• Provides memory and type safety
• Avoids the need for unsafe code
• Value Type
Span design document
What is Span<T>?
20/09/2019 14
“System.Span<T> is a new value type at the
heart of .NET [that] enables the representation
of contiguous regions of arbitrary memory.”
What is Span<T>?
20/09/2019 14
“System.Span<T> is a new value type at the
heart of .NET [that] enables the representation
of contiguous regions of arbitrary memory.”
• Native/unmanaged memory (P/Invoke)
• Managed memory (.NET types)
• Stack memory (stackalloc)
Span<byte> stackMemory = stackalloc byte[256]; // C# 7.2
IntPtr unmanagedHandle = Marshal.AllocHGlobal(256);
Span<byte> unmanaged = new Span<byte>(unmanagedHandle.ToPointer(), 256);
char[] array = new char[] {'i','m','p','l','i','c','i','t’ };
Span<char> fromArray = array; // implicit cast
ReadOnlySpan<char> fromString = "Spanification".AsSpan();
What is Span<T>?
20/09/2019 15
0x01
871
Stack
871
YGG871
Heap
0x01
0x02
0x03
0x04
0x05
0x06plate
num
string plate = “YGG871”;
plate = plate.Substring(3);
int num = int.Parse(plate);
What is Span<T>?
20/09/2019 15
0x01
871
Stack
871
YGG871
Heap
0x01
0x02
0x03
0x04
0x05
0x06
num
0x02
string plate = “YGG871”;
plate = plate.Substring(3);
int num = int.Parse(plate);
plate
span
What is Span<T>?
20/09/2019 16
0x01
0x01
0->3
6
871
Stack
871
YGG871
Heap
0x01
0x02
0x03
0x04
0x05
0x06plate
num
length
offset
pointer
string plate = “YGG871”;
ReadOnlySpan<char> s = plate.AsSpan();
s = s.Slice(3);
int num = int.Parse(s);
.NET Framework 4.5+ - slow span
span
What is Span<T>?
20/09/2019 16
0x01
0x01
0->3
6
871
Stack
871
YGG871
Heap
0x01
0x02
0x03
0x04
0x05
0x06plate
num
length
offset
pointer
string plate = “YGG871”;
ReadOnlySpan<char> s = plate.AsSpan();
s = s.Slice(3);
int num = int.Parse(s);
span
0x01+3
6->3length
pointer
.NET Framework 4.5+ - slow span.NET Core 2.0+ – fast span
DEMO
20/09/2019 17
Span<T>
* Legends *
ItemCount : Value of the 'ItemCount' parameter
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Ratio : Mean of the ratio distribution ([Current]/[Baseline])
RatioSD : Standard deviation of the ratio distribution ([Current]/[Baseline])
Rank : Relative position of current benchmark mean among all benchmarks (Arabic style)
Gen 0 : GC Generation 0 collects per 1000 operations
Gen 1 : GC Generation 1 collects per 1000 operations
Gen 2 : GC Generation 2 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ms : 1 Millisecond (0.001 sec)
Benchmark
20/09/2019 18
| Method | Mean | Error | StdDev | Ratio | Rank | Gen 0 | Gen 1 | Gen 2 | Allocated |
|-------------------------- |----------:|----------:|----------:|------:|-----:|-------:|------:|------:|----------:|
| GetLastNameWithSpan | 16.61 ns | 0.0222 ns | 0.0185 ns | 0.09 | 1 | - | - | - | - |
| GetLastNameUsingSubstring | 35.86 ns | 0.3395 ns | 0.3010 ns | 0.20 | 2 | 0.0127 | - | - | 40 B |
| GetLastName | 180.47 ns | 0.9540 ns | 0.8924 ns | 1.00 | 3 | 0.0458 | - | - | 144 B |
| Method | CharactersCount | Mean | Error | StdDev | Ratio | RatioSD | Rank | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------- |---------------- |-----------:|-----------:|-----------:|-------:|--------:|-----:|-------:|------:|------:|----------:|
| Slice | 10 | 5.090 ns | 0.0138 ns | 0.0108 ns | 1.00 | 0.00 | 1 | - | - | - | - |
| Substring | 10 | 12.858 ns | 0.1095 ns | 0.0971 ns | 2.53 | 0.02 | 2 | 0.0102 | - | - | 32 B |
| | | | | | | | | | | | |
| Slice | 10000 | 5.078 ns | 0.0084 ns | 0.0070 ns | 1.00 | 0.00 | 1 | - | - | - | - |
| Substring | 10000 | 699.687 ns | 13.8632 ns | 22.3865 ns | 137.44 | 3.65 | 2 | 3.1843 | - | - | 10024 B |
BenchmarkDotNet=v0.11.5, OS=macOS Mojave 10.14.6 (18G95) [Darwin 18.7.0]
Intel Core i7-4980HQ CPU 2.80GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.0.100-preview7-012821
[Host] : .NET Core 3.0.0-preview7-27912-14 (CoreCLR 4.700.19.32702, CoreFX 4.700.19.36209), 64bit RyuJIT
DefaultJob : .NET Core 3.0.0-preview7-27912-14 (CoreCLR 4.700.19.32702, CoreFX 4.700.19.36209), 64bit RyuJIT3
| Method | ItemCount | Mean | Error | StdDev | Ratio | RatioSD | Rank | Gen 0 | Gen 1 | Gen 2 | Allocated |
|----------------------- |---------- |---------:|----------:|----------:|------:|--------:|-----:|---------:|---------:|---------:|----------:|
| StringParseSum | 10 | NA | NA | NA | ? | ? | ? | - | - | - | - |
| StringParseSumWithSpan | 10 | NA | NA | NA | ? | ? | ? | - | - | - | - |
| | | | | | | | | | | | |
| StringParseSumWithSpan | 100000 | 2.799 ms | 0.0363 ms | 0.0322 ms | 0.32 | 0.01 | 1 | - | - | - | - |
| StringParseSum | 100000 | 8.742 ms | 0.1738 ms | 0.3666 ms | 1.00 | 0.00 | 2 | 671.8750 | 453.1250 | 218.7500 | 4139057 B |
When do mere mortals use it?
20/09/2019 19
var str = "EGOR 3.14 1234 7/3/2018";
string name = str.Substring(0, 4);
float pi = float.Parse(str.Substring(5, 4));
int number = int.Parse(str.Substring(10, 4));
DateTime date = DateTime.Parse(str.Substring(15, 8));
When do mere mortals use it?
20/09/2019 19
var str = "EGOR 3.14 1234 7/3/2018";
string name = str.Substring(0, 4);
float pi = float.Parse(str.Substring(5, 4));
int number = int.Parse(str.Substring(10, 4));
DateTime date = DateTime.Parse(str.Substring(15, 8));
Allocated on heap: 168 bytes
var str = "EGOR 3.14 1234 7/3/2018".AsSpan();
string name = str.Slice(0, 4);
float pi = float.Parse(str.Slice(5, 4));
int number = int.Parse(str.Slice(10, 4));
DateTime date = DateTime.Parse(str.Slice(15, 8));
Allocated on heap: 0 bytes
Span - The Limitations
20/09/2019 20
• Can only live on the stack
• Implemented as a ref struct
• Can only be contained by ref structs
ref structs
Structs that can exist only on the stack. New in C# 7.2
• Can’t implement interfaces
• Can’t be used as generic type arguments
• Can’t be boxed to object
• Can’t be passed in to - or used in places inside - of async
methods, iterators, nested functions or query expressions
Memory<T>
20/09/2019 21
• “Normal” struct
• Not as performant as Span
• Can be used in more places than Span (ie, doesn’t have the
limitations)
• Use the Span property to access the underlying memory
async Task DoSomethingAsync(Span<byte> buffer) // <-- error
{
buffer[0] = 0;
await Something(); // Bang!
buffer[0] = 1;
}
async Task DoSomethingAsync(Memory<byte> buffer)
{
buffer.Span[0] = 0;
await Something(); // Totally fine
buffer.Span[0] = 1;
}
DEMO
20/09/2019 22
Memory<T>
System.Buffers Namespace
20/09/2019 23
Recap
20/09/2019 24
• Span<T> makes it easy and safe to use any
kind of memory
• System.Memory package, C# 7.2
• Memory<T> has no stack-only limitations
• Don’t copy memory! Slice it!
• Prefer read-only versions over mutable ones
• Prefer safe managed memory over native
memory
Recap
Performance is a Feature!
20/09/2019 24
20/09/2019 25
Who I am
www.proxsoft.it
info@proxsoft.it
@MircoVanini
Mirco Vanini
Microsoft® MVP Windows Development
AllSeen Alliance - AllJoyn® Ambassador
Open Connectivity Foundation - OCF® Ambassador
Is C# a low-level language?
What language features of C#/F#/VB.NET or
BCL / Runtime functionality enable ‘low-level’
programming ?
20/09/2019 26
Is C# a low-level language?
What language features of C#/F#/VB.NET or
BCL / Runtime functionality enable ‘low-level’
programming ?
20/09/2019 26
Any C# developer is going to have a different
idea of what ‘low-level’ means, these features
would be taken for granted by C++ or Rust
programmers.
Is C# a low-level language?
• ref returns and ref locals: pass and return by reference to avoid
large struct copying. It can be even faster than unsafe ! link
• Unsafe code in .NET: is the part of the program that runs outside
the control of the Common Language Runtime of the .NET
frameworks. link
• Managed pointers in .NET: It could be defined as a more general
type of reference, which may point to other locations than just the
beginning of an object. link
• Span<T> and universal memory management: this presentation…
link
• Interoperability: interoperability with unmanaged code through
platform invoke services, C/C++ interoperability, and COM
interoperability (COM interop). link
20/09/2019 27

More Related Content

What's hot

08 - Return Oriented Programming, the chosen one
08 - Return Oriented Programming, the chosen one08 - Return Oriented Programming, the chosen one
08 - Return Oriented Programming, the chosen one
Alexandre Moneger
 
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on LinuxCNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on Linux
Sam Bowne
 
Micro-Benchmarking Considered Harmful
Micro-Benchmarking Considered HarmfulMicro-Benchmarking Considered Harmful
Micro-Benchmarking Considered Harmful
Thomas Wuerthinger
 
Everything You Need to Know About the Intel® MPI Library
Everything You Need to Know About the Intel® MPI LibraryEverything You Need to Know About the Intel® MPI Library
Everything You Need to Know About the Intel® MPI Library
Intel® Software
 
CNIT 127: 4: Format string bugs
CNIT 127: 4: Format string bugsCNIT 127: 4: Format string bugs
CNIT 127: 4: Format string bugs
Sam Bowne
 
Exploitation Crash Course
Exploitation Crash CourseExploitation Crash Course
Exploitation Crash Course
UTD Computer Security Group
 
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT TalksMykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
Vadym Muliavka
 
Course lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented ProgrammingCourse lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented Programming
Jonathan Salwan
 
Arduino C maXbox web of things slide show
Arduino C maXbox web of things slide showArduino C maXbox web of things slide show
Arduino C maXbox web of things slide show
Max Kleiner
 
Tensorflow on Android
Tensorflow on AndroidTensorflow on Android
Tensorflow on Android
Koan-Sin Tan
 
Survey of Program Transformation Technologies
Survey of Program Transformation TechnologiesSurvey of Program Transformation Technologies
Survey of Program Transformation Technologies
Chunhua Liao
 
Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]
LivePerson
 
A Source-To-Source Approach to HPC Challenges
A Source-To-Source Approach to HPC ChallengesA Source-To-Source Approach to HPC Challenges
A Source-To-Source Approach to HPC Challenges
Chunhua Liao
 
Ahieving Performance C#
Ahieving Performance C#Ahieving Performance C#
Ahieving Performance C#
Roman Atachiants
 
JDK8 Functional API
JDK8 Functional APIJDK8 Functional API
JDK8 Functional API
Justin Lin
 
TensorFlow 2.0 Autographs - For TFUG - Vik Pant
TensorFlow 2.0 Autographs - For TFUG - Vik PantTensorFlow 2.0 Autographs - For TFUG - Vik Pant
TensorFlow 2.0 Autographs - For TFUG - Vik Pant
Devatanu Banerjee
 
C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending Python
Priyank Kapadia
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleiner
Max Kleiner
 
GitRecruit final 1
GitRecruit final 1GitRecruit final 1
GitRecruit final 1
Yinghan Fu
 
Dark Silicon, Mobile Devices, and Possible Open-Source Solutions
Dark Silicon, Mobile Devices, and Possible Open-Source SolutionsDark Silicon, Mobile Devices, and Possible Open-Source Solutions
Dark Silicon, Mobile Devices, and Possible Open-Source SolutionsKoan-Sin Tan
 

What's hot (20)

08 - Return Oriented Programming, the chosen one
08 - Return Oriented Programming, the chosen one08 - Return Oriented Programming, the chosen one
08 - Return Oriented Programming, the chosen one
 
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on LinuxCNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on Linux
 
Micro-Benchmarking Considered Harmful
Micro-Benchmarking Considered HarmfulMicro-Benchmarking Considered Harmful
Micro-Benchmarking Considered Harmful
 
Everything You Need to Know About the Intel® MPI Library
Everything You Need to Know About the Intel® MPI LibraryEverything You Need to Know About the Intel® MPI Library
Everything You Need to Know About the Intel® MPI Library
 
CNIT 127: 4: Format string bugs
CNIT 127: 4: Format string bugsCNIT 127: 4: Format string bugs
CNIT 127: 4: Format string bugs
 
Exploitation Crash Course
Exploitation Crash CourseExploitation Crash Course
Exploitation Crash Course
 
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT TalksMykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
 
Course lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented ProgrammingCourse lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented Programming
 
Arduino C maXbox web of things slide show
Arduino C maXbox web of things slide showArduino C maXbox web of things slide show
Arduino C maXbox web of things slide show
 
Tensorflow on Android
Tensorflow on AndroidTensorflow on Android
Tensorflow on Android
 
Survey of Program Transformation Technologies
Survey of Program Transformation TechnologiesSurvey of Program Transformation Technologies
Survey of Program Transformation Technologies
 
Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]
 
A Source-To-Source Approach to HPC Challenges
A Source-To-Source Approach to HPC ChallengesA Source-To-Source Approach to HPC Challenges
A Source-To-Source Approach to HPC Challenges
 
Ahieving Performance C#
Ahieving Performance C#Ahieving Performance C#
Ahieving Performance C#
 
JDK8 Functional API
JDK8 Functional APIJDK8 Functional API
JDK8 Functional API
 
TensorFlow 2.0 Autographs - For TFUG - Vik Pant
TensorFlow 2.0 Autographs - For TFUG - Vik PantTensorFlow 2.0 Autographs - For TFUG - Vik Pant
TensorFlow 2.0 Autographs - For TFUG - Vik Pant
 
C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending Python
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleiner
 
GitRecruit final 1
GitRecruit final 1GitRecruit final 1
GitRecruit final 1
 
Dark Silicon, Mobile Devices, and Possible Open-Source Solutions
Dark Silicon, Mobile Devices, and Possible Open-Source SolutionsDark Silicon, Mobile Devices, and Possible Open-Source Solutions
Dark Silicon, Mobile Devices, and Possible Open-Source Solutions
 

Similar to Optimising code using Span<T>

Five cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark fasterFive cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark faster
Tim Ellison
 
Sista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceSista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performance
ESUG
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Maarten Balliauw
 
Architecture at Scale
Architecture at ScaleArchitecture at Scale
Architecture at Scale
Elasticsearch
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCD
rsebbe
 
DotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NETDotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NET
Maarten Balliauw
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
Garth Gilmour
 
CNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code AuditingCNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code Auditing
Sam Bowne
 
chapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guidechapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guide
SanjeevSaharan5
 
Игорь Фесенко "Direction of C# as a High-Performance Language"
Игорь Фесенко "Direction of C# as a High-Performance Language"Игорь Фесенко "Direction of C# as a High-Performance Language"
Игорь Фесенко "Direction of C# as a High-Performance Language"
Fwdays
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
ppd1961
 
High Performance With Java
High Performance With JavaHigh Performance With Java
High Performance With Java
malduarte
 
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
Intel® Software
 
cb streams - gavin pickin
cb streams - gavin pickincb streams - gavin pickin
cb streams - gavin pickin
Ortus Solutions, Corp
 
Workshop slides
Workshop slidesWorkshop slides
Workshop slides
Rishabh Jain
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects 100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects
Andrey Karpov
 
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
Alexandre Moneger
 
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management....NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
NETFest
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
Simon Ritter
 

Similar to Optimising code using Span<T> (20)

Five cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark fasterFive cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark faster
 
Sista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceSista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performance
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
 
Architecture at Scale
Architecture at ScaleArchitecture at Scale
Architecture at Scale
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCD
 
DotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NETDotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NET
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 
CNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code AuditingCNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code Auditing
 
chapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guidechapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guide
 
Игорь Фесенко "Direction of C# as a High-Performance Language"
Игорь Фесенко "Direction of C# as a High-Performance Language"Игорь Фесенко "Direction of C# as a High-Performance Language"
Игорь Фесенко "Direction of C# as a High-Performance Language"
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
 
High Performance With Java
High Performance With JavaHigh Performance With Java
High Performance With Java
 
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
Fast Insights to Optimized Vectorization and Memory Using Cache-aware Rooflin...
 
cb streams - gavin pickin
cb streams - gavin pickincb streams - gavin pickin
cb streams - gavin pickin
 
Workshop slides
Workshop slidesWorkshop slides
Workshop slides
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects 100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects
 
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
 
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management....NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
 

More from Mirco Vanini

.NET 7 Performance Improvements_10_03_2023.pdf
.NET 7 Performance Improvements_10_03_2023.pdf.NET 7 Performance Improvements_10_03_2023.pdf
.NET 7 Performance Improvements_10_03_2023.pdf
Mirco Vanini
 
Debugging a .NET program after crash (Post-mortem debugging)
Debugging a .NET program after crash (Post-mortem debugging)Debugging a .NET program after crash (Post-mortem debugging)
Debugging a .NET program after crash (Post-mortem debugging)
Mirco Vanini
 
Connect a chips to Azure
Connect a chips to AzureConnect a chips to Azure
Connect a chips to Azure
Mirco Vanini
 
Connect a chips to Azure
Connect a chips to AzureConnect a chips to Azure
Connect a chips to Azure
Mirco Vanini
 
How to modernise WPF and Windows Forms applications with Windows Apps SDK
How to modernise WPF and Windows Forms applications with Windows Apps SDKHow to modernise WPF and Windows Forms applications with Windows Apps SDK
How to modernise WPF and Windows Forms applications with Windows Apps SDK
Mirco Vanini
 
C# on a CHIPs
C# on a CHIPsC# on a CHIPs
C# on a CHIPs
Mirco Vanini
 
.NET Conf 2021 - Hot Topics Desktop Development
.NET Conf 2021 - Hot Topics Desktop Development.NET Conf 2021 - Hot Topics Desktop Development
.NET Conf 2021 - Hot Topics Desktop Development
Mirco Vanini
 
Async Debugging A Practical Guide to survive !
Async Debugging A Practical Guide to survive !Async Debugging A Practical Guide to survive !
Async Debugging A Practical Guide to survive !
Mirco Vanini
 
IoT support for .NET (Core/5/6)
IoT support for .NET (Core/5/6)IoT support for .NET (Core/5/6)
IoT support for .NET (Core/5/6)
Mirco Vanini
 
Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !
Mirco Vanini
 
IoT support for .NET Core
IoT support for .NET CoreIoT support for .NET Core
IoT support for .NET Core
Mirco Vanini
 
IoT support for .NET Core - IoT Saturday 2020
IoT support for .NET Core - IoT Saturday 2020IoT support for .NET Core - IoT Saturday 2020
IoT support for .NET Core - IoT Saturday 2020
Mirco Vanini
 
.NET Conf 2020 - Hot Topics Desktop Development
.NET Conf 2020 - Hot Topics Desktop Development.NET Conf 2020 - Hot Topics Desktop Development
.NET Conf 2020 - Hot Topics Desktop Development
Mirco Vanini
 
Are you ready for Microsoft Azure Sphere?
Are you ready for Microsoft Azure Sphere?Are you ready for Microsoft Azure Sphere?
Are you ready for Microsoft Azure Sphere?
Mirco Vanini
 
IoT Day 2019 Naples - Microsoft Azure Shpere
IoT Day 2019 Naples - Microsoft Azure ShpereIoT Day 2019 Naples - Microsoft Azure Shpere
IoT Day 2019 Naples - Microsoft Azure Shpere
Mirco Vanini
 
Debugging with VS2019
Debugging with VS2019Debugging with VS2019
Debugging with VS2019
Mirco Vanini
 
Azure Sphere
Azure SphereAzure Sphere
Azure Sphere
Mirco Vanini
 
Azure Sphere
Azure SphereAzure Sphere
Azure Sphere
Mirco Vanini
 
Xe OneDay - Modernizing Enterprise Apps
Xe OneDay - Modernizing Enterprise AppsXe OneDay - Modernizing Enterprise Apps
Xe OneDay - Modernizing Enterprise Apps
Mirco Vanini
 
Azure Sphere - GAB 2019
Azure Sphere - GAB 2019Azure Sphere - GAB 2019
Azure Sphere - GAB 2019
Mirco Vanini
 

More from Mirco Vanini (20)

.NET 7 Performance Improvements_10_03_2023.pdf
.NET 7 Performance Improvements_10_03_2023.pdf.NET 7 Performance Improvements_10_03_2023.pdf
.NET 7 Performance Improvements_10_03_2023.pdf
 
Debugging a .NET program after crash (Post-mortem debugging)
Debugging a .NET program after crash (Post-mortem debugging)Debugging a .NET program after crash (Post-mortem debugging)
Debugging a .NET program after crash (Post-mortem debugging)
 
Connect a chips to Azure
Connect a chips to AzureConnect a chips to Azure
Connect a chips to Azure
 
Connect a chips to Azure
Connect a chips to AzureConnect a chips to Azure
Connect a chips to Azure
 
How to modernise WPF and Windows Forms applications with Windows Apps SDK
How to modernise WPF and Windows Forms applications with Windows Apps SDKHow to modernise WPF and Windows Forms applications with Windows Apps SDK
How to modernise WPF and Windows Forms applications with Windows Apps SDK
 
C# on a CHIPs
C# on a CHIPsC# on a CHIPs
C# on a CHIPs
 
.NET Conf 2021 - Hot Topics Desktop Development
.NET Conf 2021 - Hot Topics Desktop Development.NET Conf 2021 - Hot Topics Desktop Development
.NET Conf 2021 - Hot Topics Desktop Development
 
Async Debugging A Practical Guide to survive !
Async Debugging A Practical Guide to survive !Async Debugging A Practical Guide to survive !
Async Debugging A Practical Guide to survive !
 
IoT support for .NET (Core/5/6)
IoT support for .NET (Core/5/6)IoT support for .NET (Core/5/6)
IoT support for .NET (Core/5/6)
 
Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !
 
IoT support for .NET Core
IoT support for .NET CoreIoT support for .NET Core
IoT support for .NET Core
 
IoT support for .NET Core - IoT Saturday 2020
IoT support for .NET Core - IoT Saturday 2020IoT support for .NET Core - IoT Saturday 2020
IoT support for .NET Core - IoT Saturday 2020
 
.NET Conf 2020 - Hot Topics Desktop Development
.NET Conf 2020 - Hot Topics Desktop Development.NET Conf 2020 - Hot Topics Desktop Development
.NET Conf 2020 - Hot Topics Desktop Development
 
Are you ready for Microsoft Azure Sphere?
Are you ready for Microsoft Azure Sphere?Are you ready for Microsoft Azure Sphere?
Are you ready for Microsoft Azure Sphere?
 
IoT Day 2019 Naples - Microsoft Azure Shpere
IoT Day 2019 Naples - Microsoft Azure ShpereIoT Day 2019 Naples - Microsoft Azure Shpere
IoT Day 2019 Naples - Microsoft Azure Shpere
 
Debugging with VS2019
Debugging with VS2019Debugging with VS2019
Debugging with VS2019
 
Azure Sphere
Azure SphereAzure Sphere
Azure Sphere
 
Azure Sphere
Azure SphereAzure Sphere
Azure Sphere
 
Xe OneDay - Modernizing Enterprise Apps
Xe OneDay - Modernizing Enterprise AppsXe OneDay - Modernizing Enterprise Apps
Xe OneDay - Modernizing Enterprise Apps
 
Azure Sphere - GAB 2019
Azure Sphere - GAB 2019Azure Sphere - GAB 2019
Azure Sphere - GAB 2019
 

Recently uploaded

Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 

Recently uploaded (20)

Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 

Optimising code using Span<T>

  • 1. www.xedotnet.org Mirco Vanini @MircoVanini Span<T> Optimising code using Span<T>, Memory<T>, ReadOnlySpan<T>, …
  • 2. Agenda • Back to Basics • The problem • What is Span<T>? • What is Memory<T>? 20/09/2019 2
  • 3. Back to Basics • .NET is a managed platform, that means the memory access and management is safe and automatic. All types are fully managed by .NET, it allocates memory either on the execution stacks, or managed heaps. 20/09/2019 3
  • 4. Back to Basics • .NET is a managed platform, that means the memory access and management is safe and automatic. All types are fully managed by .NET, it allocates memory either on the execution stacks, or managed heaps. • In .NET world, there are three types of memory: • Managed heap memory, such as an array; • Stack memory, such as objects created by stackalloc; • Native memory, such as a native pointer reference. 20/09/2019 3
  • 5. Back to Basics 20/09/2019 4 Reference Types • class keyword • Allocated on heap • A variable for a reference type is a reference to the thing on the heap not the object • Passed around by reference Assignment is a copy of the reference, Value Types • struct keyword • Allocated on stack or embedded into a reference object • A variable for a value type is the value itself, e.g. integer • Passed around by value (i.e. copied) • Assignment is a copy of the whole value
  • 6. Back to Basics 20/09/2019 5 Staff on the Stack
  • 7. Back to Basics 20/09/2019 5 Staff on the Stack Staff on the Managed Heap
  • 8. Back to Basics In C# by default Value Types are passed to methods by value 20/09/2019 6
  • 9. Back to Basics In C# all parameters are passed to methods by value by default 20/09/2019 6
  • 11. The problem • In many applications, the most CPU consuming operations are string operations. If you run a profiler session against your application, you may find the fact that 95% of the CPU time is used to call string and related functions. 20/09/2019 8
  • 12. The problem • In many applications, the most CPU consuming operations are string operations. If you run a profiler session against your application, you may find the fact that 95% of the CPU time is used to call string and related functions. • Trim() or SubString() returns a new string object that is part of the original string, this is unnecessary if there is a way to slice and return a portion of the original string to save one copy. 20/09/2019 8
  • 13. The problem • In many applications, the most CPU consuming operations are string operations. If you run a profiler session against your application, you may find the fact that 95% of the CPU time is used to call string and related functions. • Trim() or SubString() returns a new string object that is part of the original string, this is unnecessary if there is a way to slice and return a portion of the original string to save one copy. • IsNullOrWhiteSpace() takes a string object that needs a memory copy (because string is immutable.) 20/09/2019 8
  • 14. The problem • In many applications, the most CPU consuming operations are string operations. If you run a profiler session against your application, you may find the fact that 95% of the CPU time is used to call string and related functions. • Trim() or SubString() returns a new string object that is part of the original string, this is unnecessary if there is a way to slice and return a portion of the original string to save one copy. • IsNullOrWhiteSpace() takes a string object that needs a memory copy (because string is immutable.) • Specifically, string concatenation is expensive, it takes n string objects, makes n copy, generate n - 1 temporary string objects, and return a final string object, the n – 1 copies can be eliminated if there is a way to get direct access to the return string memory and perform sequential writes. 20/09/2019 8
  • 15. GC Managed Heap (Workstation mode) • Mark • Starting from the roots, mark all reachable objects as alive • In Background • Sweep • Turning all non-reachable objects into a free space • Blocking • Compact • To prevent fragmentation • Not always • Blocking 20/09/2019 9
  • 16. GC Managed Heap (Workstation mode) • Mark • Starting from the roots, mark all reachable objects as alive • In Background • Sweep • Turning all non-reachable objects into a free space • Blocking • Compact • To prevent fragmentation • Not always • Blocking 20/09/2019 9
  • 17. GC Managed Heap (Workstation mode) • Mark • Starting from the roots, mark all reachable objects as alive • In Background • Sweep • Turning all non-reachable objects into a free space • Blocking • Compact • To prevent fragmentation • Not always • Blocking 20/09/2019 9
  • 18. GC Managed Heap (Workstation mode) • Mark • Starting from the roots, mark all reachable objects as alive • In Background • Sweep • Turning all non-reachable objects into a free space • Blocking • Compact • To prevent fragmentation • Not always • Blocking 20/09/2019 9
  • 19. GC Managed Heap (Workstation mode) • Mark • Starting from the roots, mark all reachable objects as alive • In Background • Sweep • Turning all non-reachable objects into a free space • Blocking • Compact • To prevent fragmentation • Not always • Blocking 20/09/2019 9 Middle Ground between Server and Workstation GC by Maoni Running with Server GC in a Small Container Scenario Part 0 by Maoni
  • 20. Reference Semantics with Value Types (C# 7.2) • inparameters • Pass value type by reference. Called method cannot modify it • ref locals and ref returns (C# 7.0) • ref readonly returns • Return a read only value type by reference • readonly struct • Immutable value types • ref struct • Stack only value types 20/09/2019 10 Write safe and efficient C# code
  • 21. What is Span<T>? 20/09/2019 11 “System.Span<T> is a new value type at the heart of .NET [that] enables the representation of contiguous regions of arbitrary memory.” “Span<T> is a small, but critical, building block for a much larger effort to provide .NET APIs to enable development of high scalability server applications.” Stephen Toub, MSDN Magazine, January 2018 dotnet/corefxlab
  • 22. What is Span<T>? 20/09/2019 12 “System.Span<T> is a new value type at the heart of .NET [that] enables the representation of contiguous regions of arbitrary memory.”
  • 23. What is Span<T>? 20/09/2019 12 “System.Span<T> is a new value type at the heart of .NET [that] enables the representation of contiguous regions of arbitrary memory.” • Struct • Part of System.Memory, available on Nuget • .NET Standard 1.1 so can be used in .NET 4.5+ • Slow Span (.NET Standard) • Fast Span (.NET Core) • C# 7.2-ish .NET API Catalog
  • 24. What is Span<T>? 20/09/2019 13 “System.Span<T> is a new value type at the heart of .NET [that] enables the representation of contiguous regions of arbitrary memory.”
  • 25. What is Span<T>? 20/09/2019 13 “System.Span<T> is a new value type at the heart of .NET [that] enables the representation of contiguous regions of arbitrary memory.” • High performance O(1), low (no) overhead • Framework, CLR, JIT and GC support • Provides memory and type safety • Avoids the need for unsafe code • Value Type Span design document
  • 26. What is Span<T>? 20/09/2019 14 “System.Span<T> is a new value type at the heart of .NET [that] enables the representation of contiguous regions of arbitrary memory.”
  • 27. What is Span<T>? 20/09/2019 14 “System.Span<T> is a new value type at the heart of .NET [that] enables the representation of contiguous regions of arbitrary memory.” • Native/unmanaged memory (P/Invoke) • Managed memory (.NET types) • Stack memory (stackalloc) Span<byte> stackMemory = stackalloc byte[256]; // C# 7.2 IntPtr unmanagedHandle = Marshal.AllocHGlobal(256); Span<byte> unmanaged = new Span<byte>(unmanagedHandle.ToPointer(), 256); char[] array = new char[] {'i','m','p','l','i','c','i','t’ }; Span<char> fromArray = array; // implicit cast ReadOnlySpan<char> fromString = "Spanification".AsSpan();
  • 28. What is Span<T>? 20/09/2019 15 0x01 871 Stack 871 YGG871 Heap 0x01 0x02 0x03 0x04 0x05 0x06plate num string plate = “YGG871”; plate = plate.Substring(3); int num = int.Parse(plate);
  • 29. What is Span<T>? 20/09/2019 15 0x01 871 Stack 871 YGG871 Heap 0x01 0x02 0x03 0x04 0x05 0x06 num 0x02 string plate = “YGG871”; plate = plate.Substring(3); int num = int.Parse(plate); plate
  • 30. span What is Span<T>? 20/09/2019 16 0x01 0x01 0->3 6 871 Stack 871 YGG871 Heap 0x01 0x02 0x03 0x04 0x05 0x06plate num length offset pointer string plate = “YGG871”; ReadOnlySpan<char> s = plate.AsSpan(); s = s.Slice(3); int num = int.Parse(s); .NET Framework 4.5+ - slow span
  • 31. span What is Span<T>? 20/09/2019 16 0x01 0x01 0->3 6 871 Stack 871 YGG871 Heap 0x01 0x02 0x03 0x04 0x05 0x06plate num length offset pointer string plate = “YGG871”; ReadOnlySpan<char> s = plate.AsSpan(); s = s.Slice(3); int num = int.Parse(s); span 0x01+3 6->3length pointer .NET Framework 4.5+ - slow span.NET Core 2.0+ – fast span
  • 32. DEMO 20/09/2019 17 Span<T> * Legends * ItemCount : Value of the 'ItemCount' parameter Mean : Arithmetic mean of all measurements Error : Half of 99.9% confidence interval StdDev : Standard deviation of all measurements Ratio : Mean of the ratio distribution ([Current]/[Baseline]) RatioSD : Standard deviation of the ratio distribution ([Current]/[Baseline]) Rank : Relative position of current benchmark mean among all benchmarks (Arabic style) Gen 0 : GC Generation 0 collects per 1000 operations Gen 1 : GC Generation 1 collects per 1000 operations Gen 2 : GC Generation 2 collects per 1000 operations Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B) 1 ms : 1 Millisecond (0.001 sec)
  • 33. Benchmark 20/09/2019 18 | Method | Mean | Error | StdDev | Ratio | Rank | Gen 0 | Gen 1 | Gen 2 | Allocated | |-------------------------- |----------:|----------:|----------:|------:|-----:|-------:|------:|------:|----------:| | GetLastNameWithSpan | 16.61 ns | 0.0222 ns | 0.0185 ns | 0.09 | 1 | - | - | - | - | | GetLastNameUsingSubstring | 35.86 ns | 0.3395 ns | 0.3010 ns | 0.20 | 2 | 0.0127 | - | - | 40 B | | GetLastName | 180.47 ns | 0.9540 ns | 0.8924 ns | 1.00 | 3 | 0.0458 | - | - | 144 B | | Method | CharactersCount | Mean | Error | StdDev | Ratio | RatioSD | Rank | Gen 0 | Gen 1 | Gen 2 | Allocated | |---------- |---------------- |-----------:|-----------:|-----------:|-------:|--------:|-----:|-------:|------:|------:|----------:| | Slice | 10 | 5.090 ns | 0.0138 ns | 0.0108 ns | 1.00 | 0.00 | 1 | - | - | - | - | | Substring | 10 | 12.858 ns | 0.1095 ns | 0.0971 ns | 2.53 | 0.02 | 2 | 0.0102 | - | - | 32 B | | | | | | | | | | | | | | | Slice | 10000 | 5.078 ns | 0.0084 ns | 0.0070 ns | 1.00 | 0.00 | 1 | - | - | - | - | | Substring | 10000 | 699.687 ns | 13.8632 ns | 22.3865 ns | 137.44 | 3.65 | 2 | 3.1843 | - | - | 10024 B | BenchmarkDotNet=v0.11.5, OS=macOS Mojave 10.14.6 (18G95) [Darwin 18.7.0] Intel Core i7-4980HQ CPU 2.80GHz (Haswell), 1 CPU, 8 logical and 4 physical cores .NET Core SDK=3.0.100-preview7-012821 [Host] : .NET Core 3.0.0-preview7-27912-14 (CoreCLR 4.700.19.32702, CoreFX 4.700.19.36209), 64bit RyuJIT DefaultJob : .NET Core 3.0.0-preview7-27912-14 (CoreCLR 4.700.19.32702, CoreFX 4.700.19.36209), 64bit RyuJIT3 | Method | ItemCount | Mean | Error | StdDev | Ratio | RatioSD | Rank | Gen 0 | Gen 1 | Gen 2 | Allocated | |----------------------- |---------- |---------:|----------:|----------:|------:|--------:|-----:|---------:|---------:|---------:|----------:| | StringParseSum | 10 | NA | NA | NA | ? | ? | ? | - | - | - | - | | StringParseSumWithSpan | 10 | NA | NA | NA | ? | ? | ? | - | - | - | - | | | | | | | | | | | | | | | StringParseSumWithSpan | 100000 | 2.799 ms | 0.0363 ms | 0.0322 ms | 0.32 | 0.01 | 1 | - | - | - | - | | StringParseSum | 100000 | 8.742 ms | 0.1738 ms | 0.3666 ms | 1.00 | 0.00 | 2 | 671.8750 | 453.1250 | 218.7500 | 4139057 B |
  • 34. When do mere mortals use it? 20/09/2019 19 var str = "EGOR 3.14 1234 7/3/2018"; string name = str.Substring(0, 4); float pi = float.Parse(str.Substring(5, 4)); int number = int.Parse(str.Substring(10, 4)); DateTime date = DateTime.Parse(str.Substring(15, 8));
  • 35. When do mere mortals use it? 20/09/2019 19 var str = "EGOR 3.14 1234 7/3/2018"; string name = str.Substring(0, 4); float pi = float.Parse(str.Substring(5, 4)); int number = int.Parse(str.Substring(10, 4)); DateTime date = DateTime.Parse(str.Substring(15, 8)); Allocated on heap: 168 bytes var str = "EGOR 3.14 1234 7/3/2018".AsSpan(); string name = str.Slice(0, 4); float pi = float.Parse(str.Slice(5, 4)); int number = int.Parse(str.Slice(10, 4)); DateTime date = DateTime.Parse(str.Slice(15, 8)); Allocated on heap: 0 bytes
  • 36. Span - The Limitations 20/09/2019 20 • Can only live on the stack • Implemented as a ref struct • Can only be contained by ref structs ref structs Structs that can exist only on the stack. New in C# 7.2 • Can’t implement interfaces • Can’t be used as generic type arguments • Can’t be boxed to object • Can’t be passed in to - or used in places inside - of async methods, iterators, nested functions or query expressions
  • 37. Memory<T> 20/09/2019 21 • “Normal” struct • Not as performant as Span • Can be used in more places than Span (ie, doesn’t have the limitations) • Use the Span property to access the underlying memory async Task DoSomethingAsync(Span<byte> buffer) // <-- error { buffer[0] = 0; await Something(); // Bang! buffer[0] = 1; } async Task DoSomethingAsync(Memory<byte> buffer) { buffer.Span[0] = 0; await Something(); // Totally fine buffer.Span[0] = 1; }
  • 40. Recap 20/09/2019 24 • Span<T> makes it easy and safe to use any kind of memory • System.Memory package, C# 7.2 • Memory<T> has no stack-only limitations • Don’t copy memory! Slice it! • Prefer read-only versions over mutable ones • Prefer safe managed memory over native memory
  • 41. Recap Performance is a Feature! 20/09/2019 24
  • 42. 20/09/2019 25 Who I am www.proxsoft.it info@proxsoft.it @MircoVanini Mirco Vanini Microsoft® MVP Windows Development AllSeen Alliance - AllJoyn® Ambassador Open Connectivity Foundation - OCF® Ambassador
  • 43. Is C# a low-level language? What language features of C#/F#/VB.NET or BCL / Runtime functionality enable ‘low-level’ programming ? 20/09/2019 26
  • 44. Is C# a low-level language? What language features of C#/F#/VB.NET or BCL / Runtime functionality enable ‘low-level’ programming ? 20/09/2019 26 Any C# developer is going to have a different idea of what ‘low-level’ means, these features would be taken for granted by C++ or Rust programmers.
  • 45. Is C# a low-level language? • ref returns and ref locals: pass and return by reference to avoid large struct copying. It can be even faster than unsafe ! link • Unsafe code in .NET: is the part of the program that runs outside the control of the Common Language Runtime of the .NET frameworks. link • Managed pointers in .NET: It could be defined as a more general type of reference, which may point to other locations than just the beginning of an object. link • Span<T> and universal memory management: this presentation… link • Interoperability: interoperability with unmanaged code through platform invoke services, C/C++ interoperability, and COM interoperability (COM interop). link 20/09/2019 27