From 90ec1a62c4472723cf181febb2e4bc6041c07d6a Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Thu, 13 Jul 2023 00:58:23 +0300 Subject: [PATCH 1/2] Cache RetargetingParameterSymbol.TypeWithAnnotations to avoid ~3.5% allocations in a trace --- .../Symbols/Retargeting/RetargetingParameterSymbol.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs index dd32f3e7212af..b8de43056c685 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs @@ -23,6 +23,8 @@ internal abstract class RetargetingParameterSymbol : WrappedParameterSymbol /// private ImmutableArray _lazyCustomAttributes; + private TypeWithAnnotations? _lazyTypeWithAnnotations; + protected RetargetingParameterSymbol(ParameterSymbol underlyingParameter) : base(underlyingParameter) { @@ -38,7 +40,12 @@ public sealed override TypeWithAnnotations TypeWithAnnotations { get { - return this.RetargetingModule.RetargetingTranslator.Retarget(_underlyingParameter.TypeWithAnnotations, RetargetOptions.RetargetPrimitiveTypesByTypeCode); + if (!_lazyTypeWithAnnotations.HasValue) + { + _lazyTypeWithAnnotations = this.RetargetingModule.RetargetingTranslator.Retarget(_underlyingParameter.TypeWithAnnotations, RetargetOptions.RetargetPrimitiveTypesByTypeCode); + } + + return _lazyTypeWithAnnotations.Value; } } From ca72dde79094338dc37d9e87c48f9369b7921fcd Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Thu, 13 Jul 2023 18:27:22 +0300 Subject: [PATCH 2/2] Use Interlocked.CompareExchange along with TypeWithAnnotations.Boxed --- .../Symbols/Retargeting/RetargetingParameterSymbol.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs index b8de43056c685..36b791724d1b6 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; +using System.Threading; using Microsoft.CodeAnalysis.CSharp.Emit; namespace Microsoft.CodeAnalysis.CSharp.Symbols.Retargeting @@ -23,7 +24,7 @@ internal abstract class RetargetingParameterSymbol : WrappedParameterSymbol /// private ImmutableArray _lazyCustomAttributes; - private TypeWithAnnotations? _lazyTypeWithAnnotations; + private TypeWithAnnotations.Boxed? _lazyTypeWithAnnotations; protected RetargetingParameterSymbol(ParameterSymbol underlyingParameter) : base(underlyingParameter) @@ -40,9 +41,11 @@ public sealed override TypeWithAnnotations TypeWithAnnotations { get { - if (!_lazyTypeWithAnnotations.HasValue) + if (_lazyTypeWithAnnotations is null) { - _lazyTypeWithAnnotations = this.RetargetingModule.RetargetingTranslator.Retarget(_underlyingParameter.TypeWithAnnotations, RetargetOptions.RetargetPrimitiveTypesByTypeCode); + Interlocked.CompareExchange(ref _lazyTypeWithAnnotations, + new TypeWithAnnotations.Boxed(this.RetargetingModule.RetargetingTranslator.Retarget(_underlyingParameter.TypeWithAnnotations, RetargetOptions.RetargetPrimitiveTypesByTypeCode)), + null); } return _lazyTypeWithAnnotations.Value;