Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegates are poorly inlined and optimized #63425

Closed
deeprobin opened this issue Jan 6, 2022 · 3 comments
Closed

Delegates are poorly inlined and optimized #63425

deeprobin opened this issue Jan 6, 2022 · 3 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue untriaged New issue has not been triaged by the area owner

Comments

@deeprobin
Copy link
Contributor

C# Code

Example without using delegate

    public int AddFunc(int a, int b) {
        return a + b;
    }

Example with delegate

    public int DelegateFunc(int a, int b) {
        Func<int, int, int> e = delegate(int c, int d) {
            return a + b;
        };
        return e(a, b);
    }

Analysis

See [sharplab example](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKGIGYACMhgYQYG8aHunGBLAHYYGAQQAmY4gFYAFIOHY0DeQ2ABKDlx7biAdgbYGAalUBuLdwC+FhjfrKhDACIwANjADm2DDGlzHig7C6prU2jpSADzySjFBAHwMMAwAvAxibp7eMP7CYLGOYhqcYeFlegbGZjbalual5fo5ger1tTSWQA===

Stopwatch benchmark without Benchmark.NET

On the one hand, of course, all the code to instantiate a delegate function is created, however, the actual code executed is also very poorly optimized.

Assembly result

AddFunc

    L0000: lea eax, [rdx+r8]
    L0004: ret

Content of Func<int, int, int>

    L0000: mov eax, [rcx+8]
    L0003: add eax, [rcx+0xc]
    L0006: ret
@deeprobin deeprobin added the tenet-performance Performance related issue label Jan 6, 2022
@dotnet-issue-labeler dotnet-issue-labeler bot added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels Jan 6, 2022
@ghost
Copy link

ghost commented Jan 6, 2022

Tagging subscribers to this area: @JulieLeeMSFT
See info in area-owners.md if you want to be subscribed.

Issue Details

C# Code

Example without using delegate

    public int AddFunc(int a, int b) {
        return a + b;
    }

Example with delegate

    public int DelegateFunc(int a, int b) {
        Func<int, int, int> e = delegate(int c, int d) {
            return a + b;
        };
        return e(a, b);
    }

Analysis

See [sharplab example](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKGIGYACMhgYQYG8aHunGBLAHYYGAQQAmY4gFYAFIOHY0DeQ2ABKDlx7biAdgbYGAalUBuLdwC+FhjfrKhDACIwANjADm2DDGlzHig7C6prU2jpSADzySjFBAHwMMAwAvAxibp7eMP7CYLGOYhqcYeFlegbGZjbalual5fo5ger1tTSWQA===

Stopwatch benchmark without Benchmark.NET

On the one hand, of course, all the code to instantiate a delegate function is created, however, the actual code executed is also very poorly optimized.

Assembly result

AddFunc

    L0000: lea eax, [rdx+r8]
    L0004: ret

Content of Func<int, int, int>

    L0000: mov eax, [rcx+8]
    L0003: add eax, [rcx+0xc]
    L0006: ret
Author: deeprobin
Assignees: -
Labels:

tenet-performance, area-CodeGen-coreclr, untriaged

Milestone: -

@deeprobin deeprobin changed the title Delegates are poorly inlinend and optimized Delegates are poorly inlined and optimized Jan 6, 2022
@stephentoub
Copy link
Member

Dup of #56708, #6498, #4584, ...

@deeprobin
Copy link
Contributor Author

@stephentoub Super. Thanks I have probably overlooked.

@ghost ghost locked as resolved and limited conversation to collaborators Feb 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants