diff --git a/src/Compilers/Core/Portable/Diagnostic/CommonMessageProvider.cs b/src/Compilers/Core/Portable/Diagnostic/CommonMessageProvider.cs index 03795613214f1..cb0b393a12cf6 100644 --- a/src/Compilers/Core/Portable/Diagnostic/CommonMessageProvider.cs +++ b/src/Compilers/Core/Portable/Diagnostic/CommonMessageProvider.cs @@ -1,8 +1,8 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Concurrent; using System.Globalization; -using System.IO; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis @@ -13,6 +13,11 @@ namespace Microsoft.CodeAnalysis /// internal abstract class CommonMessageProvider { + /// + /// Caches the return values for . + /// + private static readonly ConcurrentDictionary<(string prefix, int code), string> s_errorIdCache = new ConcurrentDictionary<(string prefix, int code), string>(); + /// /// Given an error code, get the severity (warning or error) of the code. /// @@ -99,9 +104,13 @@ public Diagnostic CreateDiagnostic(int code, Location location) /// /// Given an error code (like 1234) return the identifier (CS1234 or BC1234). /// + [PerformanceSensitive( + "https://github.com/dotnet/roslyn/issues/31964", + AllowCaptures = false, + Constraint = "Frequently called by error list filtering; avoid allocations")] public string GetIdForErrorCode(int errorCode) { - return CodePrefix + errorCode.ToString("0000"); + return s_errorIdCache.GetOrAdd((CodePrefix, errorCode), key => key.prefix + key.code.ToString("0000")); } ///