-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Use RuntimeHelpers.CreateSpan for array initialization with relevant primitives #61414
Conversation
688ad32
to
8741563
Compare
669693c
to
f2575fe
Compare
@stephentoub We discourage force pushes and any forms of rewriting existing commits in branches that are already under review process. These operations confuse CodeFlow (the tool that we usually use for reviews) and prevent us from doing reviews in an incremental fashion. That is forcing us to start the review process from scratch after a change like that is performed. The recommended process is to keep pushing new commits into the branch, including merge commits, until PR is ready for merge, then doing a squash merge. |
Most of the code this touches had to be rewritten in the face of the conflicting commit that got merged. I don't believe the effort required to review with a rebase or a merge would have been significantly different. But I will try to refrain from rebases in the future. |
Would be nice if |
What is it you'd hope it would do it's not already doing? |
I thought the runtime materialized the string and created a span over it then, but it seems I was wrong despite the IL using |
Done with review pass (commit 2), new tests are not reviewed. |
/azp run |
Azure Pipelines successfully started running 4 pipeline(s). |
src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenReadOnlySpanConstructionTest.cs
Outdated
Show resolved
Hide resolved
Done with review pass (commit 11) |
Great, thanks. |
src/Compilers/Core/Portable/CodeGen/PrivateImplementationDetails.cs
Outdated
Show resolved
Hide resolved
Feedback addressed, thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (commit 12)
@jcouv Please review the latest revision |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks (iteration 12)
Thanks @stephentoub :-) |
Thank you, both, for the reviews. |
Looking forward to seeing a huge PR against dotnet/runtime to replace all arrays with |
I have it ready to go, we just need the new compiler to flow in :) |
FYI, it looks like this change doesn't work in Blazor WebAssembly. I think it's an issue with Mono? I created a sample solution to show that it works in a Console app, and not in a Blazor WASM app: https://github.com/codemonkey85/BlazorWasmIssueWithArrayInitializationWithRelevantPrimitives |
The compiler currently optimizes array creation where:
ReadOnlySpan<T>
such that it's not exposed in any writeable mannerIn such a situation, it creates a PrivateImplementationDetails field containing the data, and replaces the array creation with a span that points directly to the data on PrivateImplementationDetails.
This PR extends that optimization to multi-byte primitives: short, ushort, char, int, uint, long, ulong, float, and double. If the target platform has the RuntimeHelpers.CreateSpan method, it is used to handle endianness fixups. If the target platform lacks that method, the compiler emits code to cache the array in PrivateImplementationDetails and wrap a span around that lazily-initialized array.