Skip to content

Help with improving JIT for Range GetEnumerator #40770

Closed

Description

@YairHalberstadt

I'm using extension GetEnumerator (coming in C# 9) to implement foreach on Range. See https://github.com/YairHalberstadt/RangeForeach.

I'm using sharplab to check the jit output.

I want everybody to be able to rewrite their for loops to foreach loops without having to think about any potential performance impact. Therefore ideally we'd want M1 and M2 in:

public static class Program {
    [MethodImpl(MethodImplOptions.AggressiveOptimization)]
    public static void M1() {
        foreach (var i in 1..10)
            Console.WriteLine(i);
    }
    [MethodImpl(MethodImplOptions.AggressiveOptimization)]
    public static void M2() {
        for (int i = 1; i<10; i++)
            Console.WriteLine(i);
    }
}

To have identical asm.

My current implementation is:

using System;
using System.Runtime.CompilerServices;


public static class Program {
    [MethodImpl(MethodImplOptions.AggressiveOptimization)]
    public static void M1() {
        foreach (var i in 1..10)
            Console.WriteLine(i);
    }
    [MethodImpl(MethodImplOptions.AggressiveOptimization)]
    public static void M2() {
        for (int i = 1; i<10; i++)
            Console.WriteLine(i);
    }
}

public static class RangeExtensions
{
    public struct RangeEnumerator
    {
        public RangeEnumerator(int start, int end) => (Current, _end) = (start - 1, end);
        public int Current { get; private set; }
        private int _end;
        public bool MoveNext() => ++Current < _end;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static RangeEnumerator GetEnumerator(this Range range)
    {
        if (range.Start.IsFromEnd || range.End.IsFromEnd)
            ThrowIsFromEnd();

        if (range.Start.Value > range.End.Value)
            ThrowStartIsGreaterThanEnd();

        return new RangeEnumerator(range.Start.Value, range.End.Value);

        static void ThrowIsFromEnd() => throw new ArgumentException("range start and end must not be from end");
        static void ThrowStartIsGreaterThanEnd() => throw new ArgumentException("start is greater than end");
    }
}

Jit is:

Program.M1()
    L0000: push ebp
    L0001: mov ebp, esp
    L0003: push edi
    L0004: push esi
    L0005: mov ecx, 1
    L000a: mov eax, 0xa
    L000f: cmp ecx, eax
    L0011: jg short L0030
    L0013: mov esi, 1
    L0018: mov edi, 0xa
    L001d: dec esi
    L001e: jmp short L0027
    L0020: mov ecx, esi
    L0022: call System.Console.WriteLine(Int32)
    L0027: inc esi
    L0028: cmp esi, edi
    L002a: jl short L0020
    L002c: pop esi
    L002d: pop edi
    L002e: pop ebp
    L002f: ret
    L0030: call dword ptr [0x2622c9fc]
    L0036: int3

Program.M2()
    L0000: push ebp
    L0001: mov ebp, esp
    L0003: push esi
    L0004: mov esi, 1
    L0009: mov ecx, esi
    L000b: call System.Console.WriteLine(Int32)
    L0010: inc esi
    L0011: cmp esi, 0xa
    L0014: jl short L0009
    L0016: pop esi
    L0017: pop ebp
    L0018: ret

To be honest I'm pretty impressed by how much the JIT is managing to do here, but it's still approximately twice as long.

The main thing that isn't getting optimized away is the if (range.Start.Value > range.End.Value) check.
Any ideas if I can optimize this further, or if the JIT might be able to do better here?

category:cq
theme:optimization
skill-level:expert
cost:large

Activity

  1. added
    tenet-performance
    on Aug 13, 2020
  2. added
    area-CodeGen-coreclr
    untriaged
    on Aug 13, 2020
  3. changed the title [-]Help with improving JIT for method[/-] [+]Help with improving JIT for Range GetEnumerator[/+] on Aug 13, 2020
  4. SingleAccretion commented on Aug 13, 2020

    @SingleAccretion
    Contributor

    It appears you are using x86 JIT. That said, x64 code with my recent-ish version of the runtime, obtained via Disasmo, is not substantially different:

    01:
           push     rdi
           push     rsi
           sub      rsp, 40
    02:
           mov      ecx, 1
           mov      eax, 10
           cmp      ecx, eax
           jg       SHORT 07
    03:
           mov      esi, 1
           mov      edi, 10
           dec      esi
           jmp      SHORT 05
    04:
           mov      ecx, esi
           call     System.Console:WriteLine(int)
    05:
           inc      esi
           cmp      esi, edi
           jl       SHORT 04
    06:
           add      rsp, 40
           pop      rsi
           pop      rdi
           ret      
    07:
           call     RangeExtensions:<GetEnumerator>g__ThrowStartIsGreaterThanEnd|1_1()
           int3 

    I was able to make the comparison go away with this hack, but I do not know if it is good enough in the general case:

    if (range.Start.GetValue() > range.End.GetValue())
                ThrowStartIsGreaterThanEnd();
    
    private static int GetValue(this Index index) => Unsafe.As<Index, int>(ref index);
    01:
           push     rdi
           push     rsi
           sub      rsp, 40
    02:
           mov      esi, 1
           mov      edi, 10
           dec      esi
           jmp      SHORT 04
    03:
           mov      ecx, esi
           call     System.Console:WriteLine(int)
    04:
           inc      esi
           cmp      esi, edi
           jl       SHORT 03
    05:
           add      rsp, 40
           pop      rsi
           pop      rdi
           ret
  5. AndyAyersMS commented on Aug 13, 2020

    @AndyAyersMS
    Member

    Thanks for the report. #11848 called out a number of issues with the performance of Range. Good news is that most of these have been addressed over time, but it looks like there's more to do.

    Marking as future since we're closing down work for 5.0

    cc @dotnet/jit-contrib

  6. removed
    untriaged
    on Aug 13, 2020
  7. added this to the Future milestone on Aug 13, 2020
  8. YairHalberstadt commented on Aug 13, 2020

    @YairHalberstadt
    ContributorAuthor

    That's great @AndyAyersMS

    When we remove all usage of range, and just pass in the start and end directly:

    using System;
    using System.Runtime.CompilerServices;
    
    namespace System.Runtime.CompilerServices {class IsExternalInit{}}
    public static class Program {
        [MethodImpl(MethodImplOptions.AggressiveOptimization)]
        public static void M1() {
            var enumerator = RangeExtensions.GetEnumerator(1, 10);
            while(enumerator.MoveNext())
                Console.WriteLine(enumerator.Current);
        }
        [MethodImpl(MethodImplOptions.AggressiveOptimization)]
        public static void M2() {
            for (int i = 1; i<10; i++)
                Console.Write(i);
        }
    }
    
    public static class RangeExtensions
    {
        public struct RangeEnumerator
        {
            public RangeEnumerator(int start, int end) => (Current, _end) = (start - 1, end);
            public int Current { get; private set; }
            private int _end;
            public bool MoveNext() => ++Current < _end;
        }
    
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static RangeEnumerator GetEnumerator(int start, int end)
        {
            if (start > end)
                ThrowStartIsGreaterThanEnd();
                
            return new RangeEnumerator(start, end);
    
            static void ThrowStartIsGreaterThanEnd() => throw new ArgumentException("start is greater than end");
        }
    }

    X86 does a pretty good job:

    Program.M1()
        L0000: push ebp
        L0001: mov ebp, esp
        L0003: push esi
        L0004: xor esi, esi
        L0006: jmp short L000f
        L0008: mov ecx, esi
        L000a: call System.Console.WriteLine(Int32)
        L000f: inc esi
        L0010: cmp esi, 0xa
        L0013: jl short L0008
        L0015: pop esi
        L0016: pop ebp
        L0017: ret
    
    Program.M2()
        L0000: push ebp
        L0001: mov ebp, esp
        L0003: push esi
        L0004: mov esi, 1
        L0009: mov ecx, esi
        L000b: call System.Console.Write(Int32)
        L0010: inc esi
        L0011: cmp esi, 0xa
        L0014: jl short L0009
        L0016: pop esi
        L0017: pop ebp
        L0018: ret

    x64 meanwhile is still not great:

    Program.M1()
        L0000: sub rsp, 0x38
        L0004: xor eax, eax
        L0006: mov [rsp+0x30], rax
        L000b: xor ecx, ecx
        L000d: mov [rsp+0x28], ecx
        L0011: mov [rsp+0x2c], ecx
        L0015: mov [rsp+0x28], ecx
        L0019: mov dword ptr [rsp+0x2c], 0xa
        L0021: mov rcx, [rsp+0x28]
        L0026: mov [rsp+0x30], rcx
        L002b: jmp short L0036
        L002d: mov ecx, [rsp+0x30]
        L0031: call System.Console.WriteLine(Int32)
        L0036: mov ecx, [rsp+0x30]
        L003a: inc ecx
        L003c: mov [rsp+0x30], ecx
        L0040: cmp ecx, [rsp+0x34]
        L0044: jl short L002d
        L0046: add rsp, 0x38
        L004a: ret
    
    Program.M2()
        L0000: push rsi
        L0001: sub rsp, 0x20
        L0005: mov esi, 1
        L000a: mov ecx, esi
        L000c: call System.Console.Write(Int32)
        L0011: inc esi
        L0013: cmp esi, 0xa
        L0016: jl short L000a
        L0018: add rsp, 0x20
        L001c: pop rsi
        L001d: ret
  9. SingleAccretion commented on Aug 13, 2020

    @SingleAccretion
    Contributor

    x64 meanwhile is still not great

    This is not the codegen I am getting. I too saw it on sharplab and was a bit puzzled. But it does indeed seem like .NET 5 is that much better than .NET Core 3.1 in this case.

  10. added
    JitUntriaged
    on Oct 28, 2020
  11. modified the milestones: Future, 6.0.0 on Nov 11, 2020
  12. removed
    JitUntriaged
    on Nov 11, 2020
  13. added
    needs-further-triage
    on Mar 23, 2021
  14. AndyAyersMS commented on Mar 30, 2021

    @AndyAyersMS
    Member

    @YairHalberstadt have you had a chance to look at 5.0 (or even 6.0) codegen here? For the second example I am seeing:

    ; Core CLR v5.0.421.11614 on amd64
    
    Program.M1()
        L0000: push rsi
        L0001: sub rsp, 0x20
        L0005: xor esi, esi
        L0007: jmp short L0010
        L0009: mov ecx, esi
        L000b: call System.Console.WriteLine(Int32)
        L0010: inc esi
        L0012: cmp esi, 0xa
        L0015: jl short L0009
        L0017: add rsp, 0x20
        L001b: pop rsi
        L001c: ret
    
    Program.M2()
        L0000: push rsi
        L0001: sub rsp, 0x20
        L0005: mov esi, 1
        L000a: mov ecx, esi
        L000c: call System.Console.Write(Int32)
        L0011: inc esi
        L0013: cmp esi, 0xa
        L0016: jl short L000a
        L0018: add rsp, 0x20
        L001c: pop rsi
        L001d: ret

    Actually using range here still has challenges -- when we eliminate upstream control flow in assertion prop we don't clean up downstream dead phis, and so fail to propagate constants. LSRA then manages to preference and coalesce a bunch of temp lifetimes and the final codegen has obvious known-outcome compares that don't get optimized, as in the example shown above by @SingleAccretion

  15. AndyAyersMS commented on Apr 9, 2021

    @AndyAyersMS
    Member

    Ok, I am going to mark this as future.

    There is still the interesting case where we clean up control flow during optimization and then fail to benefit from that like we should. We see this a lot with index and range.

  16. removed
    needs-further-triage
    on Apr 9, 2021
  17. modified the milestones: 6.0.0, Future on Apr 9, 2021
  18. AndyAyersMS commented on Jun 5, 2026

    @AndyAyersMS
    Member

    Note

    AI-assisted (Copilot CLI).

    Re-checked on current main (arm64). Substantial improvement since 2021 (codegen ~60 B for the variable-N variant vs. ~150+ B then), but the foreach-over-Range form is still ~2× slower than the equivalent for loop. Root cause is now isolated and there is a small one-line codegen tweak that recovers ~25% of the gap by itself.

    Numbers (Apple M4 Max, arm64, DOTNET_TieredCompilation=0, N=100, 5 M outer iterations)

    Variant Codegen Inner loop ns/call
    for (int i = 1; i < n; i++) 60 B 4 instrs 23.4
    foreach (...) over enumerator with plain int Current field 60 B 4 instrs 27.2
    foreach (...) over enumerator with int Current { get; private set; } 124 B 5 instrs 48.7

    The plain-field variant produces identical codegen to the for loop. The auto-property version is the slow one.

    Root cause in the JIT

    Slow inner loop:

    add  w1, w1, w2     ; s += Current
    add  w2, w3, #1     ; tmp = Current + 1
    sxtw w3, w2         ; Current = sxtw(tmp)   ← unnecessary
    cmp  w3, w0
    blt  .loop
    

    The auto-property private set inlines as STOREIND(LCL_ADDR(struct), value). After struct promotion, the destination is the promoted <Current>k__BackingField (TYP_INT) and the IR contains no CAST node — yet arm64 codegen still emits sxtw for the int-to-int register move. Trace:

    • genCodeForStoreLclVar → inst_Mov_Extend(targetType=TYP_INT, ...)
    • ins_Move_Extend(TYP_INT, srcInReg=true) in src/coreclr/jit/instr.cpp:2008-2011 returns INS_sxtw for srcType == TYP_INT.

    For a TYP_INT-to-TYP_INT reg-reg move on arm64, INS_mov (encoded as orr Wd, WZR, Wn) is sufficient: subsequent 32-bit ops ignore upper bits, and any genuine int→long widening has an explicit CAST in the IR that takes a different codegen path.

    Local patch + measurement

    Single-line change:

    // src/coreclr/jit/instr.cpp, in ins_Move_Extend, arm64 signed branch:
    else if (srcType == TYP_INT) ins = INS_sxtw;   // → ins = INS_mov;

    Rebuilt checked arm64 JIT, swapped into Core_Root, re-measured:

    Before After Δ
    M1 (auto-prop) ~48 ns ~36 ns −25%
    M1b (plain field) ~27 ns ~27 ns —
    M2 (for loop) ~24 ns ~24 ns —

    Quick sanity for sign-extension preservation: (long)int.MinValue, Neg(int.MinValue), etc. all still produce correct results (the explicit CAST path is unaffected).

    The remaining ~12 ns gap from M2 is from V06 / V07 not being register-coalesced (an extra mov w3, w2 per iteration). That is a separate, longstanding LSRA limitation.

    Suggestion

    The sxtw → mov change is a clean isolated improvement and likely useful across many int-heavy arm64 inner loops; I will open a focused issue for it and link back here. ins_Move_Extend is called from several sites (codegenarm64.cpp:3020, codegencommon.cpp:7227, hwintrinsiccodegenarm64.cpp), so the other callers should be reviewed before the change is more than a proof of concept. SPMI diff would confirm broader impact.

  19. AndyAyersMS commented on Jun 5, 2026

    @AndyAyersMS
    Member

    Filed the focused JIT codegen issue as #129052.

  20. AndyAyersMS commented on Jun 7, 2026

    @AndyAyersMS
    Member

    Think we can close this one now.

  21. locked and limited conversation to collaborators on Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrtenet-performance

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions