From 6db1571ef12239ea8f2ea93498729ba0c5d7ae19 Mon Sep 17 00:00:00 2001 From: Todd Grunke Date: Wed, 9 Aug 2023 11:10:17 -0700 Subject: [PATCH] Avoid boxing when structs are used as collection keys. Change several structs to record structs such that they no longer are boxed when used as keys in MultiDictionary's internal ImmutableHashSet. When MultiDictionary's value is a struct and is created without specifying the value's IEqualityComparer, the default struct comparer is used. This equality comparer is notoriously slow due to boxing. By switching these structs to record structs, an implementation of IEquatable is generated and the default struct equality comparer isn't used. Addresses https://github.com/dotnet/roslyn/issues/69165 Sam mentioned that there is an issue already logged in the roslyn-analyzers repo around adding an analyzer to detect this. https://github.com/dotnet/roslyn-analyzers/issues/1640 --- .../DocumentHighlighting/IDocumentHighlightsService.cs | 2 +- .../FindSymbols/SymbolTree/SymbolTreeInfo.Node.cs | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Features/Core/Portable/DocumentHighlighting/IDocumentHighlightsService.cs b/src/Features/Core/Portable/DocumentHighlighting/IDocumentHighlightsService.cs index b43933dfb606f..cd752c003174f 100644 --- a/src/Features/Core/Portable/DocumentHighlighting/IDocumentHighlightsService.cs +++ b/src/Features/Core/Portable/DocumentHighlighting/IDocumentHighlightsService.cs @@ -20,7 +20,7 @@ internal enum HighlightSpanKind } [DataContract] - internal readonly struct HighlightSpan + internal readonly record struct HighlightSpan { [DataMember(Order = 0)] public TextSpan TextSpan { get; } diff --git a/src/Workspaces/Core/Portable/FindSymbols/SymbolTree/SymbolTreeInfo.Node.cs b/src/Workspaces/Core/Portable/FindSymbols/SymbolTree/SymbolTreeInfo.Node.cs index 43ba11acad848..4190f5cbe63de 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/SymbolTree/SymbolTreeInfo.Node.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/SymbolTree/SymbolTreeInfo.Node.cs @@ -6,7 +6,6 @@ using System.Diagnostics; using System.Reflection.Metadata; using System.Reflection.Metadata.Ecma335; -using Microsoft.CodeAnalysis.Text; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.FindSymbols @@ -63,11 +62,11 @@ private string GetDebuggerDisplay() => Name + ", " + ParentIndex; } - private readonly struct ParameterTypeInfo(string name, bool isComplex, bool isArray) + private readonly record struct ParameterTypeInfo(string name, bool isComplex, bool isArray) { /// /// This is the type name of the parameter when is false. - /// For array types, this is just the elemtent type name. + /// For array types, this is just the element type name. /// e.g. `int` for `int[][,]` /// public readonly string Name = name; @@ -96,11 +95,11 @@ private readonly struct ParameterTypeInfo(string name, bool isComplex, bool isAr public readonly bool IsComplexType = isComplex; } - public readonly struct ExtensionMethodInfo(string fullyQualifiedContainerName, string name) + public readonly record struct ExtensionMethodInfo(string fullyQualifiedContainerName, string name) { /// /// Name of the extension method. - /// This can be used to retrive corresponding symbols via + /// This can be used to retrieve corresponding symbols via /// public readonly string Name = name;