Skip to content

Conversation

@stephentoub
Copy link
Member

@stephentoub stephentoub commented Jun 10, 2021

The only time {U}Int{32/64}.TryParse needs to consult a culture is if it needs to look up the positive/negative symbol from it, and if NumberStyles.None or NumberStyles.HexNumber are passed, it won't ever do that. All such call sites benefit from using CultureInfo.InvariantCulture explicitly, as it's just a static field read, whereas passing in null causes the implementation to access CultureInfo.CurrentCulture, which incurs a thread-static read. Most of our usage already does this, but a few sites don't; this fixes those.

The only time {U}Int{32/64}.TryParse needs to consult a culture is if it needs to look up the negative symbol from it, and if NumberStyles.None or NumberStyles.HexNumber are passed, it won't ever do that.  All such call sites benefit from using CultureInfo.InvariantCulture explicitly, as it's just a static field read, whereas passing in null causes the implementation to access CultureInfo.CurrentCulture, which incurs a thread-static read.  Most of our usage already does this, but a few sites don't; this fixes those.
@ghost ghost added the area-System.Net label Jun 10, 2021
@ghost
Copy link

ghost commented Jun 10, 2021

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

Issue Details

The only time {U}Int{32/64}.TryParse needs to consult a culture is if it needs to look up the negative symbol from it, and if NumberStyles.None or NumberStyles.HexNumber are passed, it won't ever do that. All such call sites benefit from using CultureInfo.InvariantCulture explicitly, as it's just a static field read, whereas passing in null causes the implementation to access CultureInfo.CurrentCulture, which incurs a thread-static read. Most of our usage already does this, but a few sites don't; this fixes those.

Author: stephentoub
Assignees: -
Labels:

area-System.Net

Milestone: -

@stephentoub stephentoub changed the title Use InvariantCulture when parsing with NumberStyles.None Use InvariantCulture when parsing with NumberStyles.None/HexNumber Jun 10, 2021
@stephentoub stephentoub merged commit 6431819 into dotnet:main Jun 10, 2021
@stephentoub stephentoub deleted the tryparseculture branch June 10, 2021 20:59
@ghost ghost locked as resolved and limited conversation to collaborators Jul 10, 2021
@karelz karelz added this to the 6.0.0 milestone Jul 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants