Skip to content

Commit 7f6d4aa

Browse files
authored
Avoid zero-length array allocations and storing them in static fields (CA1825) (#10267)
* Avoid zero-length array allocations * mimic #4027 for XamlMemberInvoker * Avoid one more unnecessary static pointer holder
1 parent bd0c311 commit 7f6d4aa

File tree

16 files changed

+39
-52
lines changed

16 files changed

+39
-52
lines changed

src/Microsoft.DotNet.Wpf/src/.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ dotnet_diagnostic.CA1822.severity = suggestion
9292
# CA1823: Avoid unused private fields
9393
dotnet_diagnostic.CA1823.severity = suggestion
9494

95-
# CA1825: Avoid zero-length array allocations
96-
dotnet_diagnostic.CA1825.severity = suggestion
97-
9895
# CA1834: Use StringBuilder.Append(char) for single character strings
9996
dotnet_diagnostic.CA1834.severity = suggestion
10097

src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/MarkupCompiler/MarkupCompiler.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -978,11 +978,10 @@ internal void ConnectStyleEvent(XamlClrEventNode xamlClrEventNode)
978978

979979
// eventSetter = new EventSetter();
980980
//
981-
CodeExpression[] esParams = {};
982981
CodeVariableReferenceExpression cvreES = new CodeVariableReferenceExpression(EVENTSETTER);
983982
CodeAssignStatement casES = new CodeAssignStatement(cvreES,
984983
new CodeObjectCreateExpression(KnownTypes.Types[(int)KnownElements.EventSetter],
985-
esParams));
984+
Array.Empty<CodeExpression>()));
986985

987986
// eventSetter.Event = Button.ClickEvent;
988987
//
@@ -3156,9 +3155,8 @@ private CodeVariableReferenceExpression GenerateAppInstance(CodeMemberMethod cmm
31563155
//
31573156
CodeObjectCreateExpression coce;
31583157
CodeVariableReferenceExpression cvre = new CodeVariableReferenceExpression(APPVAR);
3159-
CodeExpression[] ctorParams = {};
31603158

3161-
coce = new CodeObjectCreateExpression(appClassName, ctorParams);
3159+
coce = new CodeObjectCreateExpression(appClassName, Array.Empty<CodeExpression>());
31623160

31633161
CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement(appClassName, APPVAR, coce);
31643162

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/PtsTable/RowSpanVector.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44

@@ -155,7 +155,6 @@ internal void GetNextAvailableRange(out int firstAvailableIndex, out int firstOc
155155
/// <returns>Array of cells. May be empty</returns>
156156
internal void GetSpanCells(out TableCell[] cells, out bool isLastRowOfAnySpan)
157157
{
158-
cells = s_noCells;
159158
isLastRowOfAnySpan = false;
160159

161160
// iterate the tail of entries (if any)
@@ -203,10 +202,13 @@ internal void GetSpanCells(out TableCell[] cells, out bool isLastRowOfAnySpan)
203202

204203
_size = j + 1;
205204
}
206-
207-
#if DEBUG
205+
else
206+
{
207+
cells = Array.Empty<TableCell>();
208+
}
209+
#if DEBUG
208210
_index = -1;
209-
#endif // DEBUG
211+
#endif
210212
}
211213

212214
#endregion Internal Methods
@@ -264,8 +266,6 @@ private void InflateCapacity()
264266
private int _size; // current size of the list
265267
private int _index; // index used for iteration (GetFirst / GetNext)
266268
private const int c_defaultCapacity = 8; // default capacity
267-
private static TableCell[] s_noCells = Array.Empty<TableCell>(); // empty array RowSpanVector returns to rows that do not
268-
// have row spanned cells
269269
#endregion Private Fields
270270

271271
//------------------------------------------------------

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/GuidGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static string GetSignature(Type type)
3333
var sigMethod = helperType.GetMethod("GetGuidSignature", BindingFlags.Static | BindingFlags.Public);
3434
if (sigMethod != null)
3535
{
36-
return (string)sigMethod.Invoke(null, new Type[] { });
36+
return (string)sigMethod.Invoke(null, Array.Empty<Type>());
3737
}
3838
}
3939

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GridViewAutomationPeer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ IRawElementProviderSimple[] ITableProvider.GetColumnHeaders()
146146
return array.ToArray();
147147
}
148148

149-
return new IRawElementProviderSimple[0] ;
149+
return Array.Empty<IRawElementProviderSimple>();
150150
}
151151

152152
/// <summary>

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/InkCanvas.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@ internal bool ClearSelectionRaiseSelectionChanging()
23082308
//
23092309
// attempt to clear selection
23102310
//
2311-
ChangeInkCanvasSelection(new StrokeCollection(), new UIElement[]{});
2311+
ChangeInkCanvasSelection(new StrokeCollection(), Array.Empty<UIElement>());
23122312

23132313
return !InkCanvasSelection.HasSelection;
23142314
}
@@ -2324,7 +2324,7 @@ internal void ClearSelection(bool raiseSelectionChangedEvent)
23242324
if ( InkCanvasSelection.HasSelection )
23252325
{
23262326
// Reset the current selection
2327-
CoreChangeSelection(new StrokeCollection(), new UIElement[] { }, raiseSelectionChangedEvent);
2327+
CoreChangeSelection(new StrokeCollection(), Array.Empty<UIElement>(), raiseSelectionChangedEvent);
23282328
}
23292329
}
23302330

@@ -2509,7 +2509,7 @@ private UIElement[] ValidateSelectedElements(IEnumerable<UIElement> selectedElem
25092509
{
25102510
if (selectedElements == null)
25112511
{
2512-
return new UIElement[]{};
2512+
return Array.Empty<UIElement>();
25132513
}
25142514

25152515
List<UIElement> elements = new List<UIElement>();

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Slider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ private CustomPopupPlacement[] AutoToolTipCustomPlacementCallback(Size popupSize
947947
}
948948

949949
default:
950-
return new CustomPopupPlacement[]{};
950+
return Array.Empty<CustomPopupPlacement>();
951951
}
952952
}
953953

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextSchema.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ internal static DependencyProperty[] GetNoninheritableProperties(Type type)
501501
}
502502
else if (typeof(LineBreak).IsAssignableFrom(type))
503503
{
504-
return _emptyPropertyList;
504+
return Array.Empty<DependencyProperty>();
505505
}
506506
else if (typeof(Floater).IsAssignableFrom(type))
507507
{
@@ -559,7 +559,7 @@ internal static DependencyProperty[] GetNoninheritableProperties(Type type)
559559
}
560560

561561
Invariant.Assert(false, "We do not expect any unknown elements derived directly from TextElement. Schema must have been checking for that");
562-
return _emptyPropertyList; // to make compiler happy
562+
return Array.Empty<DependencyProperty>(); // to make compiler happy
563563
}
564564

565565
// Compares two values for equality
@@ -1201,9 +1201,6 @@ private static bool AreBrushesEqual(Brush brush1, Brush brush2)
12011201
UIElement.AllowDropProperty,
12021202
};
12031203

1204-
// Empty property list
1205-
private static readonly DependencyProperty[] _emptyPropertyList = new DependencyProperty[] { };
1206-
12071204
// Structural property list.
12081205
// NB: Existing code depends on these being inheritable properties.
12091206
private static readonly DependencyProperty[] _structuralCharacterProperties = new DependencyProperty[]

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4432,20 +4432,15 @@ internal static string[] GetDefaultAssemblyNames()
44324432
/// </summary>
44334433
internal static NamespaceMapEntry[] GetDefaultNamespaceMaps()
44344434
{
4435-
return (NamespaceMapEntry[])_defaultNamespaceMapTable.Clone();
4435+
return Array.Empty<NamespaceMapEntry>();
44364436
}
44374437

44384438
#endregion Properties
44394439

44404440
#region Data
44414441

44424442
// array of our defaultAssemblies.
4443-
private static readonly string[] _defaultAssemblies = {"WindowsBase", "PresentationCore", "PresentationFramework"};
4444-
4445-
// array of namespaceMaps the map an xmlns namespaceURI
4446-
// to the assembly and urtNamespace to search in when resolving the xml
4447-
4448-
private static readonly NamespaceMapEntry[] _defaultNamespaceMapTable = { };
4443+
private static readonly string[] _defaultAssemblies = { "WindowsBase", "PresentationCore", "PresentationFramework" };
44494444

44504445
#endregion Data
44514446
}

src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlMemberInvoker.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
#nullable disable
@@ -12,7 +12,6 @@ public class XamlMemberInvoker
1212
{
1313
private static XamlMemberInvoker s_Directive;
1414
private static XamlMemberInvoker s_Unknown;
15-
private static object[] s_emptyObjectArray = Array.Empty<object>();
1615

1716
private XamlMember _member;
1817
private NullableReference<MethodInfo> _shouldSerializeMethod;
@@ -64,7 +63,7 @@ public virtual object GetValue(object instance)
6463
}
6564
else
6665
{
67-
return UnderlyingGetter.Invoke(instance, s_emptyObjectArray);
66+
return UnderlyingGetter.Invoke(instance, Array.Empty<object>());
6867
}
6968
}
7069

0 commit comments

Comments
 (0)