Skip to content

Conversation

@stephentoub
Copy link
Member

No description provided.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@stephentoub
Copy link
Member Author

@EgorBot -arm -amd -intel

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

[MemoryDiagnoser(false)]
public class Bench
{
    [Benchmark]
    [ArgumentsSource(nameof(GetLists))]
    public int SumList(List<int> list)
    {
        int sum = 0;
        foreach (int item in list)
        {
            sum += item;
        }
        return sum;
    }

    [Benchmark]
    [ArgumentsSource(nameof(GetLists))]
    public int SumEnumerable(IEnumerable<int> list)
    {
        int sum = 0;
        foreach (int item in list)
        {
            sum += item;
        }
        return sum;
    }

    public static IEnumerable<List<int>> GetLists() =>
        from count in new int[] { 1, 10, 1_000 }
        select Enumerable.Range(0, count).ToList();
}

@stephentoub
Copy link
Member Author

@EgorBot -arm -amd -intel

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Runtime.CompilerServices;

BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

[DisassemblyDiagnoser]
[MemoryDiagnoser(false)]
public class Bench
{
    [Benchmark]
    [ArgumentsSource(nameof(GetLists))]
    public int SumList(List<string> list)
    {
        int sum = 0;
        foreach (var item in list)
        {
            sum += Process(item);
        }
        return sum;
    }

    [Benchmark]
    [ArgumentsSource(nameof(GetLists))]
    public int SumEnumerable(IEnumerable<string> list)
    {
        int sum = 0;
        foreach (var item in list)
        {
            sum += Process(item);
        }
        return sum;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static int Process(string item) => item.Length;

    public static IEnumerable<List<string>> GetLists() =>
        from count in new int[] { 1, 10, 1_000 }
        select Enumerable.Range(0, count).Select(i => i.ToString()).ToList();
}

@stephentoub
Copy link
Member Author

@EgorBot -arm -amd -intel

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

[MemoryDiagnoser(false)]
public class Bench
{
    [Benchmark]
    [ArgumentsSource(nameof(GetLists))]
    public int SumList(List<int> list)
    {
        int sum = 0;
        foreach (int item in list)
        {
            sum += item;
        }
        return sum;
    }

    [Benchmark]
    [ArgumentsSource(nameof(GetLists))]
    public int SumEnumerable(IEnumerable<int> list)
    {
        int sum = 0;
        foreach (int item in list)
        {
            sum += item;
        }
        return sum;
    }

    public static IEnumerable<List<int>> GetLists() =>
        from count in new int[] { 1, 10, 1_000 }
        select Enumerable.Range(0, count).ToList();
}

@stephentoub
Copy link
Member Author

This appears to be a way to achieve the desired goal of helping the boxed case and not negatively impacting the non-boxed case.
EgorBot/runtime-utils#372
But it'd be better if the JIT could handle this. I don't think we want to add another enumerator implementation like this to list.

cc: @EgorBo, @AndyAyersMS

@stephentoub stephentoub closed this Jun 3, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jul 3, 2025
@stephentoub stephentoub deleted the optlistenum branch December 12, 2025 23:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants