From 8a9f1eb120f9713689291da097faf56c0de01131 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 10 Feb 2022 14:07:12 -0800 Subject: [PATCH] Use `static readonly` for default `ExecutionOptions` 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. --- .../PowerShell/Execution/SynchronousDelegateTask.cs | 12 ++++++------ .../Execution/SynchronousPowerShellTask.cs | 4 +++- .../Services/PowerShell/Execution/SynchronousTask.cs | 3 +++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousDelegateTask.cs b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousDelegateTask.cs index 2bec67fe9..77bae18a3 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousDelegateTask.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousDelegateTask.cs @@ -1,10 +1,10 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using Microsoft.Extensions.Logging; -using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host; using System; using System.Threading; +using Microsoft.Extensions.Logging; +using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host; using SMA = System.Management.Automation; namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution @@ -23,7 +23,7 @@ public SynchronousDelegateTask( CancellationToken cancellationToken) : base(logger, cancellationToken) { - ExecutionOptions = executionOptions ?? new ExecutionOptions(); + ExecutionOptions = executionOptions ?? s_defaultExecutionOptions; _representation = representation; _action = action; } @@ -58,7 +58,7 @@ public SynchronousDelegateTask( { _func = func; _representation = representation; - ExecutionOptions = executionOptions ?? new ExecutionOptions(); + ExecutionOptions = executionOptions ?? s_defaultExecutionOptions; } public override ExecutionOptions ExecutionOptions { get; } @@ -94,7 +94,7 @@ public SynchronousPSDelegateTask( _psesHost = psesHost; _action = action; _representation = representation; - ExecutionOptions = executionOptions ?? new ExecutionOptions(); + ExecutionOptions = executionOptions ?? s_defaultExecutionOptions; } public override ExecutionOptions ExecutionOptions { get; } @@ -131,7 +131,7 @@ public SynchronousPSDelegateTask( _psesHost = psesHost; _func = func; _representation = representation; - ExecutionOptions = executionOptions ?? new ExecutionOptions(); + ExecutionOptions = executionOptions ?? s_defaultExecutionOptions; } public override ExecutionOptions ExecutionOptions { get; } diff --git a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs index c3a1132ad..f683c95a3 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs @@ -25,6 +25,8 @@ internal class SynchronousPowerShellTask : SynchronousTask