-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Use the Regex source generator in the AlphaRouteConstraint. #44770
Conversation
The Regex source generator is faster because it doesn't need to interpret the regex. It does add slightly more code to the compiled app, but the tradeoff seems worth it. I switched the pattern from `a-z` with IgnoreCase to `A-Za-z` because the source generator generates better code with that pattern.
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.
This looks great!
Note for further optimization potential: |
Is there a regex issue for that? @stephentoub |
There's no "issue"... it's by design: [a-z] (IgnoreCase) and [A-Za-z] are functionally different (ever so slightly)... the former recognizes the Kelvin sign as being case-equivalent to k/K. So the difference in the code gen is that when using [A-Za-z], we generate: char.IsAsciiLetter(slice[iteration]) whereas with [a-z] under IgnoreCase, we generate: ((ch = slice[iteration]) < 128 ? ("\0\0\0\0\ufffe\u07ff\ufffe\u07ff"[ch >> 4] & (1 << (ch & 0xF))) != 0 : RegexRunner.CharInClass((char)ch, "\0\u0006\0A[a{KÅ")) With more work and analysis we could optimize the latter to instead be: char.IsAsciiLetter(ch = slice[iteration]) || ch == '\u212A' or something like that, but it still has the additional check. (I think it's fine to use [A-Za-z] here, though. Technically it's a breaking change, but I struggle to imagine anyone relying on Kelvin being equal to k or K in routes :) |
The public inheritance here is a problem. AlphaRouteConstraint is a public class that inherits from RegexRouteConstraint.
|
Sounds like that was an existing bug, given its documentation: https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.routing.constraints.alpharouteconstraint
for what it is worth: https://xkcd.com/ All take me to the same place. |
On the face of it that seems like that would be a worthwhile improvement for a fairly common looking pattern? |
I believe all feedback has been addressed. This should be ready for review. |
The Regex source generator is faster because it doesn't need to interpret the regex. It does add slightly more code to the compiled app, but the tradeoff seems worth it.
I switched the pattern from
a-z
with IgnoreCase toA-Za-z
because the source generator generates better code with that pattern.The results of the changed Benchmark shows a 9-10% faster when using an
alpha
constraint:Before
After
Size
Publishing a Linux NativeAOT app shows about a 4 KB increase in code size with this change:
Before: 27.1 MB (28,483,016 bytes)
After : 27.1 MB (28,487,464 bytes)