You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ChainedConfigurationProvider.GetChildKeys is inefficient #62510
ChainedConfigurationProvider.GetChildKeys ends up splitting strings which forces an array allocation even if there are no keys with segments. This has shown up on a couple of performance profiles internally and we should improve it.
If this shows up on profiles, then I think you could do this allocation free entirely.
The below code is entirely untested and for illustration purposes only 😄. It might be slower for all I know, but it doesn't allocate strings to split. This has a slight behavior change in what is returned when the segments aren't equal, but it conforms to the contract of Compare. If we want to retain identical behavior, it's possible, but would result in doing unnecessary splitting.
namespaceMicrosoft.Extensions.Configuration{/// <summary>/// IComparer implementation used to order configuration keys./// </summary>publicclassConfigurationKeyComparer:IComparer<string>{/// <summary>/// The default instance./// </summary>publicstaticConfigurationKeyComparerInstance{get;}=newConfigurationKeyComparer();/// <summary>A comparer delegate with the default instance.</summary>internalstaticComparison<string>Comparison{get;}=Instance.Compare;publicstaticvoidSplit(ReadOnlySpan<char>str,chardelimiter,outReadOnlySpan<char>next,outReadOnlySpan<char>remaining,boolskipEmpty=true){while(true){intlocation=str.IndexOf(delimiter);if(location==-1){next=str;remaining=default;return;}elseif(location==0&&skipEmpty){str=str[1..];continue;}else{next=str[..location];remaining=str[(location+1)..];return;}}}publicintCompare(string?x,string?y){ReadOnlySpan<char>xRemaining=x;ReadOnlySpan<char>yRemaining=y;do{Split(xRemaining,':',outReadOnlySpan<char>xPart,outxRemaining);Split(yRemaining,':',outReadOnlySpan<char>yPart,outyRemaining);boolxIsInt=int.TryParse(xPart,outintvalue1);boolyIsInt=int.TryParse(yPart,outintvalue2);intresult;if(!xIsInt&&!yIsInt){// Both are stringsresult=MemoryExtensions.CompareTo(xPart,yPart,StringComparison.OrdinalIgnoreCase);}elseif(xIsInt&&yIsInt){// Both are intresult=value1-value2;}else{// Only one of them is intresult=xIsInt?-1:1;}if(result!=0){// One of them is differentreturnresult;}}while(!xRemaining.IsEmpty&&!yRemaining.IsEmpty);returnxRemaining.Length-yRemaining.Length;}}}
I was going to tinker with using the existing StringSegment Tokenizer in the extensions Primitive namespace. What would be useful is some example input strings to run against.
Activity
gfoidl commentedon Dec 8, 2021
I can look into this (next week).
Seems there's some potential for improvement.
Use of
runtime/src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationKeyComparer.cs
Line 14 in 12a8819
:).vcsjones commentedon Dec 8, 2021
If this shows up on profiles, then I think you could do this allocation free entirely.
The below code is entirely untested and for illustration purposes only 😄. It might be slower for all I know, but it doesn't allocate strings to split. This has a slight behavior change in what is returned when the segments aren't equal, but it conforms to the contract of
Compare. If we want to retain identical behavior, it's possible, but would result in doing unnecessary splitting.Wraith2 commentedon Dec 8, 2021
I was going to tinker with using the existing StringSegment Tokenizer in the extensions Primitive namespace. What would be useful is some example input strings to run against.
SteveDunn commentedon Mar 25, 2022
I'm happy to look at this one. Is there a project with a suitably large set of config to baseline?
maryamariyan commentedon Mar 26, 2022
Thanks @SteveDunn this issue is currently in progress.
SteveDunn commentedon Mar 26, 2022
Sorry, I didn't see a branch/PR
maryamariyan commentedon Mar 26, 2022
@SteveDunn no worries, just added the PR here in #67186