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

.NET 10 Preview 3 - Runtime #9823

Merged
merged 4 commits into from
Apr 7, 2025
Merged

Conversation

jamesmontemagno
Copy link
Member

No description provided.

@AndyAyersMS
Copy link
Member

@amanasifkhalid are you planning on adding the codegen notes?

@amanasifkhalid
Copy link
Member

@AndyAyersMS yes, I'll have something up shortly...


For more information on stack allocation improvements in the JIT compiler, check out [dotnet/runtime #108913](https://github.com/dotnet/runtime/issues/108913).

## Improved Code Layout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndyAyersMS I'm not sure how we want to highlight this work externally. Right now, it reads pretty bookish, though I'm not sure if there's a way around that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to keep this at a higher level we could create a benchmark with similar pattern and use the [MemoryDiagnoser] to show there is now less allocation.

Though it might also run a bit slower since we're now paying to zero init the array in the method rather than getting zeroed memory "for free" from the heap. Structuring the benchmark so it properly reflects when this cost is paid on .NET 9 might be tricky.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant the 3-opt writeup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the 3-opt writeup is ok.

}
```

The lifetime of `words` is scoped to the `Print` method, and the JIT can avoid heap-allocating the strings `"Hello"` and `"world!"` already. However, the fact that `words` is an array of `strings`, a reference type, would previously stop the JIT from stack-allocating it. Now, the JIT can eliminate every heap allocation in the above example. At the assembly level, the code for `Print` used to look like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the strings are actually heap allocated here, but that happens when the method is JITed, and their addresses are stable and can be incorporated into the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


For more information on stack allocation improvements in the JIT compiler, check out [dotnet/runtime #108913](https://github.com/dotnet/runtime/issues/108913).

## Improved Code Layout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to keep this at a higher level we could create a benchmark with similar pattern and use the [MemoryDiagnoser] to show there is now less allocation.

Though it might also run a bit slower since we're now paying to zero init the array in the method rather than getting zeroed memory "for free" from the heap. Structuring the benchmark so it properly reflects when this cost is paid on .NET 9 might be tricky.

@jamesmontemagno
Copy link
Member Author

Please ping me when ready to merge

@amanasifkhalid
Copy link
Member

@jamesmontemagno I don't have anything else to add

@jamesmontemagno
Copy link
Member Author

@AndyAyersMS anything else?

@jamesmontemagno jamesmontemagno merged commit 1290dec into dotnet10-p3 Apr 7, 2025
2 checks passed
@jamesmontemagno jamesmontemagno deleted the dotnet10-p3-runtime branch April 7, 2025 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants