.NET Performance Boost
-
Upload
globallogic-ukraine -
Category
Software
-
view
869 -
download
0
Transcript of .NET Performance Boost
![Page 1: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/1.jpg)
©2016 GlobalLogic Inc.
![Page 2: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/2.jpg)
2
.NET Performance Boost
By Andrii Antilikatorov
![Page 3: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/3.jpg)
3
![Page 4: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/4.jpg)
4
Optimize or not optimize?
![Page 5: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/5.jpg)
5
![Page 6: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/6.jpg)
6
![Page 7: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/7.jpg)
7
![Page 8: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/8.jpg)
8
Quiz
![Page 9: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/9.jpg)
9
Jagged arrays vs 2D arrays
4571ns2864ns
![Page 10: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/10.jpg)
10
Jagged arrays
![Page 11: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/11.jpg)
11
FOR vs FOREACH
![Page 12: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/12.jpg)
12
FOR vs FOREACH
… it depends
![Page 13: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/13.jpg)
13
FOR vs FOREACH
![Page 14: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/14.jpg)
14
Performance of try...catch
2555ns 674ns
![Page 15: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/15.jpg)
15
Create new collection or clear existing one?
645ns
501ns
![Page 16: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/16.jpg)
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
![Page 17: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/17.jpg)
17
Avoiding Boxing
bool areEqual = v1.Equals(v2);
![Page 18: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/18.jpg)
18
Avoiding Boxing
bool areEqual = v1.Equals(v2);
![Page 19: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/19.jpg)
19
Avoiding Boxing
![Page 20: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/20.jpg)
20
Avoiding Boxing
![Page 21: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/21.jpg)
21
Avoiding Boxing
![Page 22: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/22.jpg)
22
Avoiding Boxing
![Page 23: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/23.jpg)
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.
![Page 24: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/24.jpg)
24
Cost of method calls
CALL
CALLVIRTDependency Injection
![Page 25: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/25.jpg)
25
Parameter order performance
952ms
2224ms
![Page 26: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/26.jpg)
26
Field access optimization
15.86ns
![Page 27: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/27.jpg)
27
Field access optimization
5.18ns
![Page 28: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/28.jpg)
28
Multiple return values
1.64ns
0.32ns
5.40ns
![Page 29: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/29.jpg)
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
![Page 30: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/30.jpg)
30
Array Copy
1343ns
1013ns
![Page 31: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/31.jpg)
31
Performance of DateTime
264ns
16 ns
![Page 32: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/32.jpg)
32
Memory Access
Off-Heap
(Unmanaged)
Memory-Mapped Files
Heap
(Managed)
1.068s1.096s
LOH optimization
P/Invoke, COM, Marshalling
![Page 33: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/33.jpg)
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.
![Page 34: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/34.jpg)
34
Memory Alignment
Type alignment Page size alignment
Cache line alignment
Memory Alignment
![Page 35: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/35.jpg)
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.
![Page 36: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/36.jpg)
36
Type-aligned vs unaligned Access Test
Number of pages
Alignment
Relative cost
![Page 37: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/37.jpg)
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.
![Page 38: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/38.jpg)
38
Cross-line cache access
Number of pages
Offset
Relative cost
![Page 39: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/39.jpg)
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.
![Page 40: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/40.jpg)
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.
![Page 41: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/41.jpg)
41
Cost of Access Based on Cache Line LocationOffset
Number of pagesRelative cost
![Page 42: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/42.jpg)
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.
![Page 43: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/43.jpg)
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
![Page 44: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/44.jpg)
44
Synchronized or volatile?
![Page 45: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/45.jpg)
45
Synchronized or volatile?
MESI Protocol
• Modified
• Exclusive
• Shared
• Invalid
![Page 46: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/46.jpg)
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
![Page 47: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/47.jpg)
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
![Page 48: .NET Performance Boost](https://reader031.fdocuments.us/reader031/viewer/2022022415/58e7e0451a28ab3a578b61dd/html5/thumbnails/48.jpg)
©2016 GlobalLogic Inc.