Skip to content

Inline for lambda #13679

Open
Open

Description

@blowin

Now:

public int Calculate(Func<int, int, int> c)
{
 return c(10, 20);   
}

public int CallCalculate() 
{
 return Calculate((x1, x2) => x1 + x2);   
}

Compile to:

private sealed class <>c
{
    public static readonly <>c <>9 = new <>c();

    public static Func<int, int, int> <>9__1_0;

    internal int <CallCalculate>b__1_0(int x1, int x2)
    {
        return x1 + x2;
    }
}

public int Calculate(Func<int, int, int> c)
{
    return c(10, 20);
}

public int CallCalculate()
{
    return Calculate(<>c.<>9__1_0 ?? (<>c.<>9__1_0 = new Func<int, int, int>(<>c.<>9.<CallCalculate>b__1_0)));
}

Is inline lambda possible?
Like this:

public int Calculate()
{
 return 10 + 20;   
}

public int CallCalculate() 
{
 return Calculate();   
}

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

Activity

  1. transferred this issue fromdotnet/coreclron Jan 31, 2020
  2. added this to the Future milestone on Jan 31, 2020
  3. added
    JitUntriaged
    on Oct 28, 2020
  4. am11 commented on Nov 10, 2022

    @am11
    Member

    #44610 is planned for 8.0. @EgorBo, does #44610 cover these kind of (simple callback) calls for inlining?

    current codegen: https://godbolt.org/z/Wc56zrcoq

  5. EgorBo commented on Nov 10, 2022

    @EgorBo
    Member

    #44610 is planned for 8.0. @EgorBo, does #44610 cover these kind of (simple callback) calls for inlining?

    current codegen: https://godbolt.org/z/Wc56zrcoq

    I think the current plan is to rely on guarded devirtualization for delegates for such things. Pure inlining is complicated because of that cache machinery Roslyn emits - hard to track types.

  6. am11 commented on Nov 10, 2022

    @am11
    Member

    Ah, I see. Perhaps in some future milestone, linker (or some other pre-processing step) can turn Func and Action into regular methods; at least for simple / non-capturing cases? Couple of attributes in IL are emitted to identify the compiler-generated machinery (more can be added if needed to disambiguate the patterns).

    For reference, C++ lambdas: https://godbolt.org/z/b4GThT8z6 and Swift closures: https://godbolt.org/z/8jf1jxq38 get CallCalculate optimized to return 30.

  7. removed
    JitUntriaged
    on Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions