Skip to content

Optimize delegate creation from effectively 'static' local functions #66057

@controlflow

Description

@controlflow

Version Used:

4.4.0

Steps to Reproduce:

Compile the following code:

var func1 = StaticMethodsOptimization.StaticMethod;
var func2 = LocalFunction; // ALLOCATES
var func3 = StaticLocalFunction;
var func4 = () => { };
var func5 = static () => { };

func1();
func2();
func3();
func4();
func5();

void LocalFunction() { } // no closure
static void StaticLocalFunction() { }

public static class StaticMethodsOptimization {
  public static void StaticMethod() { }
}

Expected Behavior:

All delegate instances cached.

Actual Behavior:

var func2 = LocalFunction; line allocates new delegate instance on each evaluation despite LocalFunction local function being effectively static.
This feels suboptimal and non-symmetric with the behavior of lambda expression.

[jcouv update:] relates to recent work on caching the delegate for static methods #58288

Metadata

Metadata

Assignees

Labels

4 - In ReviewA fix for the issue is submitted for review.Area-CompilersCode Gen QualityRoom for improvement in the quality of the compiler's generated code

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions