Skip to content

Commit e6736ca

Browse files
authored
Avoid boxing when structs are used as collection keys. (#69443)
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 #69165 Sam mentioned that there is an issue already logged in the roslyn-analyzers repo around adding an analyzer to detect this. dotnet/roslyn-analyzers#1640
1 parent d11f2bf commit e6736ca

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

src/Features/Core/Portable/DocumentHighlighting/IDocumentHighlightsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal enum HighlightSpanKind
2020
}
2121

2222
[DataContract]
23-
internal readonly struct HighlightSpan
23+
internal readonly record struct HighlightSpan
2424
{
2525
[DataMember(Order = 0)]
2626
public TextSpan TextSpan { get; }

src/Workspaces/Core/Portable/FindSymbols/SymbolTree/SymbolTreeInfo.Node.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Diagnostics;
77
using System.Reflection.Metadata;
88
using System.Reflection.Metadata.Ecma335;
9-
using Microsoft.CodeAnalysis.Text;
109
using Roslyn.Utilities;
1110

1211
namespace Microsoft.CodeAnalysis.FindSymbols
@@ -63,11 +62,11 @@ private string GetDebuggerDisplay()
6362
=> Name + ", " + ParentIndex;
6463
}
6564

66-
private readonly struct ParameterTypeInfo(string name, bool isComplex, bool isArray)
65+
private readonly record struct ParameterTypeInfo(string name, bool isComplex, bool isArray)
6766
{
6867
/// <summary>
6968
/// This is the type name of the parameter when <see cref="IsComplexType"/> is false.
70-
/// For array types, this is just the elemtent type name.
69+
/// For array types, this is just the element type name.
7170
/// e.g. `int` for `int[][,]`
7271
/// </summary>
7372
public readonly string Name = name;
@@ -96,11 +95,11 @@ private readonly struct ParameterTypeInfo(string name, bool isComplex, bool isAr
9695
public readonly bool IsComplexType = isComplex;
9796
}
9897

99-
public readonly struct ExtensionMethodInfo(string fullyQualifiedContainerName, string name)
98+
public readonly record struct ExtensionMethodInfo(string fullyQualifiedContainerName, string name)
10099
{
101100
/// <summary>
102101
/// Name of the extension method.
103-
/// This can be used to retrive corresponding symbols via <see cref="INamespaceOrTypeSymbol.GetMembers(string)"/>
102+
/// This can be used to retrieve corresponding symbols via <see cref="INamespaceOrTypeSymbol.GetMembers(string)"/>
104103
/// </summary>
105104
public readonly string Name = name;
106105

0 commit comments

Comments
 (0)