wpf icon indicating copy to clipboard operation
wpf copied to clipboard

Replace ArrayList in ItemContainerGenerator with List<EmptyGroupItem>

Open h3xds1nz opened this issue 1 year ago • 2 comments

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

h3xds1nz avatar Jul 20 '24 19:07 h3xds1nz

/azp run

rchauhan18 avatar Jul 23 '24 10:07 rchauhan18

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Jul 23 '24 10:07 azure-pipelines[bot]

/azp run

siagupta0202 avatar Jan 02 '25 06:01 siagupta0202

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Jan 02 '25 06:01 azure-pipelines[bot]

@h3xds1nz Thank you for your contribution!

siagupta0202 avatar Jan 02 '25 16:01 siagupta0202

@siagupta0202 Thank you for including it 👍

h3xds1nz avatar Jan 02 '25 16:01 h3xds1nz