Runtime: Struct Return not optimized

Created on 28 May 2020  路  7Comments  路  Source: dotnet/runtime

I have created a GIST Here

The difference boils down to returning the struct vs assigning it to a variable and returning it.

public TimeSpan M() {
        TimeSpan x = TimeSpan.Zero;
        return x;  
    }

    public TimeSpan N() {
        return TimeSpan.Zero;          
    }

I would expect optimized code to be the same for both of these methods however this is not the case...

Is my assumption wrong?

Environment:
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.202\

Host (useful for support):
Version: 3.1.4
Commit: 0c2e69caa6

.NET Core SDKs installed:
3.1.202 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

area-CodeGen-coreclr untriaged

All 7 comments

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

Sorry @Dotnet-GitSync-Bot, I only see Performance-mono which would not be appropriate here I don't think.

Perhaps a new tag for Performance is also needed.

That gist looks like it was debug output, the included JSON file seems to indicate as such. In release I see identical JIT output for both methods.

The thing is I have seen similar results from Production code (Built in Release) which prompted this issue creation. (Changing from the variable to the return reduced overhead by about 3%) [according to the performance profiler]

It's odd we see different things...

Is your Framework version the same as mine?

I have verified form your link that from master the output is the same I cannot immediately explain the difference and hence the reason I opened this issue.

If you can provide me with instructions on how to produce the JIT dump from the production code I will do so but I must send the file privately as it contains proprietary source code owned by my employer.

/cc @CarolEidt

The two versions of code creates exactly same IL.

You can use ILSpy to check your production IL code first. If you find difference of IL patterns, you can put them here, which is useful for the JIT team.

I will close this issue as we have already profiled (using the Visual Studio Profiler) and worked around the issue by changing to a direct return where it showed the hot spot.

Additionally it seems the code produces the same IL and ASM on the master branch so if and when we notice this again we will create a new issue with the IL from the Release code and hopefully it will be easier to triage.

Was this page helpful?
0 / 5 - 0 ratings