简体   繁体   中英

Slow ApplicationSettingsBase

The application settings mechanism (derived from ApplicationSettingsBase) seems to be a real bottleneck when used in multithreading scenarios. Especially, when properties are queried often, the concurrency they introduce is slowing down my loops. I like to use them anyway to have those nice application configuration option. But maybe I need to wrap them into my own cache or so?

Anyone having the same issue? Am I missing something? I thought, the ApplicationSettingsBase does cache all settings already? Why does it seem to lock access from multiple threads at all? What could be a common workaround?

I'd really suggest wrapping any sort of "get settings" functionality in an object and hiding it behind an interface. We strongly-type this, so we have:

public class Worker 
{
    private readonly ISettings settings;
    public Worker (ISettings settings)
    {
        this.settings = settings;
    }

    public void Work ()
    {
        for (int i = 0; i < settings.MaxWorkerIterations (); i++)
        { ... }
    }
}

public interface ISettings
{
    int MaxWorkerIterations ();
}

public class AppConfigSettings
{
    public int MaxWorkerIterations ()
    {
        return (int) ApplicationSettings["MaxWorkerIterations"];
    }
}

This has the benefit of (mostly) compile-time checking and easy testability. You can also override your AppConfigSettings class to be a CachingAppConfigSettings class that does the obvious.

I dont see annything strange in having a thread safe settings mechanismm? If it is slowing your hihgly concurrent theads down, you should try to use local variables istead of querying the getsetting fast again. I think a redesign of your settings request mechanism would help performmance considerably.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM