Parameter name case for SetParametersAsync
#26738
-
Hi, I came across performance best pactices by @SteveSandersonMS and @pranavkm and created a source generator to address the inefficiency of the default implementation of SetParametersAsync (see https://github.com/stefanloerwald/Generators.BetterBlazor). It seems like route parameters (e.g. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Yes, parameter names are case-insensitive everywhere in Blazor. I know it would be nice for perf if they were actually case sensitive, but this is what we chose back in 3.0 and need to stick with it now. I strongly recommend you don't call Instead I'd do something like: private void BetterBlazorImplementation__WriteSingleParameter(string name, object value)
{
switch (name[0])
{
case "m":
case "M":
if (string.Equals(name, nameof(MyFirstParameter), StringComparison.OrdinalIgnoreCase))
{
this.MyFirstParameter = (string)value;
return;
}
if (string.Equals(name, nameof(MySecondParameter), StringComparison.OrdinalIgnoreCase))
{
this.MySecondParameter = (string)value;
return;
}
break;
case "y":
case "Y":
if (string.Equals(name, nameof(YetAnotherParameter), StringComparison.OrdinalIgnoreCase))
{
this.YetAnotherParameter = (string)value;
return;
}
break;
}
throw new ArgumentException($"Unknown parameter: {name}");
} This retains the case-insensitivity, but reduces the number of string comparisons by branching on specific characters. Also, comparing against If you want to be fancier still, you could completely reduce it to a single string comparison per param by branching on more characters, e.g.:
It just depends on how clever you want to make your generated code. |
Beta Was this translation helpful? Give feedback.
-
For those interested, the repo for the SG project moved to https://github.com/excubo-ag/Generators.Blazor @SteveSandersonMS is this something you'd think should be moved to the framework at one point? And is there a plan to migrate the blazor compiler to roslyn-based source generators? |
Beta Was this translation helpful? Give feedback.
-
@stefanloerwald Yes, we'd certainly be interested in making this part of .NET 6. If you're interested in leading that effort, we'd need something like:
Preferably in that order. There's no point producing a final implementation until the perf difference is justified and any APIs are agreed. If you're interested in doing something like that, please let us know. If not, that's OK too - we may still do it ourselves but it depends on how other priorities are balanced.
Long term we'd love to do that. We don't have a specific commitment to do it yet though. .NET 6 planning is still very much underway. |
Beta Was this translation helpful? Give feedback.
Yes, parameter names are case-insensitive everywhere in Blazor. I know it would be nice for perf if they were actually case sensitive, but this is what we chose back in 3.0 and need to stick with it now.
I strongly recommend you don't call
ToLowerInvariant()
. That will destroy any perf gains you're making. It will have to make a new copy of the string entirely and walk across all the characters in it, and you will end up with a non-interned string.Instead I'd do something like: