Skip to content

Commit 3733a0b

Browse files
authored
Avoid unnecessary StringCollection allocation in formatting code (PowerShell#15832)
1 parent 5e15f66 commit 3733a0b

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Collections.Specialized;
76
using System.Management.Automation;
87
using System.Management.Automation.Internal;
98

@@ -1117,33 +1116,40 @@ private void InternalInitialize(ListViewEntry lve)
11171116

11181117
internal static string[] GetProperties(ListViewEntry lve)
11191118
{
1120-
StringCollection props = new StringCollection();
1121-
foreach (ListViewField lvf in lve.listViewFieldList)
1119+
int count = lve.listViewFieldList.Count;
1120+
1121+
if (count == 0)
11221122
{
1123-
props.Add(lvf.label ?? lvf.propertyName);
1123+
return null;
11241124
}
11251125

1126-
if (props.Count == 0)
1127-
return null;
1128-
string[] retVal = new string[props.Count];
1129-
props.CopyTo(retVal, 0);
1130-
return retVal;
1126+
string[] result = new string[count];
1127+
for (int index = 0; index < result.Length; ++index)
1128+
{
1129+
ListViewField lvf = lve.listViewFieldList[index];
1130+
result[index] = lvf.label ?? lvf.propertyName;
1131+
}
1132+
1133+
return result;
11311134
}
11321135

11331136
internal static string[] GetValues(ListViewEntry lve)
11341137
{
1135-
StringCollection vals = new StringCollection();
1138+
int count = lve.listViewFieldList.Count;
11361139

1137-
foreach (ListViewField lvf in lve.listViewFieldList)
1140+
if (count == 0)
11381141
{
1139-
vals.Add(lvf.formatPropertyField.propertyValue);
1142+
return null;
11401143
}
11411144

1142-
if (vals.Count == 0)
1143-
return null;
1144-
string[] retVal = new string[vals.Count];
1145-
vals.CopyTo(retVal, 0);
1146-
return retVal;
1145+
string[] result = new string[count];
1146+
for (int index = 0; index < result.Length; ++index)
1147+
{
1148+
ListViewField lvf = lve.listViewFieldList[index];
1149+
result[index] = lvf.formatPropertyField.propertyValue;
1150+
}
1151+
1152+
return result;
11471153
}
11481154

11491155
/// <summary>

0 commit comments

Comments
 (0)