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

Suspiscous comment about constrained calls in MemoryExtensions.cs, StringBuilder.cs, and DefaultInterpolatedStringHandler.cs #110594

Closed
hamarb123 opened this issue Dec 10, 2024 · 7 comments
Labels
area-System.Memory needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity question Answer questions and provide assistance, not an issue with source code or documentation. tenet-performance Performance related issue

Comments

@hamarb123
Copy link
Contributor

hamarb123 commented Dec 10, 2024

Description

If you look at this line here:

if (((ISpanFormattable)value).TryFormat(_destination.Slice(_pos), out int charsWritten, default, _provider)) // constrained call avoiding boxing for value types

It says it's emitting a constrained call to avoid boxing, but this pattern does not seem to avoid boxing at all (even in the case where the type is known & known to implement the interface, it still seems to box):
https://sharplab.io/#v2:EYLgtghglgdgNAFxFANgHwAIGYAEGBMOAwjgN4CwAUDjXgCw4CyAPACoB8AFKzgB4CUZKrRE4oAMxydeYgM44AkgEZBnTsv4CAdAA1O/ANzDaAXypnKsBAFMATuIgBja4qVUK1WhgZ7D5qrIItgCujgg4Ojggru7GNLbWEAAmAPYwKACe9K66+mQWJkA

/cc @stephentoub who seems to have written the code (in MemoryExtensions.cs anyway) to confirm if I'm missing something obvious or not :)

Configuration

N/A

Regression?

I don't know.

Analysis

The idea here is that the JIT will elide the box & instead make a copy (not on heap) and call on that - I think we should update the comment to reflect this, as opposed to claiming we're doing some kind of contrained call. I'm happy to make a PR to do this if there's interest.

@hamarb123 hamarb123 added the tenet-performance Performance related issue label Dec 10, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 10, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 10, 2024
@stephentoub
Copy link
Member

It's not about the IL. It's about how the JIT handles it.

@hamarb123
Copy link
Contributor Author

Ok, so it's just a commenting issue then? (I say that because there is still definitely no constrained call or anything - it's more that the box is elided I assume)

@jeffschwMSFT jeffschwMSFT added question Answer questions and provide assistance, not an issue with source code or documentation. area-System.Memory labels Dec 11, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

@teo-tsirpanis teo-tsirpanis removed the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 12, 2024
@tannergooding
Copy link
Member

The JIT being able to do it is in part dependent on the constrained call, from what I remember. So the comment isn't strictly inaccurate, it just doesn't tell the whole story.

I'm not sure its relevant that the entire story is told in practice, there are hundreds to thousands of places throughout the BCL where similar tricks are done and not commented at all or which are similarly only done via a small comment that alludes to the in depth reason an optimization works.

@tannergooding tannergooding added needs-author-action An issue or pull request that requires more info or actions from the author. and removed untriaged New issue has not been triaged by the area owner labels Dec 13, 2024
Copy link
Contributor

This issue has been marked needs-author-action and may be missing some important information.

@tannergooding
Copy link
Member

Ultimately, I'd defer to Stephen here on if he thinks different wording is appropriate; but I don't necessarily think the churn is worthwhile in this case myself.

Copy link
Contributor

This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@hamarb123 hamarb123 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Memory needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity question Answer questions and provide assistance, not an issue with source code or documentation. tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

5 participants