Skip to content

Commit 6c5bbaa

Browse files
committed
Optimise out some Linq operations within the logging code.
1 parent ae1ce07 commit 6c5bbaa

21 files changed

Lines changed: 22 additions & 10 deletions

src/PSADT/PSAppDeployToolkit/Foundation/ModuleDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ internal static SessionState GetSessionState()
167167
/// <param name="args">An array of arguments to pass to the script block. This parameter can be null or empty if no arguments are
168168
/// required.</param>
169169
/// <returns>A read-only collection of PSObject instances that represent the results of the script execution.</returns>
170-
internal static ReadOnlyCollection<PSObject> InvokeScript(ScriptBlock scriptBlock, params object[]? args)
170+
internal static ReadOnlyCollection<PSObject> InvokeScript(ScriptBlock scriptBlock, params object[] args)
171171
{
172172
SessionState sessionState = GetSessionState(); return new(sessionState.InvokeCommand.InvokeScript(sessionState, scriptBlock, args));
173173
}

src/PSADT/PSAppDeployToolkit/Logging/LogUtilities.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static IReadOnlyList<LogEntry> WriteLogEntry(IReadOnlyList<string> messag
8585
else
8686
{
8787
// Get the first PowerShell stack frame that contains a valid command.
88-
CallStackFrame invoker = ModuleDatabase.InvokeScript(ScriptBlock.Create("& $Script:CommandTable.'Get-PSCallStack'"), null).Skip(1).Select(static o => (CallStackFrame)o.BaseObject).First(static f => f.GetCommand() is string command && !string.IsNullOrWhiteSpace(command) && (!CallerCommandRegex.IsMatch(command) || (CallerScriptBlockRegex.IsMatch(command) && CallerScriptLocationRegex.IsMatch(f.GetScriptLocation()))));
88+
CallStackFrame invoker = ModuleDatabase.InvokeScript(ScriptBlock.Create("& $Script:CommandTable.'Get-PSCallStack'")).Skip(1).Select(static o => (CallStackFrame)o.BaseObject).First(static f => f.GetCommand() is string command && !string.IsNullOrWhiteSpace(command) && (!CallerCommandRegex.IsMatch(command) || (CallerScriptBlockRegex.IsMatch(command) && CallerScriptLocationRegex.IsMatch(f.GetScriptLocation()))));
8989
callerFileName = !string.IsNullOrWhiteSpace(invoker.ScriptName) ? invoker.ScriptName : invoker.GetScriptLocation();
9090
callerSource = invoker.GetCommand();
9191
}
@@ -126,9 +126,19 @@ public static IReadOnlyList<LogEntry> WriteLogEntry(IReadOnlyList<string> messag
126126
if (canLogToDisk)
127127
{
128128
using StreamWriter logFileWriter = new(Path.Combine(logFileDirectory!, logFileName!), true, LogEncoding);
129-
foreach (string line in logStyle.Value == LogStyle.CMTrace ? logEntries.Select(static e => e.CMTraceLogLine) : logEntries.Select(static e => e.LegacyLogLine))
129+
if (logStyle.Value == LogStyle.CMTrace)
130130
{
131-
logFileWriter.WriteLine(line);
131+
foreach (LogEntry logEntry in logEntries)
132+
{
133+
logFileWriter.WriteLine(logEntry.CMTraceLogLine);
134+
}
135+
}
136+
else
137+
{
138+
foreach (LogEntry logEntry in logEntries)
139+
{
140+
logFileWriter.WriteLine(logEntry.LegacyLogLine);
141+
}
132142
}
133143
}
134144

@@ -146,16 +156,16 @@ public static IReadOnlyList<LogEntry> WriteLogEntry(IReadOnlyList<string> messag
146156
}
147157
if (severity == LogSeverity.Error)
148158
{
149-
foreach (string line in logEntries.Select(static e => e.LegacyLogLine))
159+
foreach (LogEntry logEntry in logEntries)
150160
{
151-
Console.Error.WriteLine(line);
161+
Console.Error.WriteLine(logEntry.LegacyLogLine);
152162
}
153163
}
154164
else
155165
{
156-
foreach (string line in logEntries.Select(static e => e.LegacyLogLine))
166+
foreach (LogEntry logEntry in logEntries)
157167
{
158-
Console.WriteLine(line);
168+
Console.WriteLine(logEntry.LegacyLogLine);
159169
}
160170
}
161171
if (colouredOutput)
@@ -166,12 +176,14 @@ public static IReadOnlyList<LogEntry> WriteLogEntry(IReadOnlyList<string> messag
166176
else if (hostLogStreamType != HostLogStreamType.Verbose)
167177
{
168178
// Write the host output to PowerShell's InformationStream.
169-
_ = ModuleDatabase.InvokeScript(WriteHostDelegate, logEntries.Select(static e => e.LegacyLogLine), LogSeverityColors[(int)severity]);
179+
string[] infoMessages = [.. logEntries.Select(static e => e.LegacyLogLine)];
180+
_ = ModuleDatabase.InvokeScript(WriteHostDelegate, infoMessages, LogSeverityColors[(int)severity]);
170181
}
171182
else
172183
{
173184
// Write the host output to PowerShell's VerboseStream.
174-
_ = ModuleDatabase.InvokeScript(WriteVerboseDelegate, logEntries.Select(static e => e.Message));
185+
string[] verboseMessages = [.. logEntries.Select(static e => e.Message)];
186+
_ = ModuleDatabase.InvokeScript(WriteVerboseDelegate, verboseMessages);
175187
}
176188
}
177189
return logEntries;
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)