Skip to content

<chrono>: Replace an elaborate table with a switch#5905

Merged
StephanTLavavej merged 1 commit intomicrosoft:mainfrom
StephanTLavavej:chrono-cleanup
Nov 27, 2025
Merged

<chrono>: Replace an elaborate table with a switch#5905
StephanTLavavej merged 1 commit intomicrosoft:mainfrom
StephanTLavavej:chrono-cleanup

Conversation

@StephanTLavavej
Copy link
Member

<chrono> formatting validated modifier characters by searching an elaborate table containing bitflags of allowed modifiers. It tried to use ranges::find with a PMD projection, but we needed a product code workaround for VSO-1664341 "/clr C++20 System.NullReferenceException when calling ranges algorithms with PMD projections".

I initially tried to replace the ranges::find with a range-for loop as a perma-workaround, which was an improvement, but then I went further. We can replace all of this machinery with a switch.

By changing void _Check_modifier to bool _Valid_modifier, we can further simplify the control flow by immediately returning true or false, and having the caller take care of throwing any exception.

I'm adding _STL_INTERNAL_CHECK(_Modifier == 'E' || _Modifier == 'O'); for a previously implicit assumption.

Finally, I'm adding a comment to cite the relevant Standardese for where this switch's data comes from.

@StephanTLavavej StephanTLavavej requested a review from a team as a code owner November 24, 2025 11:32
@StephanTLavavej StephanTLavavej added enhancement Something can be improved format C++20/23 format chrono C++20 chrono labels Nov 24, 2025
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Nov 24, 2025
@StephanTLavavej StephanTLavavej moved this from Initial Review to Final Review in STL Code Reviews Nov 24, 2025
@AlexGuteniev
Copy link
Contributor

I think the jump table emitted by that switch would perform better than linear array lookup here. And maybe even better than the bitset emitted if it was written with ifs.

@StephanTLavavej StephanTLavavej moved this from Final Review to Ready To Merge in STL Code Reviews Nov 25, 2025
@StephanTLavavej StephanTLavavej moved this from Ready To Merge to Merging in STL Code Reviews Nov 25, 2025
@StephanTLavavej
Copy link
Member Author

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit e07e179 into microsoft:main Nov 27, 2025
44 checks passed
@github-project-automation github-project-automation bot moved this from Merging to Done in STL Code Reviews Nov 27, 2025
@StephanTLavavej StephanTLavavej deleted the chrono-cleanup branch November 27, 2025 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chrono C++20 chrono enhancement Something can be improved format C++20/23 format

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants