Serving the Quantitative Finance Community

 
User avatar
AnalyticalVega
Topic Author
Posts: 104
Joined: January 16th, 2013, 5:03 am

.NET Performance Tuning Tips

August 14th, 2015, 10:06 pm

.NET Performance Tuning Tips1) Pre-complier NGEN install myapp.exe2) Use Value types (no heap memory)3) Rewrite (overload) methods so that they don't use boxing or reflection 4) Use Generic Collections to avoid boxing/unboxing costs5) Use fastest JIT compiler, RyuJit > JIT64 , use RyuJit for faster compilation time6) MPGO (Managed Profile Guided Optimization) Improves precompiled assemblies' disk layout- Optimize code and data positions on disk7) Improve Cold Startup I/O time,- ILMerge , merge dlls with .exe into a new .exe file 8) Improve Cold Startup I/O time - Executable packers - compress assemblies so that they take less to load - RugLand packer (RPX , codeplex)9) Improve Cold Startup I/O time - Place Strong named assemblies in the GAC so that the CLR can skip the assembly verification checks.10) Improve Cold Startup I/O time- Windows SuperFetch Pre-Fetch code and data that it thinks your app will need.11) Precompile serialization assemblies - SGen.exe12) Precompile Regexes13) Use Unsafe code - Pointers and Memory pinning to avoid garbage collection14) Hand rolled memory copy methods using pointers sometimes it's faster for certain array sizes.15) Use Dynamic code to construct dynamic unsafe generic methods and classes using pointers and IL code.16) Use array storage for speed and low memory usage17) Use LIST<T> , as a dynamic resizable array , but only use it with append to add data to the end of the array, other insertions will cause a full copy of the array when it runs out of current space and needs to resize.18) Use SortedDictionary<K,V> or SortedSET<T> for fast searching, needs a lot of memory.19) USE Hashing with Associative Collections. Dictionary<K,V> and HASHSET<T> provide O(1) insert, delete, and lookups, but will need time to reorganize if you make a lot of changes to them. CANNOT SORT BY KEY AND HAS BIG MEMORY OVERHEAD.20) Data Parallelism 21) Asynchronous I/O and Asynchronous Parallelism- Asynchronous file read, Asynchronous HTTP Post, Wait for all tasks to complete, Wait for first task to complete22) Get rid of high level synchronization locks by replacing with low level atomic synchronization primitives (using hardware-based inter-lock compare exchange exposed by C#) - used by Concurrent Stack and Concurrent Bag23) Aggregation- Instead of locks, use smaller unlocked sub-tasks and the re-combine all the outputs when the last sub-task is finished. Basically collect intermediate results into thread-local structures. Only need synchronization when combining thread-local outputs into one global output.24) Use Atomic hardware primitives: Interlocked.Increment, Interlocked.Decrement, Interlocked.Add etc..25) Parallel Loops for CPU Bound Workloads, Tasks, Async, and Wait for I/O Bound Workloads.
Last edited by AnalyticalVega on August 20th, 2015, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

.NET Performance Tuning Tips

August 15th, 2015, 5:24 pm

26) Avoid creating willy nilly collections of medium-grained objects27) GC generational collection(?)28) Hybrid C++ C++/CLI C# 29) avoid 'dynamic' types 30) Use TPL ?
Last edited by Cuchulainn on August 14th, 2015, 10:00 pm, edited 1 time in total.
 
User avatar
Polter
Posts: 1
Joined: April 29th, 2008, 4:55 pm

.NET Performance Tuning Tips

August 15th, 2015, 6:43 pm

Making .NET Applications Faster - Sasha Goldshtein:
demos: http://blogs.microsoft.co.il/sasha/2015 ... -2015/.NET Native Performance and Internals: http://blogs.microsoft.co.il/sasha/2014 ... s/Choosing a Collection Is a Matter of Cache, Too: http://blogs.microsoft.co.il/sasha/2012 ... rformance/
 
User avatar
AnalyticalVega
Topic Author
Posts: 104
Joined: January 16th, 2013, 5:03 am

.NET Performance Tuning Tips

August 16th, 2015, 1:01 am

QuoteOriginally posted by: PolterMaking .NET Applications Faster - Sasha Goldshtein:
demos: http://blogs.microsoft.co.il/sasha/2015 ... -2015/.NET Native Performance and Internals: http://blogs.microsoft.co.il/sasha/2014 ... s/Choosing a Collection Is a Matter of Cache, Too: http://blogs.microsoft.co.il/sasha/2012 ... /Excellent! Thank you! BTW: Do you know if Sasha's book is worth getting?
 
User avatar
AnalyticalVega
Topic Author
Posts: 104
Joined: January 16th, 2013, 5:03 am

.NET Performance Tuning Tips

August 16th, 2015, 3:04 am

QuoteOriginally posted by: Cuchulainn26) Avoid creating willy nilly collections of medium-grained objects27) GC generational collection(?)28) Hybrid C++ C++/CLI C# 29) avoid 'dynamic' types 30) Use TPL ?about 28) your C++ code would have to be a hell of a lot faster than the C# version to justify the overhead of invoking the code.http://www.codeproject.com/Articles/253 ... formanceIt may be faster to use .NET IL instead of C++ since you avoid the invocation overhead of Pinvoke or CLI.
 
User avatar
Cuchulainn
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

.NET Performance Tuning Tips

August 16th, 2015, 9:07 am

QuoteIt may be faster to use .NET IL instead of C++ since you avoid the invocation overhead of Pinvoke or CLI. If that is the answer, what is the question?It's a decision that needs to be made and it has its associated costs in manhours. The answer depends on the requirements. You may have developed a Ferrari to drive to the supermarket in a 30km/hour zone.One scenario: let's say we have a C++ legacy application that we want to wrap in C#. How to proceed? That extra little perceived performance hit is something that might be tolerated. I doubt if many organisations will do a port C++ to C#. You may wish to use C# for everything except for the number crunching.
Last edited by Cuchulainn on August 15th, 2015, 10:00 pm, edited 1 time in total.
 
User avatar
AnalyticalVega
Topic Author
Posts: 104
Joined: January 16th, 2013, 5:03 am

.NET Performance Tuning Tips

August 16th, 2015, 3:06 pm

QuoteOriginally posted by: CuchulainnQuoteIt may be faster to use .NET IL instead of C++ since you avoid the invocation overhead of Pinvoke or CLI. If that is the answer, what is the question?It's a decision that needs to be made and it has its associated costs in manhours. The answer depends on the requirements. You may have developed a Ferrari to drive to the supermarket in a 30km/hour zone.One scenario: let's say we have a C++ legacy application that we want to wrap in C#. How to proceed? That extra little perceived performance hit is something that might be tolerated. I doubt if many organisations will do a port C++ to C#. You may wish to use C# for everything except for the number crunching.Yes of course if you already have the C++ code, it may make sense to use it if the invocation overhead is not too terrible given your requirements.
 
User avatar
dragonfly2
Posts: 0
Joined: August 19th, 2015, 10:16 am

.NET Performance Tuning Tips

August 19th, 2015, 4:48 pm

good stuff!!!Thanks...