Skip to content

Improve the performance of configuration binding #36130

Closed as not planned
Closed as not planned

Description

@Kahbazi

Is your feature request related to a problem? Please describe.

I have a configuration class which contains over 50 properties. I want to get the new configuration for each request so IOptionsSnapshot is my choice, but the problem is that configuration is using Reflection to bind the options for each request. And for obvious reasons it can't be cached like IOptions or IOptionsMonitor.

Also breaking the configuration class to multiple class is not an option here.

Describe the solution you'd like

Is it good idea to create a method using ILGenerator at runtime and use that to bind the configuration class? I assume once the method is created it has better performance than reflection.

P.S. I'm willing to create a PR for this.

Activity

  1. Kahbazi commented on May 8, 2020

    @Kahbazi
    MemberAuthor

    This could improve the binding performance.

    @davidfowl Can we use the new source generators to bind configs?

  2. transferred this issue fromdotnet/extensionson May 8, 2020
  3. added
    untriaged
    on May 8, 2020
  4. added this to the Future milestone on May 8, 2020
  5. removed
    untriaged
    on Jun 12, 2020
  6. czd890 commented on Sep 10, 2020

    @czd890

    Any update?

  7. added
    tenet-performance
    and removed
    tenet-performance
    on Sep 29, 2020
  8. modified the milestones: Future, 6.0.0 on Oct 2, 2020
  9. added
    tenet-performance
    on Oct 2, 2020
  10. davidfowl commented on Oct 28, 2020

    @davidfowl
    Member

    I think we need a new API here since we're going to end up wanting to cache these delegates. We can cache internally but then we need to decide how to properly evict items from the cache (like Activator.CreateInstance) or we can provide an API that returns the bind delegate (I'd prefer that).

    Proposed API

    namespace Microsoft.Extensions.Configuration
    {
        public class ConfigurationBinder 
        {
    +        public static Action<IConfiguration, T> CreateBinder<T>(Action<BinderOptions> configureOptions = null);
        }

    Usage Examples

    using System;
    using System.Collections.Generic;
    using Microsoft.Extensions.Configuration;
    
    var configuration = new ConfigurationBuilder()
        .AddInMemoryCollection(new Dictionary<string, string>
        {
            { "Name", "David" }
        })
        .Build();
    
    var p = new Person();
    Action<IConfiguration, Person> binder = configuration.CreateBinder<Person>();
    
    binder(configuration, p);
    
    Console.WriteLine(p.Name);
    
    public class Person
    {
        public string Name { get; init; }
    }

    This API would allow the calling code to appropriately cache.

    Alternative Designs

    We could change the Action<IConfiguration, T> to Func<IConfiguration, T> and it would return the object instance.

  11. modified the milestones: 6.0.0, 7.0.0 on Jul 22, 2021
  12. changed the title [-]Using ILGenerator to bind configuration[/-] [+]Improve the performance of configuration binding[/+] on Feb 3, 2022
  13. modified the milestones: 7.0.0, Future on Jul 14, 2022
  14. modified the milestones: Future, 11.0.0 on Mar 2, 2026
  15. rosebyte commented on Mar 2, 2026

    @rosebyte
    Member

    Triage: using source generated binder should resolve the performance penalty from using reflection now.

  16. locked and limited conversation to collaborators on Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions