Skip to content

[Idea] Batch the nested lambda compilation together #452

@dadhi

Description

@dadhi

Migrated from the DryIoc dadhi/DryIoc#467

The #465 showing the most time of the compilation is spent on the creating the DynamicMethod and actually compiling this.

Some delegate expressions are small like (IResolverContext r) => new A(), so the most time is spent on the wrapping it in the compilation unit, and we have many of these.

To improve the time and memory consumption, the idea is to batch those lambdas together like below and compile them as one DynamicMethod:

byte tag = 0;
(IResolverContext r) => tag switch {
   0 => new A(),
   1 => new B(),
   2 => new C(new X(), new Y())
   //... etc.
}

The tag is something that can be set in the lambda closure object. In order to do so, I will create an open delegate via dynamicMethod.CreateDelegate(DelegateType, null) and pass the preconfigured closure as its first argument.

Pros:

  • Single compilation unit instead of many with faster compile time and less memory footprint

Cons:

  • Slight delegate invocation performance decrease (only for the subset of the cases and only about the nested lambdas)
  • More complex implementation logic

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions