Skip to content

Commit 33243ad

Browse files
authored
Use pre-boxed values in diagnostics (#50013)
1 parent f94332e commit 33243ad

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Http/Routing/src/RoutingMetrics.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ internal sealed class RoutingMetrics
99
{
1010
public const string MeterName = "Microsoft.AspNetCore.Routing";
1111

12+
// Reuse boxed object for common values
13+
private static readonly object BoxedTrue = true;
14+
private static readonly object BoxedFalse = false;
15+
1216
private readonly Meter _meter;
1317
private readonly Counter<long> _matchAttemptsCounter;
1418

@@ -29,7 +33,7 @@ public void MatchSuccess(string route, bool isFallback)
2933
_matchAttemptsCounter.Add(1,
3034
new KeyValuePair<string, object?>("http.route", route),
3135
new KeyValuePair<string, object?>("aspnetcore.routing.match_status", "success"),
32-
new KeyValuePair<string, object?>("aspnetcore.routing.is_fallback", isFallback));
36+
new KeyValuePair<string, object?>("aspnetcore.routing.is_fallback", isFallback ? BoxedTrue : BoxedFalse));
3337
}
3438

3539
public void MatchFailure()

src/Mvc/Mvc.ViewFeatures/src/Diagnostics/MvcDiagnostics.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ public AfterViewEventData(IView view, ViewContext viewContext)
310310
/// </summary>
311311
public sealed class ViewFoundEventData : EventData
312312
{
313+
// Reuse boxed object for common values
314+
private static readonly object BoxedTrue = true;
315+
private static readonly object BoxedFalse = false;
316+
313317
/// <summary>
314318
/// The name of the event.
315319
/// </summary>
@@ -364,7 +368,7 @@ public ViewFoundEventData(ActionContext actionContext, bool isMainPage, ActionRe
364368
protected override KeyValuePair<string, object> this[int index] => index switch
365369
{
366370
0 => new KeyValuePair<string, object>(nameof(ActionContext), ActionContext),
367-
1 => new KeyValuePair<string, object>(nameof(IsMainPage), IsMainPage),
371+
1 => new KeyValuePair<string, object>(nameof(IsMainPage), IsMainPage ? BoxedTrue : BoxedFalse),
368372
2 => new KeyValuePair<string, object>(nameof(Result), Result),
369373
3 => new KeyValuePair<string, object>(nameof(ViewName), ViewName),
370374
4 => new KeyValuePair<string, object>(nameof(View), View),
@@ -377,6 +381,10 @@ public ViewFoundEventData(ActionContext actionContext, bool isMainPage, ActionRe
377381
/// </summary>
378382
public sealed class ViewNotFoundEventData : EventData
379383
{
384+
// Reuse boxed object for common values
385+
private static readonly object BoxedTrue = true;
386+
private static readonly object BoxedFalse = false;
387+
380388
/// <summary>
381389
/// The name of the event.
382390
/// </summary>
@@ -431,7 +439,7 @@ public ViewNotFoundEventData(ActionContext actionContext, bool isMainPage, Actio
431439
protected override KeyValuePair<string, object> this[int index] => index switch
432440
{
433441
0 => new KeyValuePair<string, object>(nameof(ActionContext), ActionContext),
434-
1 => new KeyValuePair<string, object>(nameof(IsMainPage), IsMainPage),
442+
1 => new KeyValuePair<string, object>(nameof(IsMainPage), IsMainPage ? BoxedTrue : BoxedFalse),
435443
2 => new KeyValuePair<string, object>(nameof(Result), Result),
436444
3 => new KeyValuePair<string, object>(nameof(ViewName), ViewName),
437445
4 => new KeyValuePair<string, object>(nameof(SearchedLocations), SearchedLocations),

0 commit comments

Comments
 (0)