From f87d61003ab159224efc0e9911eb46bd7709bff8 Mon Sep 17 00:00:00 2001 From: Todd Grunke Date: Mon, 18 Mar 2024 15:12:54 -0700 Subject: [PATCH] Reduce allocations in AbstractTypeMap Partial fix for https://github.com/dotnet/roslyn/issues/68996 Both SubstituteTypesWithoutModifiers and SubstituteNamedTypes allocate a temporary array that is used to potentially return an immutable array. Previously, this immutable array was allocated if needed, whereas we can not perform the extra allocation by utilizing ImmutableCollectionsMarshal as the array being wrapped is local to this method. --- src/Compilers/CSharp/Portable/Symbols/AbstractTypeMap.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Symbols/AbstractTypeMap.cs b/src/Compilers/CSharp/Portable/Symbols/AbstractTypeMap.cs index 555fcbf36d926..ee43c93bf9e79 100644 --- a/src/Compilers/CSharp/Portable/Symbols/AbstractTypeMap.cs +++ b/src/Compilers/CSharp/Portable/Symbols/AbstractTypeMap.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; +using System.Runtime.InteropServices; using Microsoft.CodeAnalysis.PooledObjects; using Roslyn.Utilities; @@ -313,7 +314,7 @@ internal ImmutableArray SubstituteTypesWithoutModifiers(ImmutableArr } } - return result != null ? result.AsImmutableOrNull() : original; + return result != null ? ImmutableCollectionsMarshal.AsImmutableArray(result) : original; } internal ImmutableArray SubstituteTypes(ImmutableArray original) @@ -430,7 +431,7 @@ internal ImmutableArray SubstituteNamedTypes(ImmutableArray