Performance implications of default struct equality in C#
https://devblogs.microsoft.com/premier-developer/performance-implications-of-default-struct-equality-in-c/
https://medium.com/@semuserable/c-journey-into-struct-equality-comparison-deep-dive-9693f74562f1
#csharp #struct #equality #performance
https://devblogs.microsoft.com/premier-developer/performance-implications-of-default-struct-equality-in-c/
https://medium.com/@semuserable/c-journey-into-struct-equality-comparison-deep-dive-9693f74562f1
#csharp #struct #equality #performance
Microsoft News
Performance implications of default struct equality in C#
If you’re familiar with C#, then you most likely heard that you should always override Equals and GetHashCode for custom structs for performance reasons. To better understand the importance and the rationale behind this advice we’re going to look at the default…
To box or not to box
Details about using statement with structs. Once again the moral of the story is: mutable value types are enough pure evil to turn you all into hermit crabs, and therefore should be avoided.
https://ericlippert.com/2011/03/14/to-box-or-not-to-box/
#disposable #struct
Details about using statement with structs. Once again the moral of the story is: mutable value types are enough pure evil to turn you all into hermit crabs, and therefore should be avoided.
https://ericlippert.com/2011/03/14/to-box-or-not-to-box/
#disposable #struct
Fabulous adventures in coding
To box or not to box
Suppose you have an immutable value type that is also disposable. Perhaps it represents some sort of handle. struct MyHandle : IDisposable { public MyHandle(int handle) : this() { this.Handle = han…
Svelto.ECS Internals: How to avoid boxing when using structs with reflection
https://www.sebaslab.com/casting-a-struct-into-an-interface-inside-a-generic-method-without-boxing/
#struct #reflection #boxing
https://www.sebaslab.com/casting-a-struct-into-an-interface-inside-a-generic-method-without-boxing/
#struct #reflection #boxing
Seba's Lab
Svelto.ECS Internals: How to avoid boxing when using structs with reflection - Seba's Lab
The home of Svelto C# ECS and advanced Unity gamedev C# articles
Why is List<Struct> 15 Times Faster to Allocate than List<Class> in C#
https://levelup.gitconnected.com/why-is-list-struct-is-15-times-faster-to-allocate-than-list-class-17f5f79889ae
#allocation #struct
https://levelup.gitconnected.com/why-is-list-struct-is-15-times-faster-to-allocate-than-list-class-17f5f79889ae
#allocation #struct
Medium
Why is List<Struct> 15 Times Faster to Allocate than List<Class> in C#
In the previous article, 5 Ways to Improve the Performance of C# Code for Free, in one of the examples, I measured the time difference…
Performance traps of ref locals and ref returns in C#
Be cautious with ref returns because they can break encapsulation.
Be cautious with readonly ref returns because they’re more performant only for readonly structs and could cause performance issues for regular structs.
Be cautious with readonly ref locals because they also could cause performance issues for non-readonly structs causing defensive copy each time the variable is used.
https://devblogs.microsoft.com/premier-developer/performance-traps-of-ref-locals-and-ref-returns-in-c/
#struct #readonly #ref #performance
Be cautious with ref returns because they can break encapsulation.
Be cautious with readonly ref returns because they’re more performant only for readonly structs and could cause performance issues for regular structs.
Be cautious with readonly ref locals because they also could cause performance issues for non-readonly structs causing defensive copy each time the variable is used.
https://devblogs.microsoft.com/premier-developer/performance-traps-of-ref-locals-and-ref-returns-in-c/
#struct #readonly #ref #performance
Microsoft News
Performance traps of ref locals and ref returns in C#
The C# language from the very first version supported passing arguments by value or by reference. But before C# 7 the C# compiler supported only one way of returning a value from a method (or a property) – returning by value.
👍2
Optimize struct performances using StructLayout
I ran a simple benchmark that passes structs as an argument: one uses StructLayout.Sequential and another StructLayout.Auto.
I intentionally ordered fields in a way that makes them have padding.
And result is (the formatting in mobile clients sucks):
It can be avoided by using 'ref' parameter so no copying occurs. I've also thrown readonly structs to the test.
https://www.meziantou.net/optimize-struct-performances-using-structlayout.htm
#optimization #struct #layout #ref
I ran a simple benchmark that passes structs as an argument: one uses StructLayout.Sequential and another StructLayout.Auto.
I intentionally ordered fields in a way that makes them have padding.
And result is (the formatting in mobile clients sucks):
| Method | Mean | Ratio |
|------- |---------:|------:|
| Auto | 298.6 us | 1.00 |
| Seq | 359.0 us | 1.20 |
Obviously copying struct that is bigger due to the padding to pass it as an arg is slower. But this operation is so quick that it might be a problem only on a very hot path.It can be avoided by using 'ref' parameter so no copying occurs. I've also thrown readonly structs to the test.
| Method | Mean | Ratio |
|------------- |---------:|------:|
| Auto | 318.5 us | 1.00 |
| Seq | 398.7 us | 1.25 |
| AutoRef | 254.5 us | 0.80 |
| SeqRef | 299.7 us | 0.94 |
| AutoReadonly | 312.9 us | 0.98 |
| SeqReadonly | 390.5 us | 1.22 |
As you can see passing both structs by ref is definitely faster. But Auto is again faster than Sequential. While readonly structs don't have any performance benefits according to this benchmark. To make sure a defensive copy of a readonly struct is avoided, I've also added 'in' parameter to the arg and now we can see that structs are not copied anymore and performance is on par with the 'ref' parameter| Method | Mean | Ratio |
|--------------- |---------:|------:|
| Auto | 299.9 us | 1.00 |
| Seq | 369.1 us | 1.23 |
| AutoRef | 245.1 us | 0.82 |
| SeqRef | 305.7 us | 1.02 |
| AutoReadonly | 309.1 us | 1.03 |
| SeqReadonly | 369.6 us | 1.23 |
| AutoReadonlyIn | 255.4 us | 0.86 |
| SeqReadonlyIn | 303.4 us | 1.02 |
I performed all these benchmarks in .NET. It would be very interesting to look at it in Unity using IL2CPP, whether it affects performance in C++ or not.https://www.meziantou.net/optimize-struct-performances-using-structlayout.htm
#optimization #struct #layout #ref
Meziantou's blog
Optimize struct performances using StructLayout
A struct type is a value type that is typically used to encapsulate small groups of related variables. It is often used for interop with native DLL ([DllImport]). Structs are also used for performance reasons. Value types are allocated on the stack so it…
👍4