This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Improve performance of Enum.Parse/TryParse #2933
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StringComparison.OrdinalIgnoreCase : | ||
StringComparison.Ordinal; | ||
|
||
for (int valueIndex = 0; valueIndex <= value.Length; valueIndex++) |
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.
I think while (valueIndex <= value.Length)
here would make it easier to understand.
Also, why is not the condition of the loop just valueIndex < value.Length
? I guess it is handling some empty string corner case - comment on it would be nice.
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.
The equals is to handle the case where the string ends with a comma.
I can change it to a while instead of a for.
LGTM. Very nice improvement. |
The allocation profile of Enum.Parse today is fairly poor. While the design of the method returning an Enum requires at least one allocation for the boxed result (the generic TryParse could be overhauled to avoid this, but still has it), additional allocations shouldn't be necessary in the common cases. However, for some reason the current code is boxing a 0 on every call. It's also using string.Trim() to remove whitespace (once for the overall string and then once for each substring), and using String.Split to parse multiple values, which ends up allocating a string[] and an int[] even if there's only one value. This commit removes all allocations from Enum.Parse other than the boxing of the Enum result and some allocations on the code path where Enum.Parse is handed a string containing a number, in which case additional allocations are involved in using the Convert.ChangeType call. With an enum like: ```C# [Flags] enum Colors { Red = 0x1, Orange = 0x2, Yellow = 0x4, Green = 0x8, Blue = 0x10 } ``` using ```Enum.Parse(typeof(Color), "Red")``` repeatedly now results in 5x fewer gen0 GCs and is ~15% faster. Using ```Enum.Parse(typeof(Color), "Red, Orange, Yellow, Green, Blue")``` repeatedly now results in 23x fewer gen0 GCs is is similarly ~15% faster.
2bd87e6
to
98acacb
Compare
Thanks, @jkotas. Feedback addressed. |
👍 |
jkotas
added a commit
that referenced
this pull request
Feb 1, 2016
Improve performance of Enum.Parse/TryParse
stephentoub
added a commit
to stephentoub/corert
that referenced
this pull request
Feb 6, 2016
Improve performance of Enum.Parse/TryParse
This was referenced Feb 6, 2016
stephentoub
added a commit
to stephentoub/corert
that referenced
this pull request
Feb 6, 2016
Improve performance of Enum.Parse/TryParse
jkotas
added a commit
to dotnet/corert
that referenced
this pull request
Feb 7, 2016
Port dotnet/coreclr#2933 to corert
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The allocation profile of Enum.Parse today is fairly poor. While the design of the method returning an Enum requires at least one allocation for the boxed result (the generic TryParse could be overhauled to avoid this, but still has it), additional allocations shouldn't be necessary in the common cases. However, for some reason the current code is boxing a 0 on every call. It's also using string.Trim() to remove whitespace (once for the overall string and then once for each substring), and using String.Split to parse multiple values, which ends up allocating a string[] and an int[] even if there's only one value.
This commit removes all allocations from Enum.Parse other than the boxing of the Enum result and some allocations on the code path where Enum.Parse is handed a string containing a number, in which case additional allocations are involved in using the Convert.ChangeType call.
With an enum like:
using
Enum.Parse(typeof(Color), "Red")
repeatedly now results in 5x fewer gen0 GCs and is ~15% faster. UsingEnum.Parse(typeof(Color), "Red, Orange, Yellow, Green, Blue")
repeatedly now results in 23x fewer gen0 GCs is is similarly ~15% faster.cc: @jkotas, @ellismg