- Frugal Cafe
- Posts
- FC15 Immutable? Isn't that like constants?
FC15 Immutable? Isn't that like constants?
Constants are reusable.
There are lots of .NET APIs I have never used; one such example is classes in Immutable namespace. Honestly, I do not see a reason using them yet.
Found an expensive allocation stack in ServiceHub.RoslynCodeAnalysisService process (I’m using Visual Studio 2022 community edition):
SupportedDiagnostics property getter is responsible for 2.2% allocations in the process. Source code:
The builder capacity seems to be missing by one. Here is the inner method:
I totally do not see such code coming from Roslyn team which I have respect for (I’ve copied their StringTable/ObjectCache classes).
Enum.GetValues should be queried once, with results reused. The result type should be casted to ErrorCode[], or else all flags would be boxed. The comparisons with ErrorCode.Void and ErrorCode.Unknown are also doing box. Overall, the whole thing is a constant, so why not generate once and reuse?
I have not figured out how does PerfView manage to get its source.
Here is a bigger allocation stack:
GetMember is responsible for 7.5% allocations, due to ImmutableArray.
Here is another place where data can be easily reused: