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

Special-case enums in interpolated string handlers #76157

Closed
Tracked by #79053
stephentoub opened this issue Sep 26, 2022 · 3 comments · Fixed by #78580
Closed
Tracked by #79053

Special-case enums in interpolated string handlers #76157

stephentoub opened this issue Sep 26, 2022 · 3 comments · Fixed by #78580
Assignees
Labels
Milestone

Comments

@stephentoub
Copy link
Member

stephentoub commented Sep 26, 2022

Once Type.IsEnum is an intrinsic (#71685) and the static Enum.TryFormat has been added (#71590), we should be able to update the in-box interpolated string handlers to special-case enums and use TryFormat rather than calling ToString on them, which at a minimum boxes and worst-case allocates a result string. This can either be a long-term solution, or it can be a stop-gap until Enum itself implements ISpanFormattable, which we've said we'll only do if the JIT is updated to avoid boxing when using its methods.

(We can similarly special-case anywhere optimized formatting would be helpful, e.g. maybe in StringBuilder, in ValueStringBuilder, etc.)

@stephentoub stephentoub added this to the 8.0.0 milestone Sep 26, 2022
@stephentoub stephentoub self-assigned this Sep 26, 2022
@ghost
Copy link

ghost commented Sep 26, 2022

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

Issue Details

Once Type.IsEnum is an intrinsic (#71685) and the static Enum.TryFormat has been added (#71590), we should be able to update the in-box interpolated string handlers to special-case enums and use TryFormat rather than calling ToString on them, which at a minimum boxes and worst-case allocates a result string. This can either be a long-term solution, or it can be a stop-gap until Enum itself implements ISpanFormattable, which we've said we'll only do if the JIT is updated to avoid boxing when using its methods.

Author: stephentoub
Assignees: stephentoub
Labels:

area-System.Runtime, tenet-performance

Milestone: 8.0.0

@mburbea
Copy link

mburbea commented Sep 26, 2022

In the mean time, I've used this pattern to some success since 6.0 to special case code for enums.

typeof(T).IsValueType && default(T) is Enum;;

https://sharplab.io/#v2:C4LghgzgtgPgAgJgIwFgBQcAMACAygCzACcAHAGTACMA6AJQFcA7YASygFMBuddAbQCkWwAOLtG7IiwDGACmABPEuwD2AMxktmASi0AaQSLETpcxSvW5gkxgHMAwsqgliLCMsY7d2A6PGTZCkpqMnBImDoAuuhwAMzYiNh2ADwAKgB82ADe6Ni58XGhAGzYlMrKADbYALIyWtgAvGk5eS2B5jIpWtQAkhAAamDl9OwpZtgAZOPYACbsqmD05cAdda7YAKKM9FDcaAC+QA===

@MichalPetryka
Copy link
Contributor

#71685 has been merged so the first part has been unblocked here.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Nov 18, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Dec 6, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jan 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants