This presentation is about various aspects of low-level optimization: productivity and consumption of various data structures, hidden pitfalls of popular approaches and templates, thread synchronization, memory access methods, effective processor cache usage and more.
This presentation by Andrii Antilikatorov (Consultant, GlobalLogic) was delivered at GlobalLogic Kharkiv .NET Conference on July 5, 2016.
16. 16
String pool
Can be disabled with CompilationRelaxationsAttribute
By default interns only string literals
Works great in multithreaded code
Has the same lifetime as CLR
May cause troubles if interning went out of control
23. 23
Avoiding Boxing
Use value types if objects are small and supposed to be used often.
Value types are useful for memory compaction.
Override Equals(), “==“ (“!=“). Implement IEquatable interface.
Override GetHashCode().
Make value types immutable.
33. 33
Memory-Mapped Files
Ideal to access file on disk without
performing I/O operations
The most efficient for cross-
process communication.
Allows properly protecting the
content and synchronize access.
35. 35
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than
unaligned access.
Memory access that spans 2 cache lines has far worse
performance than aligned mid-cache line access.
Cache line access performance changes based on
cache line location.
37. 37
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than
unaligned access.
Memory access that spans 2 cache lines has far worse
performance than aligned mid-cache line access.
Cache line access performance changes based on
cache line location.
39. 39
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than
unaligned access.
Memory access that spans 2 cache lines has far worse
performance than aligned mid-cache line access.
Cache line access performance changes based on
cache line location.
40. 40
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than
unaligned access.
Memory access that spans 2 cache lines has far worse
performance than aligned mid-cache line access.
Cache line access performance changes based on
cache line location.
41. 41
Cost of Access Based on Cache Line Location
Offset
Number of pagesRelative cost
42. 42
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than
unaligned access.
Memory access that spans 2 cache lines has far worse
performance than aligned mid-cache line access.
Cache line access performance changes based on
cache line location.
43. 43
Synchronized or volatile?
Volatile variables are less expensive than synchronized blocks
Volatile are thread-safe for atomic operations
Volatile acquires lock on variable, therefore not thread- safe
for non-atomic operations
Synchronized blocks have no performance impact if no
synchronization required
46. 46
Synchronized or volatile?
Volatile variables are less expensive than synchronized blocks
Volatile are thread-safe for atomic operations
Volatile acquires lock on variable, therefore not thread- safe
for non-atomic operations
Synchronized blocks have no performance impact if no
synchronization required