48
©2016 GlobalLogic Inc.

NET Performance Boost

Embed Size (px)

Citation preview

Page 1: NET Performance Boost

©2016 GlobalLogic Inc.

Page 2: NET Performance Boost

2

.NET Performance Boost

By Andrii Antilikatorov

Page 3: NET Performance Boost

3

Page 4: NET Performance Boost

4

Optimize or not optimize?

Page 5: NET Performance Boost

5

Page 6: NET Performance Boost

6

Page 7: NET Performance Boost

7

Page 8: NET Performance Boost

8

Quiz

Page 9: NET Performance Boost

9

Jagged arrays vs 2D arrays

4571ns2864ns

Page 10: NET Performance Boost

10

Jagged arrays

Page 11: NET Performance Boost

11

FOR vs FOREACH

Page 12: NET Performance Boost

12

FOR vs FOREACH

… it depends

Page 13: NET Performance Boost

13

FOR vs FOREACH

Page 14: NET Performance Boost

14

Performance of try...catch

2555ns 674ns

Page 15: NET Performance Boost

15

Create new collection or clear existing one?

645ns

501ns

Page 16: NET Performance Boost

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

17

Avoiding Boxing

bool areEqual = v1.Equals(v2);

Page 18: NET Performance Boost

18

Avoiding Boxing

bool areEqual = v1.Equals(v2);

Page 19: NET Performance Boost

19

Avoiding Boxing

Page 20: NET Performance Boost

20

Avoiding Boxing

Page 21: NET Performance Boost

21

Avoiding Boxing

Page 22: NET Performance Boost

22

Avoiding Boxing

Page 23: NET Performance Boost

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

24

Cost of method calls

CALL

CALLVIRTDependency Injection

Page 25: NET Performance Boost

25

Parameter order performance

952ms

2224ms

Page 26: NET Performance Boost

26

Field access optimization

15.86ns

Page 27: NET Performance Boost

27

Field access optimization

5.18ns

Page 28: NET Performance Boost

28

Multiple return values

1.64ns

0.32ns

5.40ns

Page 29: NET Performance Boost

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

30

Array Copy

1343ns

1013ns

Page 31: NET Performance Boost

31

Performance of DateTime

264ns

16 ns

Page 32: NET Performance Boost

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

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

34

Memory Alignment

Type alignment Page size alignment

Cache line alignment

Memory Alignment

Page 35: NET Performance Boost

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

36

Type-aligned vs unaligned Access Test

Number of pages

Alignment

Relative cost

Page 37: NET Performance Boost

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

38

Cross-line cache access

Number of pages

Offset

Relative cost

Page 39: NET Performance Boost

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

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

41

Cost of Access Based on Cache Line LocationOffset

Number of pagesRelative cost

Page 42: NET Performance Boost

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

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

44

Synchronized or volatile?

Page 45: NET Performance Boost

45

Synchronized or volatile?

MESI Protocol

• Modified

• Exclusive

• Shared

• Invalid

Page 46: NET Performance Boost

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

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

©2016 GlobalLogic Inc.