GithubHelp home page GithubHelp logo

Comments (7)

bcuff avatar bcuff commented on May 17, 2024

ConcurrentDictionary.AddOrUpdate may allow concurrent execution of the factory method for the same key. I wanted this to guarantee that the function would only be called exactly once per key.
https://msdn.microsoft.com/en-us/library/ee378675(v=vs.110).aspx#Anchor_2

from autolazy.

bcuff avatar bcuff commented on May 17, 2024

Same is true for GetOrAdd https://msdn.microsoft.com/en-us/library/ee378677(v=vs.110).aspx#Anchor_2

from autolazy.

Anapher avatar Anapher commented on May 17, 2024

Thats the reason why I wrote part two. You are locking the full method for all parameters while executing. If you would only lock for unique parameters (using a lock dictionary, maybe also Concurrent) and then add to a ConcurrentDictionary, you would have a huge performance gain for concurrent execution.

from autolazy.

Anapher avatar Anapher commented on May 17, 2024
        // begin - fields added by post-compile step
        private static readonly ConcurrentDictionary<string, object> GetSettingsFileSyncRoot =
            new ConcurrentDictionary<string, object>();
        private static readonly ConcurrentDictionary<string, Settings> GetSettingsFileCache
            = new ConcurrentDictionary<string, Settings>();
        // end

        public static Settings GetSettingsFile(string fileName)
        {
            if (!GetSettingsFileCache.TryGetValue(fileName, out var result))
            {
                var parameterLock = GetSettingsFileSyncRoot.GetOrAdd(fileName, _ => new object());
                lock (parameterLock)
                {
                    if (!GetSettingsFileCache.TryGetValue(fileName, out result))
                    {
                        using (var fs = File.Open(fileName, FileMode.Open))
                        {
                            var serializer = new XmlSerializer(typeof(Settings));
                            result = (Settings) serializer.Deserialize(fs);
                        }

                        //that will always add
                        Debug.Assert(GetSettingsFileCache.TryAdd(fileName, result));
                    }
                }
            }
            return result;
        }

Check out this code (from your sample), it will execute faster (because it doesn't use volatile) and with using less memory than yours (you are really creating a new dictionary for every parameter?!)

from autolazy.

bcuff avatar bcuff commented on May 17, 2024

ConcurrentDictionary needs to use at least one memory barrier to retrieve an item internally so dropping the use of volatile in favor of ConcurrentDictionary isn't a benefit here. I suspect that a retrieve operation with no add would benchmark slower with the ConcurrentDictionary approach.

Your approach, however, would allow for concurrent object creation which may improve startup performance for multi-threaded applications. The technique I'm using is more optimized for long-term post startup performance.

from autolazy.

Anapher avatar Anapher commented on May 17, 2024

You are right, that was a bad comparision. Just out of curosity, I benchmarked a ConcurrentDictionary and a volatile dictionary, the dictionary was around 13% faster (and 1,04146E-08s per TryGetValue). But I get your point even if I'd do that slightly different. Sorry that my text above sounded slightly passive-aggressive, had a bad day. Cheers 👍

from autolazy.

bcuff avatar bcuff commented on May 17, 2024

Cheers 👍

from autolazy.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.