Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace ArrayList in ItemContainerGenerator with List<EmptyGroupItem> #9432

Merged
merged 2 commits into from
Jan 2, 2025

Conversation

h3xds1nz
Copy link
Contributor

@h3xds1nz h3xds1nz commented Jul 20, 2024

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 h3xds1nz requested review from a team as code owners July 20, 2024 19:34
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Jul 20, 2024
@rchauhan18
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@siagupta0202
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@siagupta0202 siagupta0202 merged commit aa1896f into dotnet:main Jan 2, 2025
8 checks passed
@siagupta0202
Copy link
Contributor

@h3xds1nz Thank you for your contribution!

@h3xds1nz
Copy link
Contributor Author

h3xds1nz commented Jan 2, 2025

@siagupta0202 Thank you for including it 👍

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants