Skip to content

JIT: poor CQ for list enumeration constructs #9304

@AndyAyersMS

Description

@AndyAyersMS

From some discussion over in dotnet/csharplang#1085. Both the for and foreach cases have suboptimal CQ.

In the for case there is a redundant branch in the inner loop.

In the foreach case the enumerator's MoveNext is not inlined. If we force this and MoveNextRare inline then the jit is not able to optimize away the version check overhead in the inner loop.

Without the inlines the foreach is rougly 2.9x slower than the for; with the inlines this drops to 1.3x or so.

    static int SumFor(List<int> l)
    {
        int result = 0;

        for (int i = 0; i < l.Count; i++)
        {
            result += l[i];
        }

        return result;
    }
    
    static int SumForeach(List<int> l)
    {
        int result = 0;

        foreach (int i in l)
        {
            result += i;
        }

        return result;
    }

category:cq
theme:basic-cq
skill-level:expert
cost:medium
impact:large

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additionsoptimizationtenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions