Upload
globallogic-ukraine
View
869
Download
0
Embed Size (px)
Citation preview
©2016 GlobalLogic Inc.
2
.NET Performance Boost
By Andrii Antilikatorov
3
4
Optimize or not optimize?
5
6
7
8
Quiz
9
Jagged arrays vs 2D arrays
4571ns2864ns
10
Jagged arrays
11
FOR vs FOREACH
12
FOR vs FOREACH
… it depends
13
FOR vs FOREACH
14
Performance of try...catch
2555ns 674ns
15
Create new collection or clear existing one?
645ns
501ns
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
17
Avoiding Boxing
bool areEqual = v1.Equals(v2);
18
Avoiding Boxing
bool areEqual = v1.Equals(v2);
19
Avoiding Boxing
20
Avoiding Boxing
21
Avoiding Boxing
22
Avoiding Boxing
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.
24
Cost of method calls
CALL
CALLVIRTDependency Injection
25
Parameter order performance
952ms
2224ms
26
Field access optimization
15.86ns
27
Field access optimization
5.18ns
28
Multiple return values
1.64ns
0.32ns
5.40ns
29
Tuple/KeyValuePair performance
Tuple KeyValuePair
Allocate 8.23ns 0.32ns
Pass as argument 1.93ns 2.57ns
Return 6.09ns 1.91ns
Load from List 2.79ns 4.18ns
30
Array Copy
1343ns
1013ns
31
Performance of DateTime
264ns
16 ns
32
Memory Access
Off-Heap
(Unmanaged)
Memory-Mapped Files
Heap
(Managed)
1.068s1.096s
LOH optimization
P/Invoke, COM, Marshalling
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.
34
Memory Alignment
Type alignment Page size alignment
Cache line alignment
Memory Alignment
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.
36
Type-aligned vs unaligned Access Test
Number of pages
Alignment
Relative cost
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.
38
Cross-line cache access
Number of pages
Offset
Relative cost
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
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
Cost of Access Based on Cache Line LocationOffset
Number of pagesRelative cost
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
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
44
Synchronized or volatile?
45
Synchronized or volatile?
MESI Protocol
• Modified
• Exclusive
• Shared
• Invalid
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
47
Summary
Architecture first
Remember rule 80/20
Don’t try to write ideal code from the beginning
Don’t create bicycles
Don’t optimize for particular platform
©2016 GlobalLogic Inc.