Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache the return values for GetIdForErrorCode #31966

Merged
merged 1 commit into from
Jan 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Compilers/Core/Portable/Diagnostic/CommonMessageProvider.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +13,11 @@ namespace Microsoft.CodeAnalysis
/// </summary>
internal abstract class CommonMessageProvider
{
/// <summary>
/// Caches the return values for <see cref="GetIdForErrorCode(int)"/>.
/// </summary>
private static readonly ConcurrentDictionary<(string prefix, int code), string> s_errorIdCache = new ConcurrentDictionary<(string prefix, int code), string>();
sharwell marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Given an error code, get the severity (warning or error) of the code.
/// </summary>
Expand Down Expand Up @@ -99,9 +104,13 @@ public Diagnostic CreateDiagnostic(int code, Location location)
/// <summary>
/// Given an error code (like 1234) return the identifier (CS1234 or BC1234).
/// </summary>
[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"));
}

/// <summary>
Expand Down