Please enable JavaScript.
Coggle requires JavaScript to display documents.
Making .NET Applications Faster (Make Your Code as Parallel as Necessary,…
Making .NET Applications Faster
Implementing
Value Types Correctly
reference types
value types
Creating a large number of objects
Creating a large collection of objects
Avoid boxing and reflection
Override
Equals
method (will be faster)
Implement
IEqualable
Override
GetHashCode
method
Applying Precompilation
Jit compilation
ngen install MyApp.exe
Avoid time to compile to native method before execution
For example a method can spend 125 ms in compile
MPGO (Managed profle-Guided optimization)
improves precompiled assembile's disk layout
Next project
ryuJit
Reducin I/O costs
ILMerge, Rugland Packer make a single .exe compressed and optimized to load
Precompiling Regexes
RegexOptions.Precompiled
Regex.CompileToAssembly
Using Unsafe
Code and Pointers
Pointers in C#
byte* p = &src
Copyng memory using pointers
You need to mark unsafe code
You can copy multiple bytes at same time
Better in small data
Reading structures
You can read with unsafe code with best performance that C# compiler generate or BynaryReader or PtrToStructure
Choosing a Collection
Array
Good choise if you don't need to do search
Good choise if you know the length
Trees
SortedDictionary
SortedSet
Associative collections
Dictionary
HashSet
Custom collections
Trie
Union Find
Disjoint set forest
Make Your Code as Parallel as
Necessary, but Not More
Kinds of parallelism
Parallelism: Running multiple threads in parallel
Concurrency: Doing multiple things at once
Asinchrony: Whitout blocking the caller's thread
Kinds of workloads
CPU-bound
I/O bound
Mixed
Parallel loops
Parallel.For
Parallel.ForEach
Don't forget lock shared resources
I7 core has 4 physical (Hyper Threading)
Async and await
async Task<Weather>...
Useful to I/O operations
await
Servers with multiple I/O operations
Getting rid of locks
Reduce objects to sync/lock
InterLocked.CompareExchange (Atomic hardware primitives)
Agregation (3 Delegates: Initial state, operator, join local results)
Parallel.For