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 boxed bool values to reduce allocation overhead #1012

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
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
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