SlideShare a Scribd company logo
Improving performance
using .NET Core 3.0
USING THE GREAT WORK OF OTHERS TO MAKE US LOOK AWESOME
Awesome .NET Performance
https://github.com/adamsitnik/awesome-dot-net-performance
Framework
Improvements
WHAT HAS HAPPENED IN .NET CORE THAT HELPS PERFORMANCE?
Reduced memory allocations
Less time spent in GC collections and less overall GC pressure
Less time allocating and deallocating objects means more CPU for you
Across the framework, lots of small improvements over many classes.
Span<T>
C# 7.2
https://apisof.net/ & https://source.dot.net
Span<T>
https://adamsitnik.com/Span/
Stack access only (use Memory<T> for the heap)
Can’t use it as a field in a class (since a class is on the heap) but can use it in a struct.
Can’t do async/await with it (since the compiler creates a state machine… on the heap)
Substring Comparison
someText.Substring(startIndex: someText.Length / 2);
someText.AsSpan().Slice(start: someText.Length / 2);
Memory<T>
Has a .Span property that you can use to get a Span in a method
Create it from a string, array, or something implementing IOwnedMemory.
Lots of methods in .NET Core 2.1+ take Spans as arguments.
Many more do so in .NET Core 3.0 (.Net Standard 2.1)
https://apisof.net/
Base64.EncodeToUtf8(ReadOnlySpan<Byte>,Span<Byte>,Int32,Int32,Boolean)
System.Buffers.ArrayPool
Object pooling pattern - https://www.codeproject.com/articles/20848/c-object-pooling
In .NET Core (System.Buffers) - https://adamsitnik.com/Array-Pool/
var samePool = ArrayPool<byte>.Shared;
byte[] buffer = samePool.Rent(minLength);
try {
Use(buffer);
} finally {
samePool.Return(buffer);
}
Cheaper as soon as you need 1K of memory (or more) – and no allocations required.
System.Buffers.ArrayPool
String interning
https://taagung.com/string-interning/
https://docs.microsoft.com/en-us/dotnet/api/system.string.intern?view=netframework-4.7.2
Compiler puts all hardcoded strings in an assembly into an “intern pool” and references point to
them to avoid duplications.
String.Intern() is for using the same concept at runtime.
Warning: Strings in the intern pool are NEVER GC’ed. Great for unplanned memory leaks! Used
with caution can reap large benefits in certain scenarios.
ref locals and ref returns
ref int Max(ref int first, ref int second, ref int third) {
ref int max = ref first;
if (first < second) max = second;
if (second < third) max = third;
return ref max;
}
The method result is simply a reference to whichever value was the largest.
It has zero allocations.
Reduce casting and boxing
Warning: Casting to generic interfaces is sloooow!
https://www.danielcrabtree.com/blog/191/casting-to-ienumerable-t-is-two-orders-of-
magnitude-slower
Boxing operations create invisible allocations. Some boxing operations are hard to spot.
LINQ & Closures
class Symbol { public string Name { get; private set; } /*...*/
}
class Compiler {
private List<Symbol> symbols;
public Symbol FindMatchingSymbol(string name) {
return symbols.FirstOrDefault(s => s.Name == name);
}
}
private class Lambda1Environment {
public string capturedName;
public bool Evaluate(Symbol s) {
return s.Name == this.capturedName;
}
}
Lambda1Environment l = new Lambda1Environment
capturedName = name };
var predicate = new Func<Symbol, bool>(l.Evaluate);
Func<Symbol, bool> predicate = s => s.Name == name;
return symbols.FirstOrDefault(predicate);
Boxing operation.
FirstOrDefault() is an extension
method on IEnumerable<T>
Compiles to…
Alternative implementation?
Not as pretty, but no allocations.
foreach will use the List<T> iterator. No casting and no hidden lambda code.
public Symbol FindMatchingSymbol(string name)
{
foreach (Symbol s in symbols)
{
if (s.Name == name) return s;
}
return null;
}
MemoryMarshal (helps with Spans)
public Span<byte> FloatsToSpanOfBytes() => MemoryMarshal.Cast<float, byte>(arrayOfFloats);
----
[StructLayout(LayoutKind.Explicit)]
public struct Bid {
[FieldOffset(0)] public float Value;
[FieldOffset(4)] public long ProductId;
[FieldOffset(12)] public long UserId;
[FieldOffset(20)] public DateTime Time;
}
…
public Bid Deserialize(ReadOnlySpan<byte> serialized) => MemoryMarshal.Read<Bid>(serialized);
stackalloc Keyword
Allows you to directly allocate memory on the stack
Don’t overdo it and keep it for short-lived usage
Beware: It’s easy to misuse this and make things worse
Span<byte> bytes = length <= 128 ?
stackalloc byte[length] :
new byte[length];
Platform Instrinsics
System.Runtime.Intrinsics – let you use hardware accelerated SIMD specific to ARM, x64, etc.
https://bits.houmus.org/2018-08-18/netcoreapp3.0-instrinsics-in-real-life-pt1
For general use the platform independent Vector SIMD instructions are preferred.
(check System.Numerics.Vector.IsHardwareAccelerated)
Theory Time is Over
LET’S IMPROVE THE PERFORMANCE OF “SOMETHING”
Tip #1:
Understand the “Why?”
BLOCKING & I/O CAN HURT MORE THAN HEAVY CPU USE
Tip #2:
Stay Focused
DON’T OPTIMISE THE UNIMPORTANT STUFF. THINK “HOT PATH”
Tip #3:
Provable Improvements
MEASURE, CHANGE, MEASURE AGAIN.
Let’s work with some real code!
Our target library: PdfPig
Features:
* Targets .NET Standard 2.0
* Port of Apache PDFBox to C#
* Has lots of tests
(And it’s not something I’d seen before prepping this session)
Tooling
PerfView
◦ https://github.com/microsoft/perfview
BenchmarkDotNet
◦ https://benchmarkdotnet.org/
ILSpy:
◦ https://github.com/icsharpcode/ILSpy
VisualStudio 2019 Diagnostic tools (Optional)
Speedscope
◦ https://www.speedscope.app/
---
For X-Plat: dotnet-counters, dotnet-trace, dotnet-dump
◦ https://github.com/dotnet/diagnostics/tree/master/documentation
What we’ll do
Measure current performance (using .NET Core 2.2)
Upgrade to .NET Core 3.0 prev. 7 & compare performance
Analyse performance using PerfView
Run microbenchmarks to measure specific performance areas
What you’ll do
Clone https://github.com/rbanks54/PdfPig
◦ use the benchmarks branch
Identify an area you want to improve
Go ahead. Try and improve it. And prove it. 
Suggested developer loop:
1. Ensure all unit tests pass & baseline current performance
2. Make a change
3. Check unit tests still pass
4. Measure new performance and compare with baseline
5. Repeat from step 2 until happy

More Related Content

What's hot

Real-time streams and logs with Storm and Kafka
Real-time streams and logs with Storm and KafkaReal-time streams and logs with Storm and Kafka
Real-time streams and logs with Storm and Kafka
Andrew Montalenti
 
Deconstruct 2017: All programmers MUST learn C and Assembly
Deconstruct 2017: All programmers MUST learn C and AssemblyDeconstruct 2017: All programmers MUST learn C and Assembly
Deconstruct 2017: All programmers MUST learn C and Assembly
ice799
 
Cassandra sf 2015 - Steady State Data Size With Compaction, Tombstones, and TTL
Cassandra sf 2015 - Steady State Data Size With Compaction, Tombstones, and TTL Cassandra sf 2015 - Steady State Data Size With Compaction, Tombstones, and TTL
Cassandra sf 2015 - Steady State Data Size With Compaction, Tombstones, and TTL
aaronmorton
 
Accelerating microbiome research with OpenACC
Accelerating microbiome research with OpenACCAccelerating microbiome research with OpenACC
Accelerating microbiome research with OpenACC
Igor Sfiligoi
 
Linux kernel memory allocators
Linux kernel memory allocatorsLinux kernel memory allocators
Linux kernel memory allocators
Hao-Ran Liu
 
DTrace - Miracle Scotland Database Forum
DTrace - Miracle Scotland Database ForumDTrace - Miracle Scotland Database Forum
DTrace - Miracle Scotland Database ForumDoug Burns
 
Ping to Pong
Ping to PongPing to Pong
Ping to Pong
Matt Provost
 
Introduction to SLURM
Introduction to SLURMIntroduction to SLURM
[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
SAP HANA Cloud Platform
 
Network namespaces
Network namespacesNetwork namespaces
Network namespaces
Marian Marinov
 
Code gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introductionCode gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introduction
Marina Kolpakova
 
libpcap
libpcaplibpcap
libpcap
mohan43u
 
(PFC302) Performance Benchmarking on AWS | AWS re:Invent 2014
(PFC302) Performance Benchmarking on AWS | AWS re:Invent 2014(PFC302) Performance Benchmarking on AWS | AWS re:Invent 2014
(PFC302) Performance Benchmarking on AWS | AWS re:Invent 2014
Amazon Web Services
 
Slurm @ 2018 LabTech
Slurm @  2018 LabTechSlurm @  2018 LabTech
Slurm @ 2018 LabTech
Tin Ho
 
Lec7
Lec7Lec7
Ntp cheat sheet
Ntp cheat sheetNtp cheat sheet
Ntp cheat sheet
csystemltd
 
Docker tips-for-java-developers
Docker tips-for-java-developersDocker tips-for-java-developers
Docker tips-for-java-developers
Aparna Chaudhary
 
Linux Kernel Init Process
Linux Kernel Init ProcessLinux Kernel Init Process
Linux Kernel Init Process
Kernel TLV
 
Distributed Multi-device Execution of TensorFlow – an Outlook
Distributed Multi-device Execution of TensorFlow – an OutlookDistributed Multi-device Execution of TensorFlow – an Outlook
Distributed Multi-device Execution of TensorFlow – an OutlookSebnem Rusitschka
 

What's hot (20)

Real-time streams and logs with Storm and Kafka
Real-time streams and logs with Storm and KafkaReal-time streams and logs with Storm and Kafka
Real-time streams and logs with Storm and Kafka
 
Deconstruct 2017: All programmers MUST learn C and Assembly
Deconstruct 2017: All programmers MUST learn C and AssemblyDeconstruct 2017: All programmers MUST learn C and Assembly
Deconstruct 2017: All programmers MUST learn C and Assembly
 
Cassandra sf 2015 - Steady State Data Size With Compaction, Tombstones, and TTL
Cassandra sf 2015 - Steady State Data Size With Compaction, Tombstones, and TTL Cassandra sf 2015 - Steady State Data Size With Compaction, Tombstones, and TTL
Cassandra sf 2015 - Steady State Data Size With Compaction, Tombstones, and TTL
 
Accelerating microbiome research with OpenACC
Accelerating microbiome research with OpenACCAccelerating microbiome research with OpenACC
Accelerating microbiome research with OpenACC
 
Linux kernel memory allocators
Linux kernel memory allocatorsLinux kernel memory allocators
Linux kernel memory allocators
 
DTrace - Miracle Scotland Database Forum
DTrace - Miracle Scotland Database ForumDTrace - Miracle Scotland Database Forum
DTrace - Miracle Scotland Database Forum
 
Ping to Pong
Ping to PongPing to Pong
Ping to Pong
 
Introduction to SLURM
Introduction to SLURMIntroduction to SLURM
Introduction to SLURM
 
[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
 
Network namespaces
Network namespacesNetwork namespaces
Network namespaces
 
Code gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introductionCode gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introduction
 
libpcap
libpcaplibpcap
libpcap
 
(PFC302) Performance Benchmarking on AWS | AWS re:Invent 2014
(PFC302) Performance Benchmarking on AWS | AWS re:Invent 2014(PFC302) Performance Benchmarking on AWS | AWS re:Invent 2014
(PFC302) Performance Benchmarking on AWS | AWS re:Invent 2014
 
Slurm @ 2018 LabTech
Slurm @  2018 LabTechSlurm @  2018 LabTech
Slurm @ 2018 LabTech
 
Network simulator 2
Network simulator 2Network simulator 2
Network simulator 2
 
Lec7
Lec7Lec7
Lec7
 
Ntp cheat sheet
Ntp cheat sheetNtp cheat sheet
Ntp cheat sheet
 
Docker tips-for-java-developers
Docker tips-for-java-developersDocker tips-for-java-developers
Docker tips-for-java-developers
 
Linux Kernel Init Process
Linux Kernel Init ProcessLinux Kernel Init Process
Linux Kernel Init Process
 
Distributed Multi-device Execution of TensorFlow – an Outlook
Distributed Multi-device Execution of TensorFlow – an OutlookDistributed Multi-device Execution of TensorFlow – an Outlook
Distributed Multi-device Execution of TensorFlow – an Outlook
 

Similar to Improving app performance using .Net Core 3.0

Scope Stack Allocation
Scope Stack AllocationScope Stack Allocation
Scope Stack Allocation
Electronic Arts / DICE
 
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
Raahul Raghavan
 
May2010 hex-core-opt
May2010 hex-core-optMay2010 hex-core-opt
May2010 hex-core-optJeff Larkin
 
jvm goes to big data
jvm goes to big datajvm goes to big data
jvm goes to big data
srisatish ambati
 
Coding for multiple cores
Coding for multiple coresCoding for multiple cores
Coding for multiple cores
Lee Hanxue
 
Mobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneMobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneBhuvan Khanna
 
Unity best practices (2013)
Unity best practices (2013)Unity best practices (2013)
Unity best practices (2013)
Benjamin Robert
 
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
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Maarten Balliauw
 
Container Performance Analysis Brendan Gregg, Netflix
Container Performance Analysis Brendan Gregg, NetflixContainer Performance Analysis Brendan Gregg, Netflix
Container Performance Analysis Brendan Gregg, Netflix
Docker, Inc.
 
Container Performance Analysis
Container Performance AnalysisContainer Performance Analysis
Container Performance Analysis
Brendan Gregg
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesPragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Marina Kolpakova
 
Best Practices for performance evaluation and diagnosis of Java Applications ...
Best Practices for performance evaluation and diagnosis of Java Applications ...Best Practices for performance evaluation and diagnosis of Java Applications ...
Best Practices for performance evaluation and diagnosis of Java Applications ...
IndicThreads
 
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
Amazon Web Services
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeDmitri Nesteruk
 
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
Amazon Web Services
 
Lab3 advanced port scanning 30 oct 21
Lab3 advanced port scanning 30 oct 21Lab3 advanced port scanning 30 oct 21
Lab3 advanced port scanning 30 oct 21
Hussain111321
 
Super scaling singleton inserts
Super scaling singleton insertsSuper scaling singleton inserts
Super scaling singleton inserts
Chris Adkin
 
Gpu and The Brick Wall
Gpu and The Brick WallGpu and The Brick Wall
Gpu and The Brick Wall
ugur candan
 
Introduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimizationIntroduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimization
CSUC - Consorci de Serveis Universitaris de Catalunya
 

Similar to Improving app performance using .Net Core 3.0 (20)

Scope Stack Allocation
Scope Stack AllocationScope Stack Allocation
Scope Stack Allocation
 
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
 
May2010 hex-core-opt
May2010 hex-core-optMay2010 hex-core-opt
May2010 hex-core-opt
 
jvm goes to big data
jvm goes to big datajvm goes to big data
jvm goes to big data
 
Coding for multiple cores
Coding for multiple coresCoding for multiple cores
Coding for multiple cores
 
Mobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneMobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, Pune
 
Unity best practices (2013)
Unity best practices (2013)Unity best practices (2013)
Unity best practices (2013)
 
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
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
 
Container Performance Analysis Brendan Gregg, Netflix
Container Performance Analysis Brendan Gregg, NetflixContainer Performance Analysis Brendan Gregg, Netflix
Container Performance Analysis Brendan Gregg, Netflix
 
Container Performance Analysis
Container Performance AnalysisContainer Performance Analysis
Container Performance Analysis
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesPragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
 
Best Practices for performance evaluation and diagnosis of Java Applications ...
Best Practices for performance evaluation and diagnosis of Java Applications ...Best Practices for performance evaluation and diagnosis of Java Applications ...
Best Practices for performance evaluation and diagnosis of Java Applications ...
 
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
 
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
 
Lab3 advanced port scanning 30 oct 21
Lab3 advanced port scanning 30 oct 21Lab3 advanced port scanning 30 oct 21
Lab3 advanced port scanning 30 oct 21
 
Super scaling singleton inserts
Super scaling singleton insertsSuper scaling singleton inserts
Super scaling singleton inserts
 
Gpu and The Brick Wall
Gpu and The Brick WallGpu and The Brick Wall
Gpu and The Brick Wall
 
Introduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimizationIntroduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimization
 

More from Richard Banks

Reignite your desire to improve (NDC Sydney 2018)
Reignite your desire to improve (NDC Sydney 2018)Reignite your desire to improve (NDC Sydney 2018)
Reignite your desire to improve (NDC Sydney 2018)
Richard Banks
 
Flaccid coaching
Flaccid coachingFlaccid coaching
Flaccid coaching
Richard Banks
 
Microservices with .Net - NDC Sydney, 2016
Microservices with .Net - NDC Sydney, 2016Microservices with .Net - NDC Sydney, 2016
Microservices with .Net - NDC Sydney, 2016
Richard Banks
 
CQRS and what it means for your architecture
CQRS and what it means for your architectureCQRS and what it means for your architecture
CQRS and what it means for your architecture
Richard Banks
 
Architecting Microservices in .Net
Architecting Microservices in .NetArchitecting Microservices in .Net
Architecting Microservices in .Net
Richard Banks
 
Git TFS
Git TFSGit TFS
Git TFS
Richard Banks
 
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a CometDDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a CometRichard Banks
 
Architecture In The Small
Architecture In The SmallArchitecture In The Small
Architecture In The Small
Richard Banks
 
Agile Development From A Developers Perspective
Agile Development From A Developers PerspectiveAgile Development From A Developers Perspective
Agile Development From A Developers Perspective
Richard Banks
 

More from Richard Banks (9)

Reignite your desire to improve (NDC Sydney 2018)
Reignite your desire to improve (NDC Sydney 2018)Reignite your desire to improve (NDC Sydney 2018)
Reignite your desire to improve (NDC Sydney 2018)
 
Flaccid coaching
Flaccid coachingFlaccid coaching
Flaccid coaching
 
Microservices with .Net - NDC Sydney, 2016
Microservices with .Net - NDC Sydney, 2016Microservices with .Net - NDC Sydney, 2016
Microservices with .Net - NDC Sydney, 2016
 
CQRS and what it means for your architecture
CQRS and what it means for your architectureCQRS and what it means for your architecture
CQRS and what it means for your architecture
 
Architecting Microservices in .Net
Architecting Microservices in .NetArchitecting Microservices in .Net
Architecting Microservices in .Net
 
Git TFS
Git TFSGit TFS
Git TFS
 
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a CometDDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
 
Architecture In The Small
Architecture In The SmallArchitecture In The Small
Architecture In The Small
 
Agile Development From A Developers Perspective
Agile Development From A Developers PerspectiveAgile Development From A Developers Perspective
Agile Development From A Developers Perspective
 

Recently uploaded

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
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
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
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
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
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
 
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
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
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
 

Recently uploaded (20)

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
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
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.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
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
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
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
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
 

Improving app performance using .Net Core 3.0

  • 1. Improving performance using .NET Core 3.0 USING THE GREAT WORK OF OTHERS TO MAKE US LOOK AWESOME
  • 3. Framework Improvements WHAT HAS HAPPENED IN .NET CORE THAT HELPS PERFORMANCE?
  • 4. Reduced memory allocations Less time spent in GC collections and less overall GC pressure Less time allocating and deallocating objects means more CPU for you Across the framework, lots of small improvements over many classes.
  • 5. Span<T> C# 7.2 https://apisof.net/ & https://source.dot.net
  • 6. Span<T> https://adamsitnik.com/Span/ Stack access only (use Memory<T> for the heap) Can’t use it as a field in a class (since a class is on the heap) but can use it in a struct. Can’t do async/await with it (since the compiler creates a state machine… on the heap)
  • 7. Substring Comparison someText.Substring(startIndex: someText.Length / 2); someText.AsSpan().Slice(start: someText.Length / 2);
  • 8. Memory<T> Has a .Span property that you can use to get a Span in a method Create it from a string, array, or something implementing IOwnedMemory. Lots of methods in .NET Core 2.1+ take Spans as arguments. Many more do so in .NET Core 3.0 (.Net Standard 2.1) https://apisof.net/ Base64.EncodeToUtf8(ReadOnlySpan<Byte>,Span<Byte>,Int32,Int32,Boolean)
  • 9. System.Buffers.ArrayPool Object pooling pattern - https://www.codeproject.com/articles/20848/c-object-pooling In .NET Core (System.Buffers) - https://adamsitnik.com/Array-Pool/ var samePool = ArrayPool<byte>.Shared; byte[] buffer = samePool.Rent(minLength); try { Use(buffer); } finally { samePool.Return(buffer); } Cheaper as soon as you need 1K of memory (or more) – and no allocations required.
  • 11. String interning https://taagung.com/string-interning/ https://docs.microsoft.com/en-us/dotnet/api/system.string.intern?view=netframework-4.7.2 Compiler puts all hardcoded strings in an assembly into an “intern pool” and references point to them to avoid duplications. String.Intern() is for using the same concept at runtime. Warning: Strings in the intern pool are NEVER GC’ed. Great for unplanned memory leaks! Used with caution can reap large benefits in certain scenarios.
  • 12. ref locals and ref returns ref int Max(ref int first, ref int second, ref int third) { ref int max = ref first; if (first < second) max = second; if (second < third) max = third; return ref max; } The method result is simply a reference to whichever value was the largest. It has zero allocations.
  • 13. Reduce casting and boxing Warning: Casting to generic interfaces is sloooow! https://www.danielcrabtree.com/blog/191/casting-to-ienumerable-t-is-two-orders-of- magnitude-slower Boxing operations create invisible allocations. Some boxing operations are hard to spot.
  • 14. LINQ & Closures class Symbol { public string Name { get; private set; } /*...*/ } class Compiler { private List<Symbol> symbols; public Symbol FindMatchingSymbol(string name) { return symbols.FirstOrDefault(s => s.Name == name); } } private class Lambda1Environment { public string capturedName; public bool Evaluate(Symbol s) { return s.Name == this.capturedName; } } Lambda1Environment l = new Lambda1Environment capturedName = name }; var predicate = new Func<Symbol, bool>(l.Evaluate); Func<Symbol, bool> predicate = s => s.Name == name; return symbols.FirstOrDefault(predicate); Boxing operation. FirstOrDefault() is an extension method on IEnumerable<T> Compiles to…
  • 15. Alternative implementation? Not as pretty, but no allocations. foreach will use the List<T> iterator. No casting and no hidden lambda code. public Symbol FindMatchingSymbol(string name) { foreach (Symbol s in symbols) { if (s.Name == name) return s; } return null; }
  • 16. MemoryMarshal (helps with Spans) public Span<byte> FloatsToSpanOfBytes() => MemoryMarshal.Cast<float, byte>(arrayOfFloats); ---- [StructLayout(LayoutKind.Explicit)] public struct Bid { [FieldOffset(0)] public float Value; [FieldOffset(4)] public long ProductId; [FieldOffset(12)] public long UserId; [FieldOffset(20)] public DateTime Time; } … public Bid Deserialize(ReadOnlySpan<byte> serialized) => MemoryMarshal.Read<Bid>(serialized);
  • 17. stackalloc Keyword Allows you to directly allocate memory on the stack Don’t overdo it and keep it for short-lived usage Beware: It’s easy to misuse this and make things worse Span<byte> bytes = length <= 128 ? stackalloc byte[length] : new byte[length];
  • 18. Platform Instrinsics System.Runtime.Intrinsics – let you use hardware accelerated SIMD specific to ARM, x64, etc. https://bits.houmus.org/2018-08-18/netcoreapp3.0-instrinsics-in-real-life-pt1 For general use the platform independent Vector SIMD instructions are preferred. (check System.Numerics.Vector.IsHardwareAccelerated)
  • 19. Theory Time is Over LET’S IMPROVE THE PERFORMANCE OF “SOMETHING”
  • 20. Tip #1: Understand the “Why?” BLOCKING & I/O CAN HURT MORE THAN HEAVY CPU USE
  • 21. Tip #2: Stay Focused DON’T OPTIMISE THE UNIMPORTANT STUFF. THINK “HOT PATH”
  • 22. Tip #3: Provable Improvements MEASURE, CHANGE, MEASURE AGAIN.
  • 23. Let’s work with some real code! Our target library: PdfPig Features: * Targets .NET Standard 2.0 * Port of Apache PDFBox to C# * Has lots of tests (And it’s not something I’d seen before prepping this session)
  • 24. Tooling PerfView ◦ https://github.com/microsoft/perfview BenchmarkDotNet ◦ https://benchmarkdotnet.org/ ILSpy: ◦ https://github.com/icsharpcode/ILSpy VisualStudio 2019 Diagnostic tools (Optional) Speedscope ◦ https://www.speedscope.app/ --- For X-Plat: dotnet-counters, dotnet-trace, dotnet-dump ◦ https://github.com/dotnet/diagnostics/tree/master/documentation
  • 25. What we’ll do Measure current performance (using .NET Core 2.2) Upgrade to .NET Core 3.0 prev. 7 & compare performance Analyse performance using PerfView Run microbenchmarks to measure specific performance areas
  • 26. What you’ll do Clone https://github.com/rbanks54/PdfPig ◦ use the benchmarks branch Identify an area you want to improve Go ahead. Try and improve it. And prove it.  Suggested developer loop: 1. Ensure all unit tests pass & baseline current performance 2. Make a change 3. Check unit tests still pass 4. Measure new performance and compare with baseline 5. Repeat from step 2 until happy