Skip to content

Commit

Permalink
Merge pull request #1012 from Erarndt/avoidBoolBoxing
Browse files Browse the repository at this point in the history
Cache boxed bool values to reduce allocation overhead
  • Loading branch information
davkean committed Apr 1, 2022
2 parents 1a5b137 + 600cb05 commit 2b3b6d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/Microsoft.VisualStudio.Threading/Boxed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.Threading
{
internal static class Boxed
{
/// <summary>
/// Returns an object containing <see langword="true"/>.
/// </summary>
public static readonly object True = true;

/// <summary>
/// Returns an object containing <see langword="false"/>.
/// </summary>
public static readonly object False = false;

/// <summary>
/// Returns an object containing specified value.
/// </summary>
public static object Box(bool value)
{
return value ? True : False;
}
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.VisualStudio.Threading/ThreadingEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void WaitReaderWriterLockStop(int lockId, AsyncReaderWriterLock.LockKind
[Event(CompleteOnCurrentThreadStartEvent)]
public void CompleteOnCurrentThreadStart(int taskId, bool isOnMainThread)
{
this.WriteEvent(CompleteOnCurrentThreadStartEvent, taskId, isOnMainThread);
this.WriteEvent(CompleteOnCurrentThreadStartEvent, taskId, Boxed.Box(isOnMainThread));
}

/// <summary>
Expand Down Expand Up @@ -145,7 +145,7 @@ public void WaitSynchronouslyStop()
[Event(PostExecutionStartEvent, Level = EventLevel.Verbose)]
public void PostExecutionStart(int requestId, bool mainThreadAffinitized)
{
this.WriteEvent(PostExecutionStartEvent, requestId, mainThreadAffinitized);
this.WriteEvent(PostExecutionStartEvent, requestId, Boxed.Box(mainThreadAffinitized));
}

/// <summary>
Expand Down

0 comments on commit 2b3b6d1

Please sign in to comment.