Skip to content

Commit 77d1ffc

Browse files
Replace superfluous Default member with default values (#1693)
I think this was a case of premature optimization. We're using C# records, which should be as cheap as an object as they come, especially when we can rely on the compiler for default values. Now we're passing fewer objects around too.
1 parent 5f54c43 commit 77d1ffc

File tree

9 files changed

+35
-53
lines changed

9 files changed

+35
-53
lines changed

src/PowerShellEditorServices/Services/Extension/ExtensionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ internal Task InitializeAsync()
107107
// Register the editor object in the runspace
108108
return ExecutionService.ExecuteDelegateAsync(
109109
$"Create ${PSEditorVariableName} object",
110-
ExecutionOptions.Default,
110+
executionOptions: null,
111111
(pwsh, _) => pwsh.Runspace.SessionStateProxy.PSVariable.Set(psEditor),
112112
CancellationToken.None);
113113
}

src/PowerShellEditorServices/Services/PowerShell/Console/LegacyReadLine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ public override string ReadLine(CancellationToken cancellationToken)
8484
.AddParameter("CursorColumn", currentCursorIndex)
8585
.AddParameter("Options", null);
8686

87-
currentCompletion = _psesHost.InvokePSCommand<CommandCompletion>(command, PowerShellExecutionOptions.Default, cancellationToken).FirstOrDefault();
87+
currentCompletion = _psesHost.InvokePSCommand<CommandCompletion>(command, executionOptions: null, cancellationToken).FirstOrDefault();
8888
}
8989
else
9090
{
9191
currentCompletion = _psesHost.InvokePSDelegate(
9292
"Legacy readline inline command completion",
93-
ExecutionOptions.Default,
94-
(pwsh, cancellationToken) => CommandCompletion.CompleteInput(inputAfterCompletion, currentCursorIndex, options: null, pwsh),
93+
executionOptions: null,
94+
(pwsh, _) => CommandCompletion.CompleteInput(inputAfterCompletion, currentCursorIndex, options: null, pwsh),
9595
cancellationToken);
9696

9797
if (currentCompletion.CompletionMatches.Count > 0)
@@ -198,7 +198,7 @@ public override string ReadLine(CancellationToken cancellationToken)
198198
PSCommand command = new PSCommand()
199199
.AddCommand("Get-History");
200200

201-
currentHistory = _psesHost.InvokePSCommand<PSObject>(command, PowerShellExecutionOptions.Default, cancellationToken);
201+
currentHistory = _psesHost.InvokePSCommand<PSObject>(command, executionOptions: null, cancellationToken);
202202

203203
if (currentHistory != null)
204204
{

src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static Task<DscBreakpointCapability> GetDscCapabilityAsync(
164164

165165
return psesHost.ExecuteDelegateAsync<DscBreakpointCapability>(
166166
nameof(getDscBreakpointCapabilityFunc),
167-
ExecutionOptions.Default,
167+
executionOptions: null,
168168
getDscBreakpointCapabilityFunc,
169169
cancellationToken);
170170
}

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,16 @@ public enum ExecutionPriority
1515
// Generally the executor will do the right thing though; some options just priority over others.
1616
public record ExecutionOptions
1717
{
18-
public static ExecutionOptions Default = new()
19-
{
20-
Priority = ExecutionPriority.Normal,
21-
MustRunInForeground = false,
22-
InterruptCurrentForeground = false,
23-
};
24-
25-
public ExecutionPriority Priority { get; init; }
26-
18+
public ExecutionPriority Priority { get; init; } = ExecutionPriority.Normal;
2719
public bool MustRunInForeground { get; init; }
28-
2920
public bool InterruptCurrentForeground { get; init; }
3021
}
3122

3223
public record PowerShellExecutionOptions : ExecutionOptions
3324
{
34-
public static new PowerShellExecutionOptions Default = new()
35-
{
36-
Priority = ExecutionPriority.Normal,
37-
MustRunInForeground = false,
38-
InterruptCurrentForeground = false,
39-
WriteOutputToHost = false,
40-
WriteInputToHost = false,
41-
ThrowOnError = true,
42-
AddToHistory = false,
43-
};
44-
4525
public bool WriteOutputToHost { get; init; }
46-
4726
public bool WriteInputToHost { get; init; }
48-
49-
public bool ThrowOnError { get; init; }
50-
27+
public bool ThrowOnError { get; init; } = true;
5128
public bool AddToHistory { get; init; }
5229
}
5330
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public SynchronousDelegateTask(
2323
CancellationToken cancellationToken)
2424
: base(logger, cancellationToken)
2525
{
26-
ExecutionOptions = executionOptions;
26+
ExecutionOptions = executionOptions ?? new ExecutionOptions();
2727
_representation = representation;
2828
_action = action;
2929
}
@@ -58,7 +58,7 @@ public SynchronousDelegateTask(
5858
{
5959
_func = func;
6060
_representation = representation;
61-
ExecutionOptions = executionOptions;
61+
ExecutionOptions = executionOptions ?? new ExecutionOptions();
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;
97+
ExecutionOptions = executionOptions ?? new ExecutionOptions();
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;
134+
ExecutionOptions = executionOptions ?? new ExecutionOptions();
135135
}
136136

137137
public override ExecutionOptions ExecutionOptions { get; }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public SynchronousPowerShellTask(
3636
_logger = logger;
3737
_psesHost = psesHost;
3838
_psCommand = command;
39-
PowerShellExecutionOptions = executionOptions;
39+
PowerShellExecutionOptions = executionOptions ?? new PowerShellExecutionOptions();
4040
}
4141

4242
public PowerShellExecutionOptions PowerShellExecutionOptions { get; }

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ public Task<TResult> ExecuteDelegateAsync<TResult>(
307307
Func<PowerShell, CancellationToken, TResult> func,
308308
CancellationToken cancellationToken)
309309
{
310-
return InvokeTaskOnPipelineThreadAsync(new SynchronousPSDelegateTask<TResult>(_logger, this, representation, executionOptions ?? ExecutionOptions.Default, func, cancellationToken));
310+
return InvokeTaskOnPipelineThreadAsync(
311+
new SynchronousPSDelegateTask<TResult>(_logger, this, representation, executionOptions, func, cancellationToken));
311312
}
312313

313314
public Task ExecuteDelegateAsync(
@@ -316,7 +317,8 @@ public Task ExecuteDelegateAsync(
316317
Action<PowerShell, CancellationToken> action,
317318
CancellationToken cancellationToken)
318319
{
319-
return InvokeTaskOnPipelineThreadAsync(new SynchronousPSDelegateTask(_logger, this, representation, executionOptions ?? ExecutionOptions.Default, action, cancellationToken));
320+
return InvokeTaskOnPipelineThreadAsync(
321+
new SynchronousPSDelegateTask(_logger, this, representation, executionOptions, action, cancellationToken));
320322
}
321323

322324
public Task<TResult> ExecuteDelegateAsync<TResult>(
@@ -325,7 +327,8 @@ public Task<TResult> ExecuteDelegateAsync<TResult>(
325327
Func<CancellationToken, TResult> func,
326328
CancellationToken cancellationToken)
327329
{
328-
return InvokeTaskOnPipelineThreadAsync(new SynchronousDelegateTask<TResult>(_logger, representation, executionOptions ?? ExecutionOptions.Default, func, cancellationToken));
330+
return InvokeTaskOnPipelineThreadAsync(
331+
new SynchronousDelegateTask<TResult>(_logger, representation, executionOptions, func, cancellationToken));
329332
}
330333

331334
public Task ExecuteDelegateAsync(
@@ -334,26 +337,26 @@ public Task ExecuteDelegateAsync(
334337
Action<CancellationToken> action,
335338
CancellationToken cancellationToken)
336339
{
337-
return InvokeTaskOnPipelineThreadAsync(new SynchronousDelegateTask(_logger, representation, executionOptions ?? ExecutionOptions.Default, action, cancellationToken));
340+
return InvokeTaskOnPipelineThreadAsync(
341+
new SynchronousDelegateTask(_logger, representation, executionOptions, action, cancellationToken));
338342
}
339343

340344
public Task<IReadOnlyList<TResult>> ExecutePSCommandAsync<TResult>(
341345
PSCommand psCommand,
342346
CancellationToken cancellationToken,
343347
PowerShellExecutionOptions executionOptions = null)
344348
{
345-
return InvokeTaskOnPipelineThreadAsync(new SynchronousPowerShellTask<TResult>(
346-
_logger,
347-
this,
348-
psCommand,
349-
executionOptions ?? PowerShellExecutionOptions.Default,
350-
cancellationToken));
349+
return InvokeTaskOnPipelineThreadAsync(
350+
new SynchronousPowerShellTask<TResult>(_logger, this, psCommand, executionOptions, cancellationToken));
351351
}
352352

353353
public Task ExecutePSCommandAsync(
354354
PSCommand psCommand,
355355
CancellationToken cancellationToken,
356-
PowerShellExecutionOptions executionOptions = null) => ExecutePSCommandAsync<PSObject>(psCommand, cancellationToken, executionOptions);
356+
PowerShellExecutionOptions executionOptions = null)
357+
{
358+
return ExecutePSCommandAsync<PSObject>(psCommand, cancellationToken, executionOptions);
359+
}
357360

358361
public TResult InvokeDelegate<TResult>(string representation, ExecutionOptions executionOptions, Func<CancellationToken, TResult> func, CancellationToken cancellationToken)
359362
{
@@ -374,7 +377,9 @@ public IReadOnlyList<TResult> InvokePSCommand<TResult>(PSCommand psCommand, Powe
374377
}
375378

376379
public void InvokePSCommand(PSCommand psCommand, PowerShellExecutionOptions executionOptions, CancellationToken cancellationToken)
377-
=> InvokePSCommand<PSObject>(psCommand, executionOptions, cancellationToken);
380+
{
381+
InvokePSCommand<PSObject>(psCommand, executionOptions, cancellationToken);
382+
}
378383

379384
public TResult InvokePSDelegate<TResult>(string representation, ExecutionOptions executionOptions, Func<PowerShell, CancellationToken, TResult> func, CancellationToken cancellationToken)
380385
{
@@ -662,7 +667,7 @@ private void DoOneRepl(CancellationToken cancellationToken)
662667
private string GetPrompt(CancellationToken cancellationToken)
663668
{
664669
var command = new PSCommand().AddCommand("prompt");
665-
IReadOnlyList<string> results = InvokePSCommand<string>(command, PowerShellExecutionOptions.Default, cancellationToken);
670+
IReadOnlyList<string> results = InvokePSCommand<string>(command, executionOptions: null, cancellationToken);
666671
string prompt = results.Count > 0 ? results[0] : DefaultPrompt;
667672

668673
if (CurrentRunspace.RunspaceOrigin != RunspaceOrigin.Local)
@@ -846,7 +851,7 @@ private void OnPowerShellIdle(CancellationToken idleCancellationToken)
846851
// to force event processing
847852
if (runPipelineForEventProcessing)
848853
{
849-
InvokePSCommand(new PSCommand().AddScript("0", useLocalScope: true), PowerShellExecutionOptions.Default, CancellationToken.None);
854+
InvokePSCommand(new PSCommand().AddScript("0", useLocalScope: true), executionOptions: null, CancellationToken.None);
850855
}
851856
}
852857

src/PowerShellEditorServices/Services/PowerShell/Utility/CommandHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static async Task<string> GetCommandSynopsisAsync(
183183

184184
IEnumerable<CommandInfo> aliases = await executionService.ExecuteDelegateAsync<IEnumerable<CommandInfo>>(
185185
nameof(GetAliasesAsync),
186-
Execution.ExecutionOptions.Default,
186+
executionOptions: null,
187187
(pwsh, _) =>
188188
{
189189
CommandInvocationIntrinsics invokeCommand = pwsh.Runspace.SessionStateProxy.InvokeCommand;

src/PowerShellEditorServices/Services/Workspace/RemoteFileManagerService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,8 @@ private async void HandlePSEventReceivedAsync(object sender, PSEventArgs args)
657657
private Task RegisterPSEditFunctionAsync()
658658
=> _executionService.ExecuteDelegateAsync(
659659
"Register psedit function",
660-
ExecutionOptions.Default,
661-
(pwsh, cancellationToken) => RegisterPSEditFunction(pwsh.Runspace),
660+
executionOptions: null,
661+
(pwsh, _) => RegisterPSEditFunction(pwsh.Runspace),
662662
CancellationToken.None);
663663

664664
private void RegisterPSEditFunction(Runspace runspace)

0 commit comments

Comments
 (0)