wpf
wpf copied to clipboard
Replace ArrayList in ItemContainerGenerator with List<EmptyGroupItem>
Description
Replaces ArrayList with List<EmptyGroupItem> to hold empty group items when needed. Improves code quality by swapping to generic collection and decreases memory footprint.
Sample benchmark showing difference between ArrayList and List<EmptyGroupItem>.
10 additions comparison
| Method | Mean [ns] | Error [ns] | StdDev [ns] | Gen0 | Code Size [B] | Gen1 | Allocated [B] |
|---|---|---|---|---|---|---|---|
| Original | 103.59 ns | 2.013 ns | 3.578 ns | 0.0483 | 740 B | 0.0001 | 808 B |
| PR__EDIT | 95.02 ns | 1.907 ns | 2.480 ns | 0.0483 | 342 B | 0.0001 | 808 B |
Benchmark code
[Benchmark]
public void Original()
{
ArrayList? _alEmptyGroupItems = null;
for (int i = 0; i < 10; i++)
BenchV1(ref _alEmptyGroupItems);
}
[Benchmark]
public void PR__EDIT()
{
List<EmptyGroupItem>? _emptyGroupItems = null;
for (int i = 0; i < 10; i++)
BenchV2(ref _emptyGroupItems);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void BenchV1(ref ArrayList? _alEmptyGroupItems)
{
EmptyGroupItem emptyGroupItem = new EmptyGroupItem();
if (_alEmptyGroupItems == null)
_alEmptyGroupItems = new ArrayList();
_alEmptyGroupItems.Add(emptyGroupItem);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void BenchV2(ref List<EmptyGroupItem>? _emptyGroupItems)
{
EmptyGroupItem emptyGroupItem = new EmptyGroupItem();
if (_emptyGroupItems == null)
_emptyGroupItems = new();
_emptyGroupItems.Add(emptyGroupItem);
}
public class EmptyGroupItem
{
public string? Item1 { get; set; }
public Int32? Item2 { get; set; }
public Int32? Item3 { get; set; }
public List<int>? Item4 { get; set; }
}
Customer Impact
Improved performance, getting rid of ArrayList.
Regression
No.
Testing
Local build.
Risk
None, changes are minimal.
Microsoft Reviewers: Open in CodeFlow
/azp run
Azure Pipelines successfully started running 1 pipeline(s).
/azp run
Azure Pipelines successfully started running 1 pipeline(s).
@h3xds1nz Thank you for your contribution!
@siagupta0202 Thank you for including it 👍