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
This implementation makes allocations which is not ideal. We could get better perf with a new API with a more optimal framework implementation.
API Proposal
namespace Microsoft.Extensions.Configuration
{
public interface IConfiguration
{
+ public bool HasChildren { get; }
}
}
API Usage
A primary user of this feature would be the configuration binding source generator. Sample code it would generate:
privatestaticvoidBindCore(IConfigurationconfiguration,MyClassobj){ThrowIfNull(obj);if(configuration["MyString"]isstringstringValue0){obj.MyString=stringValue0;}IConfigurationSectionsection2=configuration.GetSection("MyList");if(section2.HasChildren){List<int>temp3=obj.MyList;temp3??=newList<int>();BindCore(section2,reftemp3);obj.MyList=temp3;}IConfigurationSectionsection4=configuration.GetSection("MyDictionary");if(section4.HasChildren){Dictionary<string,string>temp5=obj.MyDictionary;temp5??=newDictionary<string,string>();BindCore(section4,reftemp5);obj.MyDictionary=temp5;}// Repeat for every object or collection property on MyClass}
We see that the binding performance would improve with a more optimal implementation, particularly for large object graphs.
I think this needs to be an extension method on the IConfiguration and not extending the original interface.
I know we need to have a public way to access it though.
Would we be establishing a new pattern for APIs on the type?
Having it be a property on the type itself seemed natural to me since we expect this to be a cheap/cached check. I did a quick scan of what an implementation would look like to verify this assumption, but I'll have to dive a bit deeper to be sure.
We could also consider optimizing GetChildren(); there's a lot of LINQ usage in the implementation.
In order for this to actually be beneficial, it would need to be a property on the interface. An extension property could only hard-code our own configuration types - ConfigurationRoot, ConfigurationManager, etc. For other IConfiguration types, it would need to call GetChildren().Any().
We could also consider optimizing GetChildren(); there's a lot of LINQ usage in the implementation.
Note that in this case we are eagerly building the full list (which we need to do for the reason in the comment):
Is it possible we can extend this interface? note this interface is supported on the .NET Framework which adding anything there will be a breaking change.
Or we could introduce a new interface that had the new property on it. Then an extension method could check for the interface and cast to it. And if the IConfiguration didn't implement the interface, it could fall back to GetChildren().Any(). That would still work on .NET Framework.
I wasn't aware of that implementation. I also used section.GetChildren().Any() initially but the feedback is that we want to avoid LINQ in the source gen implementation.
If there is a section then we're going to enumerating it anyways.
I guess it would be helpful in the cases where there are no children so we avoid allocating a section.
I wasn't aware of that implementation. I also used section.GetChildren().Any() initially but the feedback is that we want to avoid LINQ in the source gen implementation.
We should use Exists in the source generator.
I guess it would be helpful in the cases where there are no children so we avoid allocating a section.
It'd only be helpful in the case where there was a section, but it was empty.
In the latter case this looks like it can be optimized to avoid allocations for empty sections.
We will create an IConfigurationSection for each property in the object, whether that property has anything in the configuration or not. (Note the other implementations of GetSection are the same.)
, but it was empty.
This would also help in the case where there is information in the configuration. Calling GetChildren().Any() just to check if there are any, and then calling GetChildren() again to actually enumerate them throws away the information built up above (the Aggregate, Distinct, Select, and most importantly the ToList() at the end).
Optimizing for an empty section seems like an edge case?
It is optimizing for both empty and non-empty. When it is non-empty we are throwing the info away.
It isn't really an edge case. Not every property in the object is always filled out in Configuration. For every property in the Type we will get a IConfigurationSection and then call GetChildren().Any() on it, whether it has a value in the configuration or not. So unless every property of the Options object has a value in the configuration, it isn't an edge case.
When it is non-empty we are throwing the info away.
What do you mean?
For every property in the Type we will get a IConfigurationSection and then call GetChildren().Any() on it, whether it has a value in the configuration or not. So unless every property of the Options object has a value in the configuration, it isn't an edge case.
How? When there's no section we only allocate the IConfigurationSection and the Value is null. Why would we do IConfigurationSection().Any() in that scenario?
I don't see how that code above is allocating per call to Exists on an non-existent section... (what am I missing..). Also, how does the above API avoid allocating the IConfigurationSection for every property? Doesn't the code for object binding still call GetSection(...).HasChildren?
That call to GetChildren().Any() allocates all the objects I outlined above just to check if there are any, and then throws that all away. Then inside the if, it callsBindArray, which then calls GetChildren() again to enumerate them:
When there's no section we only allocate the IConfigurationSection and the Value is null. Why would we do IConfigurationSection().Any() in that scenario?
Notice the section.Value != null || section.GetChildren().Any(). When the Value is null, the left hand side is false and the GetChildren().Any() will be called.
Also, how does the above API avoid allocating the IConfigurationSection for every property?
That call to GetChildren().Any() allocates all the objects I outlined above just to check if there are any, and then throws that all away. Then inside the if, it callsBindArray, which then calls GetChildren() again to enumerate them:
OK I get it, when section.Value is null that isn't enough to determine that there's no children. This API is making more sense now 😄 . Sorry for the confusion.
FWIW, I don't necessarily see this as a "must have" API for .NET 8. Using the ConfigurationBinder source generator is going to give so much performance benefits over the reflection based ConfigurationBinder, these allocations aren't going to make that much of a difference. And I don't see ConfigurationBinder being on a "hot path" in most apps.
Activity
tarekgh commentedon Mar 20, 2023
I think this needs to be an extension method on the IConfiguration and not extending the original interface. Right? Can't we just add this to the internal extensions we have here https://source.dot.net/#Microsoft.Extensions.Configuration/InternalConfigurationRootExtensions.cs,13? I know we need to have a public way to access it though.
layomia commentedon Mar 20, 2023
Would we be establishing a new pattern for APIs on the type?
Having it be a property on the type itself seemed natural to me since we expect this to be a cheap/cached check. I did a quick scan of what an implementation would look like to verify this assumption, but I'll have to dive a bit deeper to be sure.
We could also consider optimizing
GetChildren(); there's a lot of LINQ usage in the implementation.eerhardt commentedon Mar 20, 2023
In order for this to actually be beneficial, it would need to be a property on the interface. An extension property could only hard-code our own configuration types -
ConfigurationRoot,ConfigurationManager, etc. For otherIConfigurationtypes, it would need to callGetChildren().Any().Note that in this case we are eagerly building the full list (which we need to do for the reason in the comment):
runtime/src/libraries/Microsoft.Extensions.Configuration/src/InternalConfigurationRootExtensions.cs
Lines 38 to 39 in 967250c
tarekgh commentedon Mar 20, 2023
Is it possible we can extend this interface? note this interface is supported on the .NET Framework which adding anything there will be a breaking change.
eerhardt commentedon Mar 20, 2023
We would either do it the way we IMemoryCache.GetCurrentStatistics:
runtime/src/libraries/Microsoft.Extensions.Caching.Abstractions/src/IMemoryCache.cs
Lines 34 to 40 in 967250c
Or we could introduce a new interface that had the new property on it. Then an extension method could check for the interface and cast to it. And if the
IConfigurationdidn't implement the interface, it could fall back toGetChildren().Any(). That would still work on .NET Framework.davidfowl commentedon Mar 21, 2023
Help me understand the case this is optimized for? For a missing section the IConfigurationSection.Value is null, so
runtime/src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/ConfigurationExtensions.cs
Line 78 in 967250c
layomia commentedon Mar 21, 2023
I wasn't aware of that implementation. I also used
section.GetChildren().Any()initially but the feedback is that we want to avoid LINQ in the source gen implementation.I guess it would be helpful in the cases where there are no children so we avoid allocating a section.
davidfowl commentedon Mar 22, 2023
We should use Exists in the source generator.
It'd only be helpful in the case where there was a section, but it was empty.
In the latter case this looks like it can be optimized to avoid allocations for empty sections.
eerhardt commentedon Mar 22, 2023
runtime/src/libraries/Microsoft.Extensions.Configuration/src/InternalConfigurationRootExtensions.cs
Lines 26 to 40 in 967250c
I'm seeing at least 4 allocations there:
AggregateDistinctSelect(There are actually more under the covers, as
source.GetChildKeys(seed, path)internally creates aList<T>, and so doesSelect.)Here's a benchmark using
7.0.0:And the results - almost half a KB is allocated every time you call
Exists()on an empty section:Note that there is always a section returned, even if it doesn't exist in the configuration:
runtime/src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationManager.cs
Line 62 in 967250c
We will create an IConfigurationSection for each property in the object, whether that property has anything in the configuration or not. (Note the other implementations of GetSection are the same.)
This would also help in the case where there is information in the configuration. Calling
GetChildren().Any()just to check if there are any, and then callingGetChildren()again to actually enumerate them throws away the information built up above (the Aggregate, Distinct, Select, and most importantly theToList()at the end).davidfowl commentedon Mar 23, 2023
Optimizing for an empty section seems like an edge case? What’s motivating that?
eerhardt commentedon Mar 23, 2023
GetChildren().Any()on it, whether it has a value in the configuration or not. So unless every property of the Options object has a value in the configuration, it isn't an edge case.davidfowl commentedon Mar 23, 2023
What do you mean?
How? When there's no section we only allocate the
IConfigurationSectionand the Value is null. Why would we do IConfigurationSection().Any() in that scenario?I don't see how that code above is allocating per call to Exists on an non-existent section... (what am I missing..). Also, how does the above API avoid allocating the
IConfigurationSectionfor every property? Doesn't the code for object binding still callGetSection(...).HasChildren?eerhardt commentedon Mar 23, 2023
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Line 303 in 967250c
That call to
GetChildren().Any()allocates all the objects I outlined above just to check if there are any, and then throws that all away. Then inside theif, it callsBindArray, which then callsGetChildren()again to enumerate them:runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Line 734 in 967250c
runtime/src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/ConfigurationExtensions.cs
Lines 78 to 84 in 967250c
Notice the
section.Value != null || section.GetChildren().Any(). When the Value is null, the left hand side isfalseand theGetChildren().Any()will be called.It doesn't.
davidfowl commentedon Mar 23, 2023
OK I get it, when section.Value is null that isn't enough to determine that there's no children. This API is making more sense now 😄 . Sorry for the confusion.
eerhardt commentedon Mar 23, 2023
FWIW, I don't necessarily see this as a "must have" API for .NET 8. Using the ConfigurationBinder source generator is going to give so much performance benefits over the reflection based ConfigurationBinder, these allocations aren't going to make that much of a difference. And I don't see ConfigurationBinder being on a "hot path" in most apps.
rosebyte commentedon Mar 4, 2026
Triage: we agree with @eerhardt