Домой United States USA — software Performance of Structs in C# 7.2

Performance of Structs in C# 7.2

332
0
ПОДЕЛИТЬСЯ

The C# compiler, under some circumstances involving readonly, creates defensive copies of a struct. While this issue is well known and documented, it’s worth revisiting as it’s tied to several features of C# 7.2. The in and ref readonly keywords make occurrences of the issue more frequent, while readonly structs offer a way to fix it.
The C# compiler, under some circumstances involving readonly, creates defensive copies of a struct. While this issue is well known and documented, it’s worth revisiting as it’s tied to several features of C# 7.2. The in and ref readonly keywords make occurrences of the issue more frequent, while readonly structs offer a way to fix it.
Structs in C# are generally used for high-performance purposes to avoid the cost of memory allocation/dealocation. However, the potential pitfalls have restrained their use. C# 7.2 adds an improvement, readonly structs, to deal with the most common occurrences of the issue.
The C# compiler will create copies of a struct if:
The same rules apply when x is an in -parameter, a ref readonly local variable or a result of a method invocation that returns a value by readonly reference.
C# 7.2 adds the possibility to declare a struct readonly, providing a solution to avoid defensive copies. Structs declared as readonly can’t have property setters and prevents assignation of the struct members.
The defensive copies issue can be detected through static analysis. ErrorProne. NET is inspired from ErrorProne, a static analysis tool for Java. The. NET port is a set of Roslyn analyzers with a focus on correctness and performance. A subset of analyzers focuses on structs and is available in a Nuget package.

Continue reading...