From 0043f309d4a7a59b031cee722acc81e22379a58a Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 15 May 2018 08:37:01 +0500 Subject: [PATCH 01/18] [Feature] Exclude extra allocations in ConsoleControl --- .../host/msh/ConsoleControl.cs | 89 +++++--- .../host/msh/ConsoleHostRawUserInterface.cs | 4 +- .../host/msh/ConsoleHostTranscript.cs | 37 ++-- .../host/msh/ConsoleHostUserInterface.cs | 194 ++++++++++++++---- .../msh/ConsoleHostUserInterfaceProgress.cs | 4 +- .../msh/ConsoleHostUserInterfacePrompt.cs | 3 +- ...ConsoleHostUserInterfacePromptForChoice.cs | 6 +- .../msh/ConsoleHostUserInterfaceSecurity.cs | 3 +- .../host/msh/ConsoleTextWriter.cs | 32 ++- 9 files changed, 280 insertions(+), 92 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index 0a0775a18a5..6698603799c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -2548,16 +2548,28 @@ internal static void SetConsoleWindowTitle(string consoleTitle) /// string that is written /// /// + /// + /// + /// New line is written + /// + /// + /// /// if the Win32's WriteConsole fails /// - - internal static void WriteConsole(ConsoleHandle consoleHandle, string output) + internal static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan output, bool newLine) { Dbg.Assert(!consoleHandle.IsInvalid, "ConsoleHandle is not valid"); Dbg.Assert(!consoleHandle.IsClosed, "ConsoleHandle is closed"); - if (string.IsNullOrEmpty(output)) + if (output.Length == 0) + { + if (newLine) + { + WriteConsole(consoleHandle, ConsoleHostUserInterface.Crlf.AsSpan()); + } + return; + } // Native WriteConsole doesn't support output buffer longer than 64K. // We need to chop the output string if it is too long. @@ -2567,36 +2579,52 @@ internal static void WriteConsole(ConsoleHandle consoleHandle, string output) while (cursor < output.Length) { - string outBuffer; + ReadOnlySpan outBuffer; if (cursor + maxBufferSize < output.Length) { - outBuffer = output.Substring(cursor, maxBufferSize); + outBuffer = output.Slice(cursor, maxBufferSize); cursor += maxBufferSize; } else { - outBuffer = output.Substring(cursor); + outBuffer = output.Slice(cursor); cursor = output.Length; } - DWORD charsWritten; - bool result = - NativeMethods.WriteConsole( - consoleHandle.DangerousGetHandle(), - outBuffer, - (DWORD)outBuffer.Length, - out charsWritten, - IntPtr.Zero); - - if (result == false) + if (newLine) { - int err = Marshal.GetLastWin32Error(); - - HostException e = CreateHostException(err, "WriteConsole", - ErrorCategory.WriteError, ConsoleControlStrings.WriteConsoleExceptionTemplate); - throw e; + Span outBufferLine = stackalloc char[outBuffer.Length + 2]; + outBuffer.CopyTo(outBufferLine); + ConsoleHostUserInterface.Crlf.AsSpan().CopyTo(outBufferLine.Slice(outBufferLine.Length-2)); + WriteConsole(consoleHandle, outBufferLine); } + else + { + WriteConsole(consoleHandle, outBuffer); + } + + } + } + + private static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan buffer) + { + DWORD charsWritten; + bool result = + NativeMethods.WriteConsole( + consoleHandle.DangerousGetHandle(), + buffer, + (DWORD)buffer.Length, + out charsWritten, + IntPtr.Zero); + + if (result == false) + { + int err = Marshal.GetLastWin32Error(); + + HostException e = CreateHostException(err, "WriteConsole", + ErrorCategory.WriteError, ConsoleControlStrings.WriteConsoleExceptionTemplate); + throw e; } } @@ -3009,15 +3037,30 @@ out DWORD numberOfCharsWritten [DllImport(PinvokeDllNames.WriteConsoleDllName, SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool WriteConsole + private static extern unsafe bool WriteConsole ( NakedWin32Handle consoleOutput, - string buffer, + char* buffer, DWORD numberOfCharsToWrite, out DWORD numberOfCharsWritten, IntPtr reserved ); + internal static unsafe bool WriteConsole + ( + NakedWin32Handle consoleOutput, + ReadOnlySpan buffer, + DWORD numberOfCharsToWrite, + out DWORD numberOfCharsWritten, + IntPtr reserved + ) + { + fixed (char* bufferPtr = &MemoryMarshal.GetReference(buffer)) + { + return WriteConsole(consoleOutput, bufferPtr, numberOfCharsToWrite, out numberOfCharsWritten, reserved); + } + } + [DllImport(PinvokeDllNames.GetConsoleTitleDllName, SetLastError = true, CharSet = CharSet.Unicode)] internal static extern DWORD GetConsoleTitle(StringBuilder consoleTitle, DWORD size); diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs index 9026739dbeb..5f1c63af9c0 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs @@ -705,9 +705,7 @@ public override if ((options & ReadKeyOptions.NoEcho) == 0) { - parent.WriteToConsole( - keyInfo.Character.ToString(), - true); + parent.WriteToConsole(keyInfo.Character, transcribeResult: true); } return keyInfo; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostTranscript.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostTranscript.cs index f551f6bb41f..8f052004160 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostTranscript.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostTranscript.cs @@ -10,15 +10,9 @@ namespace Microsoft.PowerShell { - internal sealed partial - class ConsoleHost - : - PSHost, - IDisposable + internal sealed partial class ConsoleHost : PSHost, IDisposable { - internal - bool - IsTranscribing + internal bool IsTranscribing { get { @@ -69,9 +63,7 @@ internal void StartTranscribing(string transcriptFilename, bool shouldAppend) */ private string _transcriptFileName = string.Empty; - internal - string - StopTranscribing() + internal string StopTranscribing() { lock (_transcriptionStateLock) { @@ -106,15 +98,30 @@ internal void StartTranscribing(string transcriptFilename, bool shouldAppend) } } - internal - void - WriteToTranscript(string text) + internal void WriteToTranscript(ReadOnlySpan text) + { + WriteToTranscript(text, newLine: false); + } + + internal void WriteLineToTranscript(ReadOnlySpan text) + { + WriteToTranscript(text, newLine: true); + } + + private void WriteToTranscript(ReadOnlySpan text, bool newLine) { lock (_transcriptionStateLock) { if (_isTranscribing && _transcriptionWriter != null) { - _transcriptionWriter.Write(text); + if (newLine) + { + _transcriptionWriter.WriteLine(text); + } + else + { + _transcriptionWriter.Write(text); + } } } } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index bc8fa54f8a3..f7196686c93 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -542,7 +542,24 @@ private static bool shouldUnsetMode( #region WriteToConsole - internal void WriteToConsole(string value, bool transcribeResult) + internal void WriteToConsole(char c, bool transcribeResult) + { + Span value = stackalloc char[1]; + value[0] = c; + WriteToConsole(value, transcribeResult); + } + + internal void WriteToConsole(ReadOnlySpan value, bool transcribeResult) + { + WriteToConsole(value, transcribeResult, newLine: false); + } + + internal void WriteLineToConsole(ReadOnlySpan value, bool transcribeResult) + { + WriteToConsole(value, transcribeResult, newLine: true); + } + + private void WriteToConsole(ReadOnlySpan value, bool transcribeResult, bool newLine) { #if !UNIX ConsoleHandle handle = ConsoleControl.GetActiveScreenBufferHandle(); @@ -568,14 +585,14 @@ internal void WriteToConsole(string value, bool transcribeResult) // This is atomic, so we don't lock here... #if !UNIX - ConsoleControl.WriteConsole(handle, value); + ConsoleControl.WriteConsole(handle, value, newLine); #else - Console.Out.Write(value); + ConsoleOutWriteHelper(value, newLine); #endif if (_isInteractiveTestToolListening && Console.IsOutputRedirected) { - Console.Out.Write(value); + ConsoleOutWriteHelper(value, newLine); } if (transcribeResult) @@ -588,34 +605,61 @@ internal void WriteToConsole(string value, bool transcribeResult) } } - private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text) + private void ConsoleOutWriteHelper(ReadOnlySpan value, bool newLine) { - ConsoleColor fg = RawUI.ForegroundColor; - ConsoleColor bg = RawUI.BackgroundColor; - - RawUI.ForegroundColor = foregroundColor; - RawUI.BackgroundColor = backgroundColor; - - try + if (newLine) { - WriteToConsole(text, true); + Console.Out.WriteLine(value); } - finally + else { - RawUI.ForegroundColor = fg; - RawUI.BackgroundColor = bg; + Console.Out.Write(value); + } + } + + private void WriteLineToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text) + { + WriteToConsole(PromptColor, backgroundColor, text, newLine: true); + } + + private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text, bool newLine = false) + { + // Sync access so that we don't race on color settings if called from multiple threads. + lock (_instanceLock) + { + ConsoleColor fg = RawUI.ForegroundColor; + ConsoleColor bg = RawUI.BackgroundColor; + + RawUI.ForegroundColor = foregroundColor; + RawUI.BackgroundColor = backgroundColor; + + try + { + if (newLine) + { + WriteLineToConsole(text, true); + } + else + { + WriteToConsole(text, true); + } + } + finally + { + RawUI.ForegroundColor = fg; + RawUI.BackgroundColor = bg; + } } } private void WriteLineToConsole(string text) { - WriteToConsole(text, true); - WriteToConsole(Crlf, true); + WriteLineToConsole(text, true); } private void WriteLineToConsole() { - WriteToConsole(Crlf, true); + WriteLineToConsole("", true); } #endregion WriteToConsole @@ -636,15 +680,28 @@ private void WriteLineToConsole() public override void Write(string value) { - if (string.IsNullOrEmpty(value)) - { - // do nothing + WriteHelper(value, newLine: false); + } + private void WriteHelper(string value, bool newLine) + { + if (string.IsNullOrEmpty(value) && !newLine) + { return; } // If the test hook is set, write to it and continue. - if (s_h != null) s_h.Write(value); + if (s_h != null) + { + if (newLine) + { + s_h.WriteLine(value); + } + else + { + s_h.Write(value); + } + } TextWriter writer = Console.IsOutputRedirected ? Console.Out : _parent.ConsoleTextWriter; @@ -653,10 +710,22 @@ public override void Write(string value) Dbg.Assert(writer == _parent.OutputSerializer.textWriter, "writers should be the same"); _parent.OutputSerializer.Serialize(value); + + if (newLine) + { + _parent.OutputSerializer.Serialize(Crlf); + } } else { - writer.Write(value); + if (newLine) + { + writer.WriteLine(value); + } + else + { + writer.Write(value); + } } } @@ -682,8 +751,40 @@ public override void Write(string value) public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) { - // Sync access so that we don't race on color settings if called from multiple threads. + Write(foregroundColor, backgroundColor, value, newLine: false); + } + + /// + /// + /// See base class + /// + /// + /// + /// + /// + /// + /// + /// If obtaining information about the buffer failed + /// OR + /// Win32's SetConsoleTextAttribute + /// OR + /// Win32's CreateFile fails + /// OR + /// Win32's GetConsoleMode fails + /// OR + /// Win32's SetConsoleMode fails + /// OR + /// Win32's WriteConsole fails + /// + /// + public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) + { + Write(foregroundColor, backgroundColor, value, newLine: true); + } + private void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value, bool newLine) + { + // Sync access so that we don't race on color settings if called from multiple threads. lock (_instanceLock) { ConsoleColor fg = RawUI.ForegroundColor; @@ -694,7 +795,14 @@ public override void Write(ConsoleColor foregroundColor, ConsoleColor background try { - this.Write(value); + if (newLine) + { + this.WriteLine(value); + } + else + { + this.Write(value); + } } finally { @@ -717,16 +825,30 @@ public override void Write(ConsoleColor foregroundColor, ConsoleColor background /// OR /// Win32's WriteConsole fails /// - public override void WriteLine(string value) { - // lock here so that the newline is written atomically with the value + this.WriteHelper(value, newLine: true); + } - lock (_instanceLock) - { - this.Write(value); - this.Write(Crlf); - } + /// + /// + /// See base class + /// + /// + /// + /// + /// Win32's CreateFile fails + /// OR + /// Win32's GetConsoleMode fails + /// OR + /// Win32's SetConsoleMode fails + /// OR + /// Win32's WriteConsole fails + /// + /// + public override void WriteLine() + { + this.WriteHelper("", newLine: true); } #region Word Wrapping @@ -1227,8 +1349,6 @@ public override void WriteErrorLine(string value) { if (string.IsNullOrEmpty(value)) { - // do nothing - return; } @@ -1247,7 +1367,7 @@ public override void WriteErrorLine(string value) if (writer == _parent.ConsoleTextWriter) WriteLine(ErrorForegroundColor, ErrorBackgroundColor, value); else - Console.Error.Write(value + Crlf); + Console.Error.WriteLine(value); } } @@ -1941,7 +2061,7 @@ internal string ReadLineWithTabCompletion(Executor exec) { // Reads always terminate with the enter key, so add that. - _parent.WriteToTranscript(input + Crlf); + _parent.WriteLineToTranscript(input); } return input; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs index d6308575153..9b3c7abbcd0 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs @@ -130,7 +130,7 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt private void - PostWrite(string value) + PostWrite(ReadOnlySpan value) { PostWrite(); @@ -178,7 +178,7 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt try { // Reads always terminate with the enter key, so add that. - _parent.WriteToTranscript(value + Crlf); + _parent.WriteLineToTranscript(value); } catch (Exception) { diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePrompt.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePrompt.cs index b82e09a8abf..88eaab50cbf 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePrompt.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePrompt.cs @@ -118,8 +118,7 @@ public override // Should be a skin lookup WriteLineToConsole(); - WriteToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption)); - WriteLineToConsole(); + WriteLineToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption)); } if (!string.IsNullOrEmpty(message)) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs index 3f10679b996..fd2fc1db935 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs @@ -70,8 +70,7 @@ public override int PromptForChoice(string caption, string message, Collection PromptForChoice(string caption, { // Should be a skin lookup WriteLineToConsole(); - WriteToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption)); - WriteLineToConsole(); + WriteLineToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption)); } // write message if (!string.IsNullOrEmpty(message)) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs index ee44fe39d2b..18e5ce20f81 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs @@ -72,8 +72,7 @@ public override PSCredential PromptForCredential( // Should be a skin lookup WriteLineToConsole(); - WriteToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption)); - WriteLineToConsole(); + WriteLineToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption)); } if (!string.IsNullOrEmpty(message)) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs index 78f4389701b..ccd287c0f21 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs @@ -42,32 +42,56 @@ public override _ui.WriteToConsole(value, true); } + public override + void + Write(ReadOnlySpan value) + { + _ui.WriteToConsole(value, true); + } + public override void WriteLine(string value) { - this.Write(value + ConsoleHostUserInterface.Crlf); + _ui.WriteLineToConsole(value, true); + } + + public override + void + WriteLine(ReadOnlySpan value) + { + _ui.WriteLineToConsole(value, true); } public override void Write(Boolean b) { - this.Write(b.ToString()); + if (b) + { + _ui.WriteToConsole(Boolean.TrueString.AsSpan(), true); + } + else + { + _ui.WriteToConsole(Boolean.FalseString.AsSpan(), true); + } + } public override void Write(char c) { - this.Write(new String(c, 1)); + Span c1 = stackalloc char[1]; + c1[0] = c; + _ui.WriteToConsole(c1, true); } public override void Write(char[] a) { - this.Write(new String(a)); + _ui.WriteToConsole(a, true); } private ConsoleHostUserInterface _ui; From fdd4e8d43045181a6111793c496e30cd5761d1b2 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 6 Jun 2018 14:05:45 +0500 Subject: [PATCH 02/18] Fix param section --- src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index 6698603799c..517c883ffb2 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -2547,12 +2547,12 @@ internal static void SetConsoleWindowTitle(string consoleTitle) /// /// string that is written /// - /// /// /// /// New line is written /// /// + /// /// /// if the Win32's WriteConsole fails /// From c8e106cb1e28d9fa37f8e726a8778cabd129fdd4 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 6 Jun 2018 16:28:14 +0500 Subject: [PATCH 03/18] Minor style corrections --- .../host/msh/ConsoleControl.cs | 7 +++++-- .../host/msh/ConsoleHostUserInterface.cs | 4 ++-- .../host/msh/ConsoleTextWriter.cs | 19 +++++++++---------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index 517c883ffb2..328175c405b 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -2622,8 +2622,11 @@ private static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan { int err = Marshal.GetLastWin32Error(); - HostException e = CreateHostException(err, "WriteConsole", - ErrorCategory.WriteError, ConsoleControlStrings.WriteConsoleExceptionTemplate); + HostException e = CreateHostException( + err, + "WriteConsole", + ErrorCategory.WriteError, + ConsoleControlStrings.WriteConsoleExceptionTemplate); throw e; } } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index f7196686c93..150e828f4ae 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -654,12 +654,12 @@ private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroun private void WriteLineToConsole(string text) { - WriteLineToConsole(text, true); + WriteLineToConsole(text, transcribeResult: true); } private void WriteLineToConsole() { - WriteLineToConsole("", true); + WriteLineToConsole(string.Empty, transcribeResult: true); } #endregion WriteToConsole diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs index ccd287c0f21..ce93aa2f74e 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs @@ -39,28 +39,28 @@ public override void Write(string value) { - _ui.WriteToConsole(value, true); + _ui.WriteToConsole(value, transcribeResult: true); } public override void Write(ReadOnlySpan value) { - _ui.WriteToConsole(value, true); + _ui.WriteToConsole(value, transcribeResult: true); } public override void WriteLine(string value) { - _ui.WriteLineToConsole(value, true); + _ui.WriteLineToConsole(value, transcribeResult: true); } public override void WriteLine(ReadOnlySpan value) { - _ui.WriteLineToConsole(value, true); + _ui.WriteLineToConsole(value, transcribeResult: true); } public override @@ -69,11 +69,11 @@ public override { if (b) { - _ui.WriteToConsole(Boolean.TrueString.AsSpan(), true); + _ui.WriteToConsole(Boolean.TrueString.AsSpan(), transcribeResult: true); } else { - _ui.WriteToConsole(Boolean.FalseString.AsSpan(), true); + _ui.WriteToConsole(Boolean.FalseString.AsSpan(), transcribeResult: true); } } @@ -84,17 +84,16 @@ public override { Span c1 = stackalloc char[1]; c1[0] = c; - _ui.WriteToConsole(c1, true); + _ui.WriteToConsole(c1, transcribeResult: true); } public override void Write(char[] a) { - _ui.WriteToConsole(a, true); + _ui.WriteToConsole(a, transcribeResult: true); } private ConsoleHostUserInterface _ui; } -} // namespace - +} From ded4dd7902d0ea3a0db08723bbc22ac1ad074df6 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 10 Jan 2019 09:45:59 +0500 Subject: [PATCH 04/18] Fix doc comments --- .../host/msh/ConsoleControl.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index 328175c405b..97211e4d1d6 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -2542,19 +2542,16 @@ internal static void SetConsoleWindowTitle(string consoleTitle) /// Wrap Win32 WriteConsole. /// /// - /// handle for the console where the string is written + /// Handle for the console where the string is written. /// /// - /// string that is written + /// String that is written. /// /// - /// - /// New line is written - /// + /// New line is written. /// /// - /// - /// if the Win32's WriteConsole fails + /// If the Win32's WriteConsole fails. /// internal static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan output, bool newLine) { From a6be5b13a181cd5e618b7c16ae0a98b81f890572 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 10 Jan 2019 09:58:34 +0500 Subject: [PATCH 05/18] Address feedback --- .../host/msh/ConsoleControl.cs | 6 ++++-- .../host/msh/ConsoleHostUserInterface.cs | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index 97211e4d1d6..0e31c61d973 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -2591,9 +2591,11 @@ internal static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan outBufferLine = stackalloc char[outBuffer.Length + 2]; + var endOfLine = ConsoleHostUserInterface.Crlf.AsSpan(); + var endOfLineLength = endOfLine.Length; + Span outBufferLine = stackalloc char[outBuffer.Length + endOfLineLength]; outBuffer.CopyTo(outBufferLine); - ConsoleHostUserInterface.Crlf.AsSpan().CopyTo(outBufferLine.Slice(outBufferLine.Length-2)); + endOfLine.CopyTo(outBufferLine.Slice(outBufferLine.Length - endOfLineLength)); WriteConsole(consoleHandle, outBufferLine); } else diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 150e828f4ae..1558f1afcbb 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -637,11 +637,11 @@ private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroun { if (newLine) { - WriteLineToConsole(text, true); + WriteLineToConsole(text, transcribeResult: true); } else { - WriteToConsole(text, true); + WriteToConsole(text, transcribeResult: true); } } finally @@ -848,7 +848,7 @@ public override void WriteLine(string value) /// public override void WriteLine() { - this.WriteHelper("", newLine: true); + this.WriteHelper(string.Empty, newLine: true); } #region Word Wrapping From 92503569f1b365797ed9590cd662ff4c09976be9 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 10 Jan 2019 10:15:03 +0500 Subject: [PATCH 06/18] Style fix 1 --- .../host/msh/ConsoleControl.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index 0e31c61d973..63751b596b8 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -2570,18 +2570,17 @@ internal static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan outBuffer; - if (cursor + maxBufferSize < output.Length) + if (cursor + MaxBufferSize < output.Length) { - outBuffer = output.Slice(cursor, maxBufferSize); - cursor += maxBufferSize; + outBuffer = output.Slice(cursor, MaxBufferSize); + cursor += MaxBufferSize; } else { @@ -2602,7 +2601,6 @@ internal static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan Date: Thu, 10 Jan 2019 10:15:20 +0500 Subject: [PATCH 07/18] Style fix 2 --- .../host/msh/ConsoleHostUserInterface.cs | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 1558f1afcbb..f21ebb429fc 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -268,7 +268,7 @@ private object ReadLineSafe(bool isSecureString, char? printToken) #else // Ensure that we're in the proper line-input mode. - ConsoleControl.ConsoleModes desiredMode = + const ConsoleControl.ConsoleModes DesiredMode = ConsoleControl.ConsoleModes.Extended | ConsoleControl.ConsoleModes.QuickEdit; @@ -278,13 +278,13 @@ private object ReadLineSafe(bool isSecureString, char? printToken) bool shouldUnsetMouseInput = shouldUnsetMode(ConsoleControl.ConsoleModes.MouseInput, ref m); bool shouldUnsetProcessInput = shouldUnsetMode(ConsoleControl.ConsoleModes.ProcessedInput, ref m); - if ((m & desiredMode) != desiredMode || + if ((m & DesiredMode) != DesiredMode || shouldUnsetMouseInput || shouldUnsetEchoInput || shouldUnsetLineInput || shouldUnsetProcessInput) { - m |= desiredMode; + m |= DesiredMode; ConsoleControl.SetMode(handle, m); } else @@ -566,16 +566,15 @@ private void WriteToConsole(ReadOnlySpan value, bool transcribeResult, boo // Ensure that we're in the proper line-output mode. We don't lock here as it does not matter if we // attempt to set the mode from multiple threads at once. - ConsoleControl.ConsoleModes m = ConsoleControl.GetMode(handle); - const ConsoleControl.ConsoleModes desiredMode = - ConsoleControl.ConsoleModes.ProcessedOutput + const ConsoleControl.ConsoleModes DesiredMode = + ConsoleControl.ConsoleModes.ProcessedOutput | ConsoleControl.ConsoleModes.WrapEndOfLine; - if ((m & desiredMode) != desiredMode) + if ((m & DesiredMode) != DesiredMode) { - m |= desiredMode; + m |= DesiredMode; ConsoleControl.SetMode(handle, m); } #endif @@ -583,7 +582,6 @@ private void WriteToConsole(ReadOnlySpan value, bool transcribeResult, boo PreWrite(); // This is atomic, so we don't lock here... - #if !UNIX ConsoleControl.WriteConsole(handle, value, newLine); #else @@ -755,15 +753,12 @@ public override void Write(ConsoleColor foregroundColor, ConsoleColor background } /// - /// - /// See base class - /// + /// See base class. /// /// /// /// /// - /// /// If obtaining information about the buffer failed /// OR /// Win32's SetConsoleTextAttribute @@ -775,7 +770,6 @@ public override void Write(ConsoleColor foregroundColor, ConsoleColor background /// Win32's SetConsoleMode fails /// OR /// Win32's WriteConsole fails - /// /// public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) { @@ -831,12 +825,9 @@ public override void WriteLine(string value) } /// - /// - /// See base class - /// + /// See base class. /// /// - /// /// Win32's CreateFile fails /// OR /// Win32's GetConsoleMode fails @@ -844,7 +835,6 @@ public override void WriteLine(string value) /// Win32's SetConsoleMode fails /// OR /// Win32's WriteConsole fails - /// /// public override void WriteLine() { @@ -1539,15 +1529,15 @@ private string ReadLineFromConsole(bool endOnTab, string initialContent, bool ca ConsoleHandle handle = ConsoleControl.GetConioDeviceHandle(); ConsoleControl.ConsoleModes m = ConsoleControl.GetMode(handle); - const ConsoleControl.ConsoleModes desiredMode = + const ConsoleControl.ConsoleModes DesiredMode = ConsoleControl.ConsoleModes.LineInput | ConsoleControl.ConsoleModes.EchoInput | ConsoleControl.ConsoleModes.ProcessedInput; - if ((m & desiredMode) != desiredMode || (m & ConsoleControl.ConsoleModes.MouseInput) > 0) + if ((m & DesiredMode) != DesiredMode || (m & ConsoleControl.ConsoleModes.MouseInput) > 0) { m &= ~ConsoleControl.ConsoleModes.MouseInput; - m |= desiredMode; + m |= DesiredMode; ConsoleControl.SetMode(handle, m); } #endif From 0354f7297e5be914d770ad119f9d86d2d1b24da6 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 10 Jan 2019 10:15:35 +0500 Subject: [PATCH 08/18] Style fix 3 --- .../host/msh/ConsoleTextWriter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs index ce93aa2f74e..e8ad4daecaa 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs @@ -65,15 +65,15 @@ public override public override void - Write(Boolean b) + Write(bool b) { if (b) { - _ui.WriteToConsole(Boolean.TrueString.AsSpan(), transcribeResult: true); + _ui.WriteToConsole(bool.TrueString.AsSpan(), transcribeResult: true); } else { - _ui.WriteToConsole(Boolean.FalseString.AsSpan(), transcribeResult: true); + _ui.WriteToConsole(bool.FalseString.AsSpan(), transcribeResult: true); } } From b66f03ecbc7b7935d8fc170ff17a46cffd6a1558 Mon Sep 17 00:00:00 2001 From: Ilya Date: Wed, 3 Apr 2019 09:47:23 +0500 Subject: [PATCH 09/18] Address feedback --- .../host/msh/ConsoleHostTranscript.cs | 2 +- .../host/msh/ConsoleHostUserInterface.cs | 27 +++---------------- .../msh/ConsoleHostUserInterfaceProgress.cs | 4 +-- 3 files changed, 7 insertions(+), 26 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostTranscript.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostTranscript.cs index 8f052004160..fe3b37a77ba 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostTranscript.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostTranscript.cs @@ -108,7 +108,7 @@ internal void WriteLineToTranscript(ReadOnlySpan text) WriteToTranscript(text, newLine: true); } - private void WriteToTranscript(ReadOnlySpan text, bool newLine) + internal void WriteToTranscript(ReadOnlySpan text, bool newLine) { lock (_transcriptionStateLock) { diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index f21ebb429fc..720aa9bb65a 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -595,7 +595,7 @@ private void WriteToConsole(ReadOnlySpan value, bool transcribeResult, boo if (transcribeResult) { - PostWrite(value); + PostWrite(value, newLine); } else { @@ -633,14 +633,7 @@ private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroun try { - if (newLine) - { - WriteLineToConsole(text, transcribeResult: true); - } - else - { - WriteToConsole(text, transcribeResult: true); - } + WriteToConsole(text, transcribeResult: true, newLine); } finally { @@ -707,12 +700,7 @@ private void WriteHelper(string value, bool newLine) { Dbg.Assert(writer == _parent.OutputSerializer.textWriter, "writers should be the same"); - _parent.OutputSerializer.Serialize(value); - - if (newLine) - { - _parent.OutputSerializer.Serialize(Crlf); - } + _parent.OutputSerializer.Serialize(value + Crlf); } else { @@ -789,14 +777,7 @@ private void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, s try { - if (newLine) - { - this.WriteLine(value); - } - else - { - this.Write(value); - } + this.WriteHelper(value, newLine); } finally { diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs index 9b3c7abbcd0..88ad4e99792 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs @@ -130,7 +130,7 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt private void - PostWrite(ReadOnlySpan value) + PostWrite(ReadOnlySpan value, bool newLine) { PostWrite(); @@ -138,7 +138,7 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt { try { - _parent.WriteToTranscript(value); + _parent.WriteToTranscript(value, newLine); } catch (Exception) { From 42823c60501ced2bf2477122379544b09cc4a477 Mon Sep 17 00:00:00 2001 From: Ilya Date: Wed, 3 Apr 2019 10:03:24 +0500 Subject: [PATCH 10/18] Add AggressiveInlining --- .../host/msh/ConsoleHostUserInterface.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 720aa9bb65a..322f4e44bb6 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -2,16 +2,18 @@ // Licensed under the MIT License. using System; -using System.IO; -using System.Diagnostics.CodeAnalysis; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Linq; -using System.Management.Automation.Runspaces; -using System.Text; using System.Management.Automation; using System.Management.Automation.Internal; using System.Management.Automation.Host; +using System.Management.Automation.Runspaces; +using System.Runtime.CompilerServices; using System.Security; +using System.Text; + using Dbg = System.Management.Automation.Diagnostics; #if !UNIX using ConsoleHandle = Microsoft.Win32.SafeHandles.SafeFileHandle; @@ -542,6 +544,7 @@ private static bool shouldUnsetMode( #region WriteToConsole + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void WriteToConsole(char c, bool transcribeResult) { Span value = stackalloc char[1]; @@ -549,11 +552,13 @@ internal void WriteToConsole(char c, bool transcribeResult) WriteToConsole(value, transcribeResult); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void WriteToConsole(ReadOnlySpan value, bool transcribeResult) { WriteToConsole(value, transcribeResult, newLine: false); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void WriteLineToConsole(ReadOnlySpan value, bool transcribeResult) { WriteToConsole(value, transcribeResult, newLine: true); @@ -603,6 +608,7 @@ private void WriteToConsole(ReadOnlySpan value, bool transcribeResult, boo } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ConsoleOutWriteHelper(ReadOnlySpan value, bool newLine) { if (newLine) @@ -615,6 +621,7 @@ private void ConsoleOutWriteHelper(ReadOnlySpan value, bool newLine) } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteLineToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text) { WriteToConsole(PromptColor, backgroundColor, text, newLine: true); @@ -643,11 +650,13 @@ private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroun } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteLineToConsole(string text) { WriteLineToConsole(text, transcribeResult: true); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteLineToConsole() { WriteLineToConsole(string.Empty, transcribeResult: true); From 273b1b770f5eda767e8b392247e5931b85a23bef Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 4 Apr 2019 08:41:45 +0500 Subject: [PATCH 11/18] Fix broken rebase --- .../host/msh/ConsoleHostUserInterface.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 322f4e44bb6..ecc61267587 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -624,7 +624,7 @@ private void ConsoleOutWriteHelper(ReadOnlySpan value, bool newLine) [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteLineToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text) { - WriteToConsole(PromptColor, backgroundColor, text, newLine: true); + WriteToConsole(foregroundColor, backgroundColor, text, newLine: true); } private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text, bool newLine = false) From 5b7bb7796edd42c5390ac8b6bdc8b126f9a6a561 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 4 Apr 2019 09:12:34 +0500 Subject: [PATCH 12/18] Put new line only after last position --- .../host/msh/ConsoleControl.cs | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index 63751b596b8..37dcfb6ae9a 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -2581,25 +2581,27 @@ internal static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan outBufferLine = stackalloc char[outBuffer.Length + endOfLineLength]; - outBuffer.CopyTo(outBufferLine); - endOfLine.CopyTo(outBufferLine.Slice(outBufferLine.Length - endOfLineLength)); - WriteConsole(consoleHandle, outBufferLine); - } - else - { - WriteConsole(consoleHandle, outBuffer); + if (newLine) + { + var endOfLine = ConsoleHostUserInterface.Crlf.AsSpan(); + var endOfLineLength = endOfLine.Length; + Span outBufferLine = stackalloc char[outBuffer.Length + endOfLineLength]; + outBuffer.CopyTo(outBufferLine); + endOfLine.CopyTo(outBufferLine.Slice(outBufferLine.Length - endOfLineLength)); + WriteConsole(consoleHandle, outBufferLine); + } + else + { + WriteConsole(consoleHandle, outBuffer); + } } } } From e57001de719f7aa47ae04a17d6b31da5ed95b044 Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 8 Apr 2019 08:50:15 +0500 Subject: [PATCH 13/18] Fix init --- .../host/msh/ConsoleHostUserInterface.cs | 3 +-- .../host/msh/ConsoleTextWriter.cs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index ecc61267587..a126a5d8339 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -547,8 +547,7 @@ private static bool shouldUnsetMode( [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void WriteToConsole(char c, bool transcribeResult) { - Span value = stackalloc char[1]; - value[0] = c; + Span value = stackalloc char[1] { c }; WriteToConsole(value, transcribeResult); } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs index e8ad4daecaa..9538453f0fa 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs @@ -82,8 +82,7 @@ public override void Write(char c) { - Span c1 = stackalloc char[1]; - c1[0] = c; + Span c1 = stackalloc char[1] { c }; _ui.WriteToConsole(c1, transcribeResult: true); } From d16d77930ab02f48f52b2e8574f448db56f7dd6e Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 8 Apr 2019 08:52:52 +0500 Subject: [PATCH 14/18] Update ConsoleTextWriter.cs Remove AsSpan() --- .../host/msh/ConsoleTextWriter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs index 9538453f0fa..ae990c4c7a3 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs @@ -69,11 +69,11 @@ public override { if (b) { - _ui.WriteToConsole(bool.TrueString.AsSpan(), transcribeResult: true); + _ui.WriteToConsole(bool.TrueString, transcribeResult: true); } else { - _ui.WriteToConsole(bool.FalseString.AsSpan(), transcribeResult: true); + _ui.WriteToConsole(bool.FalseString, transcribeResult: true); } } From c97670d13df9c8c61565d6b28b3ea15ed5f67b66 Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 8 Apr 2019 09:06:19 +0500 Subject: [PATCH 15/18] Reorder methods --- .../host/msh/ConsoleHostUserInterface.cs | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index a126a5d8339..a02826e9922 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -557,12 +557,6 @@ internal void WriteToConsole(ReadOnlySpan value, bool transcribeResult) WriteToConsole(value, transcribeResult, newLine: false); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal void WriteLineToConsole(ReadOnlySpan value, bool transcribeResult) - { - WriteToConsole(value, transcribeResult, newLine: true); - } - private void WriteToConsole(ReadOnlySpan value, bool transcribeResult, bool newLine) { #if !UNIX @@ -607,25 +601,6 @@ private void WriteToConsole(ReadOnlySpan value, bool transcribeResult, boo } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ConsoleOutWriteHelper(ReadOnlySpan value, bool newLine) - { - if (newLine) - { - Console.Out.WriteLine(value); - } - else - { - Console.Out.Write(value); - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void WriteLineToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text) - { - WriteToConsole(foregroundColor, backgroundColor, text, newLine: true); - } - private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text, bool newLine = false) { // Sync access so that we don't race on color settings if called from multiple threads. @@ -649,6 +624,31 @@ private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroun } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ConsoleOutWriteHelper(ReadOnlySpan value, bool newLine) + { + if (newLine) + { + Console.Out.WriteLine(value); + } + else + { + Console.Out.Write(value); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void WriteLineToConsole(ReadOnlySpan value, bool transcribeResult) + { + WriteToConsole(value, transcribeResult, newLine: true); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void WriteLineToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text) + { + WriteToConsole(foregroundColor, backgroundColor, text, newLine: true); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteLineToConsole(string text) { From 232e303723480ee84f349f6df5393fecd5dd05b4 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 9 Apr 2019 19:28:32 +0500 Subject: [PATCH 16/18] Fix Serialize --- .../host/msh/ConsoleHostUserInterface.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index a02826e9922..f2ae73ce13a 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -708,7 +708,12 @@ private void WriteHelper(string value, bool newLine) { Dbg.Assert(writer == _parent.OutputSerializer.textWriter, "writers should be the same"); - _parent.OutputSerializer.Serialize(value + Crlf); + _parent.OutputSerializer.Serialize(value); + + if (newLine) + { + _parent.OutputSerializer.Serialize(Crlf); + } } else { From 59b6cd00659e73dec211d11a1da281a27239ba46 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 15 Apr 2019 11:27:03 -0700 Subject: [PATCH 17/18] Address my own comments --- src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs | 2 +- .../host/msh/ConsoleHostUserInterface.cs | 2 +- .../host/msh/ConsoleTextWriter.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index 37dcfb6ae9a..f925dbd7f73 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -2562,7 +2562,7 @@ internal static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan value = stackalloc char[1] { c }; + ReadOnlySpan value = stackalloc char[1] { c }; WriteToConsole(value, transcribeResult); } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs index ae990c4c7a3..1315d139995 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs @@ -82,7 +82,7 @@ public override void Write(char c) { - Span c1 = stackalloc char[1] { c }; + ReadOnlySpan c1 = stackalloc char[1] { c }; _ui.WriteToConsole(c1, transcribeResult: true); } From f6a72af9b08285fd8dcbcda085935231baccb3bf Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 15 Apr 2019 11:47:43 -0700 Subject: [PATCH 18/18] Rename 'WriteHelper' to 'WriteImpl'; refactor 'WriteLine' and 'WriteLineToConsole' --- .../host/msh/ConsoleHostUserInterface.cs | 12 ++++++------ .../host/msh/ConsoleTextWriter.cs | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 09160e3ebd7..53431f42d92 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -658,7 +658,7 @@ private void WriteLineToConsole(string text) [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteLineToConsole() { - WriteLineToConsole(string.Empty, transcribeResult: true); + WriteToConsole(Environment.NewLine, transcribeResult: true, newLine: false); } #endregion WriteToConsole @@ -679,10 +679,10 @@ private void WriteLineToConsole() public override void Write(string value) { - WriteHelper(value, newLine: false); + WriteImpl(value, newLine: false); } - private void WriteHelper(string value, bool newLine) + private void WriteImpl(string value, bool newLine) { if (string.IsNullOrEmpty(value) && !newLine) { @@ -790,7 +790,7 @@ private void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, s try { - this.WriteHelper(value, newLine); + this.WriteImpl(value, newLine); } finally { @@ -815,7 +815,7 @@ private void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, s /// public override void WriteLine(string value) { - this.WriteHelper(value, newLine: true); + this.WriteImpl(value, newLine: true); } /// @@ -832,7 +832,7 @@ public override void WriteLine(string value) /// public override void WriteLine() { - this.WriteHelper(string.Empty, newLine: true); + this.WriteImpl(Environment.NewLine, newLine: false); } #region Word Wrapping diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs index 1315d139995..888ba4bd7d1 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleTextWriter.cs @@ -75,7 +75,6 @@ public override { _ui.WriteToConsole(bool.FalseString, transcribeResult: true); } - } public override