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

Avoid unnecessary CultureInfo.CurrentCulture accesses in Enum #38866

Merged
merged 2 commits into from
Jul 8, 2020

Conversation

stephentoub
Copy link
Member

Enum can only be backed by primitive numerical types, and using the IConvertible interface implementations to convert to numerical types won't pay any attention to culture, so just as there's no need to pass through the supplied provider, there's no need to access CultureInfo.CurrentCulture.

(As suggested by @GrabYourPitchforks at #38779 (comment).)

[Benchmark]
public int ToInt() => Convert.ToInt32(DayOfWeek.Tuesday);
Method Toolchain Mean Error StdDev Ratio Allocated
ToInt \master\corerun.exe 20.82 ns 0.108 ns 0.101 ns 1.00 48 B
ToInt \pr\corerun.exe 16.55 ns 0.247 ns 0.231 ns 0.79 48 B

Enum can only be backed by primitive numerical types, and using the IConvertible interface implementations to convert to numerical types won't pay any attention to culture, so just as there's no need to pass through the supplied provider, there's no need to access CultureInfo.CurrentCulture.
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@stephentoub stephentoub closed this Jul 8, 2020
@stephentoub stephentoub reopened this Jul 8, 2020
@stephentoub stephentoub closed this Jul 8, 2020
@stephentoub stephentoub reopened this Jul 8, 2020
@stephentoub stephentoub merged commit bf15c2e into dotnet:master Jul 8, 2020
@stephentoub stephentoub deleted the enumconvert branch July 8, 2020 11:26
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants