Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ private enum FormattingState
/// </summary>
private enum PreprocessingState { raw, processed, error }

private const int DefaultConsoleWidth = 120;
internal const int StackAllocThreshold = 120;

/// <summary>
/// test if an object coming from the pipeline needs to be
/// preprocessed by the default formatter
Expand Down Expand Up @@ -758,13 +761,11 @@ private void ProcessCachedGroupOnWide(WideViewHeaderInfo wvhi, List<PacketInfoDa
/// </summary>
static private int GetConsoleWindowWidth(int columnNumber)
{
const int defaultConsoleWidth = 120;

if (columnNumber == int.MaxValue)
{
if (_noConsole)
{
return defaultConsoleWidth;
return DefaultConsoleWidth;
}
try
{
Expand All @@ -773,7 +774,7 @@ static private int GetConsoleWindowWidth(int columnNumber)
catch
{
_noConsole = true;
return defaultConsoleWidth;
return DefaultConsoleWidth;
}
}
return columnNumber;
Expand Down Expand Up @@ -941,7 +942,6 @@ internal override void Initialize()
TableFormattingHint tableHint = this.InnerCommand.RetrieveFormattingHint() as TableFormattingHint;
int[] columnWidthsHint = null;
// We expect that console width is less then 120.
const int columnsThresHold = 120;

if (tableHint != null)
{
Expand All @@ -957,8 +957,8 @@ internal override void Initialize()
}

// create arrays for widths and alignment
Span<int> columnWidths = columns < columnsThresHold ? stackalloc int[columns] : new int[columns];
Span<int> alignment = columns < columnsThresHold ? stackalloc int[columns] : new int[columns];
Span<int> columnWidths = columns <= StackAllocThreshold ? stackalloc int[columns] : new int[columns];
Span<int> alignment = columns <= StackAllocThreshold ? stackalloc int[columns] : new int[columns];

int k = 0;
foreach (TableColumnInfo tci in this.CurrentTableHeaderInfo.tableColumnInfoList)
Expand Down Expand Up @@ -1008,7 +1008,7 @@ internal override void ProcessPayload(FormatEntryData fed)

// need to make sure we have matching counts: the header count will have to prevail
string[] values = new string[headerColumns];
Span<int> alignment = stackalloc int[headerColumns];
Span<int> alignment = headerColumns <= StackAllocThreshold ? stackalloc int[headerColumns] : new int[headerColumns];

int fieldCount = tre.formatPropertyFieldList.Count;

Expand Down Expand Up @@ -1170,8 +1170,8 @@ internal override void Initialize()
_buffer = new StringValuesBuffer(itemsPerRow);

// initialize the writer
Span<int> columnWidths = stackalloc int[itemsPerRow];
Span<int> alignment = stackalloc int[itemsPerRow];
Span<int> columnWidths = itemsPerRow <= StackAllocThreshold ? stackalloc int[itemsPerRow] : new int[itemsPerRow];
Span<int> alignment = itemsPerRow <= StackAllocThreshold ? stackalloc int[itemsPerRow] : new int[itemsPerRow];

for (int k = 0; k < itemsPerRow; k++)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Management.Automation.Internal;
Expand Down Expand Up @@ -64,7 +65,7 @@ internal void Initialize(string[] propertyNames, int screenColumnWidth, DisplayC
_propertyLabelsDisplayLength = 0; // reset max

// cache the cell lengths for each property
int[] propertyNameCellCounts = new int[propertyNames.Length];
Span<int> propertyNameCellCounts = propertyNames.Length <= OutCommandInner.StackAllocThreshold ? stackalloc int[propertyNames.Length] : new int[propertyNames.Length];;
for (int k = 0; k < propertyNames.Length; k++)
{
Debug.Assert(propertyNames[k] != null, "propertyNames[k] is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,6 @@ internal void Initialize(int leftMarginIndent, int screenColumns, Span<int> colu
//Console.WriteLine(" 1 2 3 4 5 6 7");
//Console.WriteLine("01234567890123456789012345678901234567890123456789012345678901234567890123456789");

if (screenColumns == int.MaxValue)
{
try
{
screenColumns = System.Console.WindowWidth;
}
catch
{
screenColumns = 120;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing this part?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It duplicates a code in GetConsoleWindowWidth so we should remove this (or add cache as in GetConsoleWindowWidth).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, got it. #close


if (leftMarginIndent < 0)
{
leftMarginIndent = 0;
Expand Down Expand Up @@ -194,7 +182,7 @@ internal void GenerateRow(string[] values, LineOutput lo, bool multiLine, ReadOn

// build the current row alignment settings
int cols = _si.columnInfo.Length;
Span<int> currentAlignment = stackalloc int[cols];
Span<int> currentAlignment = cols <= OutCommandInner.StackAllocThreshold ? stackalloc int[cols] : new int[cols];

if (alignment == null)
{
Expand Down Expand Up @@ -232,7 +220,7 @@ internal void GenerateRow(string[] values, LineOutput lo, bool multiLine, ReadOn
private string[] GenerateTableRow(string[] values, ReadOnlySpan<int> alignment, DisplayCells ds)
{
// select the active columns (skip hidden ones)
Span<int> validColumnArray = stackalloc int[_si.columnInfo.Length];
Span<int> validColumnArray = _si.columnInfo.Length <= OutCommandInner.StackAllocThreshold ? stackalloc int[_si.columnInfo.Length] : new int[_si.columnInfo.Length];
int validColumnCount = 0;
for (int k = 0; k < _si.columnInfo.Length; k++)
{
Expand Down Expand Up @@ -303,7 +291,7 @@ private string[] GenerateTableRow(string[] values, ReadOnlySpan<int> alignment,
//
// To ensure we don't add whitespace to the end, we need to determine the last column in each row with content

System.Span<int> lastColWithContent = stackalloc int[screenRows];
System.Span<int> lastColWithContent = screenRows <= OutCommandInner.StackAllocThreshold ? stackalloc int[screenRows] : new int[screenRows];
for (int row = 0; row < screenRows; row++)
{
for (int col = scArray.Length - 1; col > 0; col--)
Expand Down