Code Quality Design Help

Using Record Structs for Enhanced Performance in .NET

When developing DTOs or Value Objects in .NET, consider the adoption of record structs instead of traditional records (which are classes). This change can lead to improved performance metrics and reduced memory overhead due to the value-type nature of structs, avoiding heap allocations for small, frequently created objects.

Record Class Example:

public record BookId { public required Guid Value { get; init; } }

Record Struct Example:

public record struct AuthorId { public required Guid Value { get; init; } }

Through benchmarking, we observe a considerable performance increase and memory savings when opting for record structs, especially when these types are instantiated frequently:

Method

Mean (ns)

Allocated (B)

BookIdRecord

126.41

128

AuthorIdRecordStruct

51.90

96

While the differences may appear trivial at a glance, with execution times in nanoseconds, the shift to record struct yields a roughly 140% increase in speed (lower is better, so it takes less time) and a notable 32 B reduction in memory allocation per instance in this specific benchmark. This optimization, albeit small on a per-instance basis, can accumulate significantly in high-throughput systems, reducing memory pressure on the garbage collector. Choose record struct when value-type semantics and performance are critical for small, immutable data holders.

See Also:

05 May 2025