Skip to content

Conversation

@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Apr 19, 2018

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

Use safe pattern 'columns <= columnsThresHold ? stackalloc int[columns] : new int[columns];'
Remove duplicate code.
Use common 'columnsThresHold' constant
@iSazonov iSazonov self-assigned this Apr 19, 2018
/// </summary>
private enum PreprocessingState { raw, processed, error }

const int defaultConsoleWidth = 120;
Copy link
Member

Choose a reason for hiding this comment

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

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 :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

I added the private modifier to 'DefaultConsoleWidth'.

private enum PreprocessingState { raw, processed, error }

const int defaultConsoleWidth = 120;
internal const int columnsThresHold = 120;
Copy link
Member

Choose a reason for hiding this comment

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

columnsThresHold

threshold is a single word, so maybe change it to columnsThreshold?

Copy link
Member

@daxian-dbw daxian-dbw Apr 23, 2018

Choose a reason for hiding this comment

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

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 PascalCase to 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).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

/// <param name="dc">instance of the DisplayCells helper object</param>
internal void Initialize(string[] propertyNames, int screenColumnWidth, DisplayCells dc)
{
const int propertiesThresHold = OutCommandInner.columnsThresHold;
Copy link
Member

@daxian-dbw daxian-dbw Apr 23, 2018

Choose a reason for hiding this comment

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

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         

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the in-depth explanation!

Fixed.

{
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


private int _startColumn = 0;

const int columnsThresHold = OutCommandInner.columnsThresHold;
Copy link
Member

Choose a reason for hiding this comment

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

Same here. No need to declare another const int variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

@iSazonov iSazonov merged commit a9781de into PowerShell:master Apr 25, 2018
@iSazonov
Copy link
Collaborator Author

@daxian-dbw Thanks for help!

@iSazonov iSazonov deleted the span-listwriter branch April 25, 2018 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants