dotnet / dotnet/runtime

JIT should hoist interface method resolution out of loops

Open
#130,572 3 comments 0 reactions 0 assignees View on GitHub
area-CodeGen-coreclr help wanted tenet-performance
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description
The JIT seems to not host interface method resolution out of loops, it should be possible for it to do that while preserving side effects NREs.

Example code:
```cs
public static class Test
{
public static void A(object o)
{
for (int i = 0; i < 1000; i++)
B(o.ToString());
}

public static void A(IInterface o)
{
for (int i = 0; i < 1000; i++)
B(o.Get());
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void B(string s) {}

public interface IInterface
{
string Get();
}
}
```

Both `ToString` and `Get` resolution should be hoisted out of loop.

### Configuration
Main on Godbolt

### Regression?
No

### Data
https://compiler-explorer.com/z/1KbzTa5M9

Contributor guide

Open the contributing guide

Research direction

Start with the C# reproducer and the linked Compiler Explorer configuration on Godbolt, comparing the generated code for the object.ToString and interface Get loops. Trace the JIT's interface method resolution and its handling of side effects and null-reference exceptions. Done means both resolutions are hoisted out of the loops without changing those observable behaviors.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.