Comments (7)
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.
Same is true for GetOrAdd https://msdn.microsoft.com/en-us/library/ee378677(v=vs.110).aspx#Anchor_2
from autolazy.
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.
// 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.
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.
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.
Cheers
from autolazy.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autolazy.