
Structs have their advantages in .NET: they are especially efficient in the runtime if used correctly.
But if you use structs with an interface, the massive advantages unfortunately turn into disadvantages:
As soon as an interface is attached to a struct, Boxing
comes into effect; the values are thus copied over and over again, which is usually not what you want - but it also shows up in the benchmarks.
1BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19044.2486/21H2/November2021Update)
2AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
3.NET SDK=7.0.200-preview.22628.1
4 [Host] : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT AVX2
5 DefaultJob : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT AVX2
6
7
8| Method | Mean | Error | StdDev | Median | Gen0 | Allocated |
9|-------------------- |----------:|----------:|----------:|----------:|-------:|----------:|
10| Class | 2.9964 ns | 0.0713 ns | 0.0667 ns | 3.0374 ns | 0.0014 | 24 B |
11| ClassWithInterface | 3.0590 ns | 0.1043 ns | 0.0976 ns | 3.0742 ns | 0.0014 | 24 B |
12| Struct | 0.0018 ns | 0.0030 ns | 0.0023 ns | 0.0012 ns | - | - |
13| StructWithInterface | 3.1241 ns | 0.0835 ns | 0.0781 ns | 3.1147 ns | 0.0014 | 24 B |
In principle, this behavior is nothing new. Potential solutions are already being worked dotnet/csharplang - Proposal: Allow readonly ref structs to implement interfaces, but disallow boxing them #1479 on so that structs can be used boxing-free.

Comments