Add some optimizations in formatting subsystem#6678
Add some optimizations in formatting subsystem#6678iSazonov merged 3 commits intoPowerShell:masterfrom
Conversation
Use safe pattern 'columns <= columnsThresHold ? stackalloc int[columns] : new int[columns];' Remove duplicate code. Use common 'columnsThresHold' constant
| /// </summary> | ||
| private enum PreprocessingState { raw, processed, error } | ||
|
|
||
| const int defaultConsoleWidth = 120; |
There was a problem hiding this comment.
This variable is only used in the method GetConsoleWindowWidth. Maybe it should be kept as a local const variable in that method.
I know both const vars here are with the value 120, but the naming suggests different things. return defaultConsoleWidth in GetConsoleWindowWidth is more readable than return StackAllocThreshold in my opinion :)
There was a problem hiding this comment.
I put both constants next to each other specially because if we decide to change the first value, we probably want to change the second value too.
There was a problem hiding this comment.
I added the private modifier to 'DefaultConsoleWidth'.
| private enum PreprocessingState { raw, processed, error } | ||
|
|
||
| const int defaultConsoleWidth = 120; | ||
| internal const int columnsThresHold = 120; |
There was a problem hiding this comment.
columnsThresHold
threshold is a single word, so maybe change it to columnsThreshold?
There was a problem hiding this comment.
This threshold is more about using stack allocation or managed heap allocation, so maybe we can give it a more general name, like StackAllocThreshold.
As for the naming, you probably should use StackAllocThreshold, accoding to the naming convention code-guidline
Use
PascalCaseto name constant local variables and fields. The only exception is for interop code where the constant should exactly match the name and value of the code you are calling via interop (e.g.const int ERROR_SUCCESS = 0).
| /// <param name="dc">instance of the DisplayCells helper object</param> | ||
| internal void Initialize(string[] propertyNames, int screenColumnWidth, DisplayCells dc) | ||
| { | ||
| const int propertiesThresHold = OutCommandInner.columnsThresHold; |
There was a problem hiding this comment.
I don't think you need this; just use OutCommandInner.columnsThresHold instead.
C# compiler will use the int value directly in the IL. See an example here:
C# code
void Main()
{
int a = Console.Read();
string[] b = a <= Test.columnsThresHold ? new string[2] : new string[3];
}
// Define other methods and classes here
internal class Test
{
internal const int columnsThresHold = 120;
}
IL code
IL_0000: nop
IL_0001: call System.Console.Read
IL_0006: stloc.0 // a
IL_0007: ldloc.0 // a
IL_0008: ldc.i4.s 78 // <=== 0x78 == 120
IL_000A: ble.s IL_0014
IL_000C: ldc.i4.3
IL_000D: newarr System.String
IL_0012: br.s IL_001A
IL_0014: ldc.i4.2
IL_0015: newarr System.String
IL_001A: stloc.1 // b
IL_001B: ret
Test..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: nop
IL_0007: ret
There was a problem hiding this comment.
Thanks for the in-depth explanation!
Fixed.
| { | ||
| screenColumns = 120; | ||
| } | ||
| } |
There was a problem hiding this comment.
Why are we removing this part?
There was a problem hiding this comment.
It duplicates a code in GetConsoleWindowWidth so we should remove this (or add cache as in GetConsoleWindowWidth).
|
|
||
| private int _startColumn = 0; | ||
|
|
||
| const int columnsThresHold = OutCommandInner.columnsThresHold; |
There was a problem hiding this comment.
Same here. No need to declare another const int variable.
|
@daxian-dbw Thanks for help! |
PR Summary
Continue #6648
Use safe pattern 'columns <= columnsThresHold ? stackalloc int[columns] : new int[columns];'.
Remove duplicate code (we already have it in
GetConsoleWindowWidth()with a cache).Use common 'StackAllocThreshold ' constant.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests