Focus on performance optimization in .NET Code and not architecture.
Being called out for premature optimizations.
“Scale for an application can mean the number of users that will concurrently connect to the application at any given time, the amount of input to process or the number of times data needs to be processed.
For us, as engineers, it means we have to know what to ignore and knowing what to pay close attention to.” David Fowler
Think at least twice before using LINQ or unnecessary enumeration on the hot path
We can only know the before and after when we measure it.
~20-40%
~20-40%
Array.Empty<T>
to represent empty arrays
Enumerable.Empty<T>
to represent empty
enumerablesEnumerable.TryGetNonEnumeratedCount
We can only know the before and after when we measure it.
~5-64%
~23-61%
+56%
~23-61%
Be aware of closure allocations
We can only know the before and after when we measure it.
~71%
~Gone!
*__DisplayClass*
or various variants of Action*
and
Func*
Pool and re-use buffers (and larger objects)
We can only know the before and after when we measure it.
+226%
~Gone!
For smaller local buffers, consider using the stack
We can only know the before and after when we measure it.
~45%
~Gone!
Parameter overloads and boxing
Task.WhenAny
Avoid parameter overloads and boxing.
Task.WhenAny
Avoid parameter overloads and boxing.
CancellationTokenSource
Avoid parameter overloads and boxing.
CancellationTokenSource
Avoid parameter overloads and boxing.
Watch out for immutable/readonly data that is copied
Immutable/readonly data should not be copied.
Immutable/readonly data should not be copied.
Span
or Memory
Span
or
Memory
based variants
We can only know the before and after when we measure it.
~38-47%
~Gone!
Span
or Memory
Span
or
Memory
based variants
Array.Empty<T>
to represent empty arrays
Enumerable.Empty<T>
to represent empty
enumerablesEnumerable.TryGetNonEnumeratedCount
Span
or Memory
Span
or
Memory
based variants
The original ComputeHash
method had a .
Did you
spot it?
static void ComputeHash(byte[] data, uint seed1, uint seed2, out uint hash1, out uint hash2) { uint a, b, c; a = b = c = (uint)(0xdeadbeef + data.Length + seed1); c += seed2; int index = 0, size = data.Length; while (size > 12) { a += BitConverter.ToUInt32(data, index); b += BitConverter.ToUInt32(data, index + 4); c += BitConverter.ToUInt32(data, index + 8); // rest omitted }
Tweak
expensive I/O operations first.
Pay close attention to the context of the
code.
Apply the principles where they matter.
Everywhere else, favor readability.
github.com/danielmarbach/PerformanceTricksAzureSDK
Happy coding!