Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

stephentoub
Copy link
Member

When using DateTime{Offset}.ParseExact with a culture that employs genitive month names, the public DateTimeFormatInfo.AbbreviatedMonthGenitiveNames and DateTimeFormatInfo.MonthGenitiveNames properties are being accessed to get a string[] of the month names, but those properties clone. If we instead access the internal non-cloning version, we save on an array allocation on each parse.

private CultureInfo _ci = new CultureInfo("ru-RU");

[Benchmark] public DateTime Parse() => DateTime.ParseExact("вторник, 18 апреля 2023 04:31:26", "dddd, dd MMMM yyyy HH:mm:ss", _ci);
Method Toolchain Mean Ratio Allocated Alloc Ratio
Parse \main\corerun.exe 2.517 us 1.00 128 B 1.00
Parse \pr\corerun.exe 2.465 us 0.98 - 0.00

When using DateTime{Offset}.ParseExact with a culture that employs genitive month names, the public DateTimeFormatInfo.AbbreviatedMonthGenitiveNames and DateTimeFormatInfo.MonthGenitiveNames properties are being accessed to get a string[] of the month names, but those properties clone.  If we instead access the internal non-cloning version, we save on an array allocation on each parse.
@huoyaoyuan
Copy link
Member

Many members of CultureInfo are returning copies of arrays. This makes types out of corelib unable to avoid the allocation like this, including BigInteger. Can we add ReadOnlySpan returning variations to help them?

@stephentoub
Copy link
Member Author

Can we add ReadOnlySpan returning variations to help them?

You're of course welcome to open a proposal.

@stephentoub
Copy link
Member Author

Failure is #84979

@stephentoub stephentoub merged commit 1517fe4 into dotnet:main Apr 18, 2023
@stephentoub stephentoub deleted the parsegenitivemonth branch April 18, 2023 23:22
@ghost ghost locked as resolved and limited conversation to collaborators May 19, 2023
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.

3 participants