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
2 changes: 1 addition & 1 deletion .globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ dotnet_diagnostic.CA1823.severity = none
dotnet_diagnostic.CA1824.severity = suggestion

# CA1825: Avoid zero-length array allocations
dotnet_diagnostic.CA1825.severity = suggestion
dotnet_diagnostic.CA1825.severity = warning

# CA1826: Do not use Enumerable methods on indexable collections
dotnet_diagnostic.CA1826.severity = suggestion
Expand Down
3 changes: 0 additions & 3 deletions docs/dev-process/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ Some general guidelines:
* Avoid using string interpolations and overloads with implicit parameters such as `Culture` and `StringComparison`.
Instead, use overloads with more explicit parameters such as `String.Format(IFormatProvider, String, Object[])` and `Equals(String, String, StringComparison)`.

* Avoid creating empty arrays.
Comment thread
xtqqczze marked this conversation as resolved.
Instead, reuse the static ones via `Utils.EmptyArray<T>`.

* Avoid unnecessary memory allocation in a loop.
Move the memory allocation outside the loop if possible.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public ModuleSpecification[] FullyQualifiedModule
}
}

private ModuleSpecification[] _moduleSpecifications = new ModuleSpecification[0];
private ModuleSpecification[] _moduleSpecifications = Array.Empty<ModuleSpecification>();
internal bool IsFullyQualifiedModuleSpecified = false;

private bool _commandParameterSpecified; // initialized to default value in the constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ private string FormatLine(string lineStr, int displayLineNumber, string displayP
/// <summary>
/// Gets or sets a list of all Regex matches on the matching line.
/// </summary>
public Match[] Matches { get; set; } = new Match[] { };
public Match[] Matches { get; set; } = Array.Empty<Match>();

/// <summary>
/// Create a deep copy of this MatchInfo instance.
Expand Down Expand Up @@ -1897,8 +1897,8 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
if (matchInfo.Context != null)
{
matchResult = matchInfo.Clone();
matchResult.Context.DisplayPreContext = new string[] { };
matchResult.Context.DisplayPostContext = new string[] { };
matchResult.Context.DisplayPreContext = Array.Empty<string>();
matchResult.Context.DisplayPostContext = Array.Empty<string>();
}
else
{
Expand All @@ -1924,7 +1924,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI

// Matches should be an empty list, rather than null,
// in the cases of notMatch and simpleMatch.
matchResult.Matches = matches ?? new Match[] { };
matchResult.Matches = matches ?? Array.Empty<Match>();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ protected override void BeginProcessing()
ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
if (ci != null && ci.IsPublic)
{
_newObject = CallConstructor(type, new ConstructorInfo[] { ci }, new object[] { });
_newObject = CallConstructor(type, new ConstructorInfo[] { ci }, Array.Empty<object>());
if (_newObject != null && Property != null)
{
// The method invocation is disabled for "Hashtable to Object conversion" (Win8:649519), but we need to keep it enabled for New-Object for compatibility to PSv2
Expand Down
2 changes: 1 addition & 1 deletion src/System.Management.Automation/engine/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ private void OnElapsedEvent(object source)
if (_engineEventSubscribers.TryGetValue(PSEngineEvent.OnIdle, out subscribers) && subscribers.Count > 0)
{
// We send out on-idle event and keep enabling the timer only if there still are subscribers to the on-idle event
GenerateEvent(PSEngineEvent.OnIdle, null, new object[] { }, null, false, false);
GenerateEvent(PSEngineEvent.OnIdle, null, Array.Empty<object>(), null, false, false);
EnableTimer();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private void CreateGetEnumerator()

emitter.Emit(OpCodes.Ldarg_0);
emitter.Emit(OpCodes.Castclass, _enumerableType);
MethodInfo methodInfo = _enumerableType.GetMethod("GetEnumerator", new Type[] { });
MethodInfo methodInfo = _enumerableType.GetMethod("GetEnumerator", Array.Empty<Type>());
emitter.Emit(OpCodes.Callvirt, methodInfo);
emitter.Emit(OpCodes.Ret);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6882,7 +6882,7 @@ internal PSModuleInfo LoadBinaryModule(PSModuleInfo parentModule, bool trySnapIn
iss.Bind(Context, updateOnly: true, module, options.NoClobber, options.Local, setLocation: false);

// Scan all of the types in the assembly to register JobSourceAdapters.
IEnumerable<Type> allTypes = new Type[] { };
IEnumerable<Type> allTypes = Array.Empty<Type>();
if (assembly != null)
{
allTypes = assembly.ExportedTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ private object EncodeCollection(
toType,
0,
null,
new object[] { },
Array.Empty<object>(),
System.Globalization.CultureInfo.InvariantCulture);
if (collectionTypeInformation.ParameterCollectionType == ParameterCollectionType.IList)
resultAsIList = (IList)resultCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,6 @@ public string HelpFile
/// </summary>
public object Data { get; set; }

internal static readonly RuntimeDefinedParameter[] EmptyParameterArray = new RuntimeDefinedParameter[0];
internal static readonly RuntimeDefinedParameter[] EmptyParameterArray = Array.Empty<RuntimeDefinedParameter>();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Perhaps we can inline EmptyParameterArray ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not in the PR.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ private void DoCloseHelper()

// Generate the shutdown event
if (Events != null)
Events.GenerateEvent(PSEngineEvent.Exiting, null, new object[] { }, null, true, false);
Events.GenerateEvent(PSEngineEvent.Exiting, null, Array.Empty<object>(), null, true, false);

// Stop all running pipelines
// Note:Do not perform the Cancel in lock. Reason is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,35 +144,35 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHost),
"get_Name",
typeof(string),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.GetVersion:
return new RemoteHostMethodInfo(
typeof(PSHost),
"get_Version",
typeof(Version),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.GetInstanceId:
return new RemoteHostMethodInfo(
typeof(PSHost),
"get_InstanceId",
typeof(Guid),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.GetCurrentCulture:
return new RemoteHostMethodInfo(
typeof(PSHost),
"get_CurrentCulture",
typeof(System.Globalization.CultureInfo),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.GetCurrentUICulture:
return new RemoteHostMethodInfo(
typeof(PSHost),
"get_CurrentUICulture",
typeof(System.Globalization.CultureInfo),
new Type[] { });
Array.Empty<Type>());

// Host methods.

Expand All @@ -188,28 +188,28 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHost),
"EnterNestedPrompt",
typeof(void),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.ExitNestedPrompt:
return new RemoteHostMethodInfo(
typeof(PSHost),
"ExitNestedPrompt",
typeof(void),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.NotifyBeginApplication:
return new RemoteHostMethodInfo(
typeof(PSHost),
"NotifyBeginApplication",
typeof(void),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.NotifyEndApplication:
return new RemoteHostMethodInfo(
typeof(PSHost),
"NotifyEndApplication",
typeof(void),
new Type[] { });
Array.Empty<Type>());

// Host UI methods.

Expand All @@ -218,14 +218,14 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostUserInterface),
"ReadLine",
typeof(string),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.ReadLineAsSecureString:
return new RemoteHostMethodInfo(
typeof(PSHostUserInterface),
"ReadLineAsSecureString",
typeof(System.Security.SecureString),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.Write1:
return new RemoteHostMethodInfo(
Expand All @@ -246,7 +246,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostUserInterface),
"WriteLine",
typeof(void),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.WriteLine2:
return new RemoteHostMethodInfo(
Expand Down Expand Up @@ -339,7 +339,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"get_ForegroundColor",
typeof(ConsoleColor),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.SetForegroundColor:
return new RemoteHostMethodInfo(
Expand All @@ -353,7 +353,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"get_BackgroundColor",
typeof(ConsoleColor),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.SetBackgroundColor:
return new RemoteHostMethodInfo(
Expand All @@ -367,7 +367,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"get_CursorPosition",
typeof(Coordinates),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.SetCursorPosition:
return new RemoteHostMethodInfo(
Expand All @@ -381,7 +381,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"get_WindowPosition",
typeof(Coordinates),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.SetWindowPosition:
return new RemoteHostMethodInfo(
Expand All @@ -395,7 +395,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"get_CursorSize",
typeof(int),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.SetCursorSize:
return new RemoteHostMethodInfo(
Expand All @@ -409,7 +409,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"get_BufferSize",
typeof(Size),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.SetBufferSize:
return new RemoteHostMethodInfo(
Expand All @@ -423,7 +423,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"get_WindowSize",
typeof(Size),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.SetWindowSize:
return new RemoteHostMethodInfo(
Expand All @@ -437,7 +437,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"get_WindowTitle",
typeof(string),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.SetWindowTitle:
return new RemoteHostMethodInfo(
Expand All @@ -453,21 +453,21 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"get_MaxWindowSize",
typeof(Size),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.GetMaxPhysicalWindowSize:
return new RemoteHostMethodInfo(
typeof(PSHostRawUserInterface),
"get_MaxPhysicalWindowSize",
typeof(Size),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.GetKeyAvailable:
return new RemoteHostMethodInfo(
typeof(PSHostRawUserInterface),
"get_KeyAvailable",
typeof(bool),
new Type[] { });
Array.Empty<Type>());

// Host raw UI methods.

Expand All @@ -483,7 +483,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(PSHostRawUserInterface),
"FlushInputBuffer",
typeof(void),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.SetBufferContents1:
return new RemoteHostMethodInfo(
Expand Down Expand Up @@ -527,7 +527,7 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(IHostSupportsInteractiveSession),
"PopRunspace",
typeof(void),
new Type[] { });
Array.Empty<Type>());

// IHostSupportsInteractiveSession properties.

Expand All @@ -536,14 +536,14 @@ internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)
typeof(IHostSupportsInteractiveSession),
"get_IsRunspacePushed",
typeof(bool),
new Type[] { });
Array.Empty<Type>());

case RemoteHostMethodId.GetRunspace:
return new RemoteHostMethodInfo(
typeof(IHostSupportsInteractiveSession),
"get_Runspace",
typeof(System.Management.Automation.Runspaces.Runspace),
new Type[] { });
Array.Empty<Type>());

default:
Dbg.Assert(false, "All RemoteHostMethodId's should be handled. This code should not be reached.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ internal static T[] Multiply<T>(T[] array, uint times)

if (times == 0 || array.Length == 0)
{
return new T[0]; // don't use Utils.EmptyArray, always return a new array
#pragma warning disable CA1825 // Avoid zero-length array allocations
// Don't use Array.Empty<T>(); always return a new instance.
return new T[0];
#pragma warning restore CA1825 // Avoid zero-length array allocations
}

var context = LocalPipeline.GetExecutionContextFromTLS();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3228,7 +3228,10 @@ internal static object Multiply(IEnumerator enumerator, uint times)

if (originalList.Count == 0)
{
return new object[0]; // don't use Utils.EmptyArray, always return a new array
#pragma warning disable CA1825 // Avoid zero-length array allocations
// Don't use Array.Empty<object>(); always return a new instance.
return new object[0];
#pragma warning restore CA1825 // Avoid zero-length array allocations
}

return ArrayOps.Multiply(originalList.ToArray(), times);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3814,7 +3814,7 @@ private static object ConvertValueToKind(object value, RegistryValueKind kind)
value,
typeof(byte[]),
CultureInfo.CurrentCulture)
: new byte[] { };
: Array.Empty<byte>();
break;

case RegistryValueKind.DWord:
Expand Down Expand Up @@ -3851,7 +3851,7 @@ private static object ConvertValueToKind(object value, RegistryValueKind kind)
value,
typeof(string[]),
CultureInfo.CurrentCulture)
: new string[] { };
: Array.Empty<string>();
break;

case RegistryValueKind.QWord:
Expand Down
Loading