BT

Performance of Structs in C# 7.2

| by Pierre-Luc Maheu Follow 4 Followers on Jul 31, 2018. Estimated reading time: 1 minute |

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 struct signature is not readonly.
  •  The struct variable has the readonly modifier.
  •  A method (including properties) is called.
public struct SomeStruct  
{
	private int _x;

	public int X { get { return _x; } }
}

private readonly SomeStruct s = new SomeStruct(42);

s.X; // Compiler creates a defensive copy.

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.

public void BadFunction(in SomeStruct s)
{
  s.X; // Compiler creates a defensive copy.
}

 

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.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT