Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit e1730db

Browse files
committed
Avoid allocating when SuppressFlow'ing in default ExecutionContext (#17081)
* Avoid allocating when SuppressFlow'ing in default ExecutionContext * Address PR feedback Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent 29d5555 commit e1730db

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/Common/src/CoreLib/System/Threading/ExecutionContext.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace System.Threading
2424
public sealed class ExecutionContext : IDisposable, ISerializable
2525
{
2626
internal static readonly ExecutionContext Default = new ExecutionContext(isDefault: true);
27+
internal static readonly ExecutionContext DefaultFlowSuppressed = new ExecutionContext(AsyncLocalValueMap.Empty, Array.Empty<IAsyncLocal>(), isFlowSuppressed: true);
2728

2829
private readonly IAsyncLocalValueMap m_localValues;
2930
private readonly IAsyncLocal[] m_localChangeNotifications;
@@ -63,15 +64,15 @@ private ExecutionContext ShallowClone(bool isFlowSuppressed)
6364
{
6465
Debug.Assert(isFlowSuppressed != m_isFlowSuppressed);
6566

66-
if (!isFlowSuppressed &&
67-
(m_localValues == null ||
68-
m_localValues.GetType() == typeof(AsyncLocalValueMap.EmptyAsyncLocalValueMap))
69-
)
67+
if (m_localValues == null ||
68+
m_localValues.GetType() == typeof(AsyncLocalValueMap.EmptyAsyncLocalValueMap))
7069
{
71-
return null; // implies the default context
70+
return isFlowSuppressed ?
71+
DefaultFlowSuppressed :
72+
null; // implies the default context
7273
}
73-
// Flow suppressing a Default context will have null values, set them to Empty
74-
return new ExecutionContext(m_localValues ?? AsyncLocalValueMap.Empty, m_localChangeNotifications ?? Array.Empty<IAsyncLocal>(), isFlowSuppressed);
74+
75+
return new ExecutionContext(m_localValues, m_localChangeNotifications, isFlowSuppressed);
7576
}
7677

7778
public static AsyncFlowControl SuppressFlow()

0 commit comments

Comments
 (0)