Skip to content

Commit 6754359

Browse files
Use static readonly for default ExecutionOptions (#1703)
But at the `SynchronousTask` level. Now we get the best of both worlds: the records have the correct default values on initialization, and we only heap allocate a single static instance of `ExecutionOptions` for the default case.
1 parent 3ad0f39 commit 6754359

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousDelegateTask.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Microsoft.Extensions.Logging;
5-
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
64
using System;
75
using System.Threading;
6+
using Microsoft.Extensions.Logging;
7+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
88
using SMA = System.Management.Automation;
99

1010
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution
@@ -23,7 +23,7 @@ public SynchronousDelegateTask(
2323
CancellationToken cancellationToken)
2424
: base(logger, cancellationToken)
2525
{
26-
ExecutionOptions = executionOptions ?? new ExecutionOptions();
26+
ExecutionOptions = executionOptions ?? s_defaultExecutionOptions;
2727
_representation = representation;
2828
_action = action;
2929
}
@@ -58,7 +58,7 @@ public SynchronousDelegateTask(
5858
{
5959
_func = func;
6060
_representation = representation;
61-
ExecutionOptions = executionOptions ?? new ExecutionOptions();
61+
ExecutionOptions = executionOptions ?? s_defaultExecutionOptions;
6262
}
6363

6464
public override ExecutionOptions ExecutionOptions { get; }
@@ -94,7 +94,7 @@ public SynchronousPSDelegateTask(
9494
_psesHost = psesHost;
9595
_action = action;
9696
_representation = representation;
97-
ExecutionOptions = executionOptions ?? new ExecutionOptions();
97+
ExecutionOptions = executionOptions ?? s_defaultExecutionOptions;
9898
}
9999

100100
public override ExecutionOptions ExecutionOptions { get; }
@@ -131,7 +131,7 @@ public SynchronousPSDelegateTask(
131131
_psesHost = psesHost;
132132
_func = func;
133133
_representation = representation;
134-
ExecutionOptions = executionOptions ?? new ExecutionOptions();
134+
ExecutionOptions = executionOptions ?? s_defaultExecutionOptions;
135135
}
136136

137137
public override ExecutionOptions ExecutionOptions { get; }

src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal class SynchronousPowerShellTask<TResult> : SynchronousTask<IReadOnlyLis
2525

2626
private SMA.PowerShell _pwsh;
2727

28+
private readonly static PowerShellExecutionOptions s_defaultPowerShellExecutionOptions = new();
29+
2830
public SynchronousPowerShellTask(
2931
ILogger logger,
3032
PsesInternalHost psesHost,
@@ -36,7 +38,7 @@ public SynchronousPowerShellTask(
3638
_logger = logger;
3739
_psesHost = psesHost;
3840
_psCommand = command;
39-
PowerShellExecutionOptions = executionOptions ?? new PowerShellExecutionOptions();
41+
PowerShellExecutionOptions = executionOptions ?? s_defaultPowerShellExecutionOptions;
4042
}
4143

4244
public PowerShellExecutionOptions PowerShellExecutionOptions { get; }

src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousTask.cs

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public TResult Result
6565

6666
public abstract ExecutionOptions ExecutionOptions { get; }
6767

68+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "RCS1158", Justification = "Field is not type-dependent")]
69+
internal static readonly ExecutionOptions s_defaultExecutionOptions = new();
70+
6871
public abstract TResult Run(CancellationToken cancellationToken);
6972

7073
public abstract override string ToString();

0 commit comments

Comments
 (0)