Skip to main content
added 103 characters in body
Source Link
Trauer
  • 801
  • 6
  • 17

The most up-to-date version of the code can be found here. I'm currently trying to implement an elegant and low-contentionin TValue GetOrCreate(TKey key, Func<TValue> valueCreator) function; will update thi question when I'm done.

P.s.:my own answer; since I received such great feedback, it is kinda hard to and couldn't choose onea single answer as the answer, I decided to compile the suggestions, and post the resulting code, in a single new answer. This way I'm able to share the final result with the community, acknowledge the help and still keep the question / answers valid.

I won't accept my answer as "the answer" because I don't want to fragsteal.

The most up-to-date version of the code can be found here. I'm currently trying to implement an elegant and low-contention TValue GetOrCreate(TKey key, Func<TValue> valueCreator) function; will update thi question when I'm done.

P.s.: I received such great feedback, it is kinda hard to choose one as the answer.

The most up-to-date version of the code can be found in my own answer; since I received such great feedback and couldn't choose a single answer as the answer, I decided to compile the suggestions, and post the resulting code, in a single new answer. This way I'm able to share the final result with the community, acknowledge the help and still keep the question / answers valid.

I won't accept my answer as "the answer" because I don't want to fragsteal.

edits don't need to be explicit
Source Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 101

Edit: I can't provide a real and concise usage example of this class right now; and I have not yet measured the following numbers, but I expect that most 4/5 cache accesses will result in a hit, and 1/5 in a miss followed by an add.

Edit 2: An actual, yet codeless, example of usage of this class is to store the fitness of (immutable) individuals in a genetic algorithm. Since the individuals are immutable, their fitnesses stay the same as long as they live; thus I can compute it once and cache the result.

Edit 3: theThe most up-to-date version of the code can be found here. I'm currently trying to implement an elegant and low-contention TValue GetOrCreate(TKey key, Func<TValue> valueCreator) function; will update thi question when I'm done.

Edit: I can't provide a real and concise usage example of this class right now; and I have not yet measured the following numbers, but I expect that most 4/5 cache accesses will result in a hit, and 1/5 in a miss followed by an add.

Edit 2: An actual, yet codeless, example of usage of this class is to store the fitness of (immutable) individuals in a genetic algorithm. Since the individuals are immutable, their fitnesses stay the same as long as they live; thus I can compute it once and cache the result.

Edit 3: the most up-to-date version of the code can be found here. I'm currently trying to implement an elegant and low-contention TValue GetOrCreate(TKey key, Func<TValue> valueCreator) function; will update thi question when I'm done.

I can't provide a real and concise usage example of this class right now; and I have not yet measured the following numbers, but I expect that most 4/5 cache accesses will result in a hit, and 1/5 in a miss followed by an add.

An actual, yet codeless, example of usage of this class is to store the fitness of (immutable) individuals in a genetic algorithm. Since the individuals are immutable, their fitnesses stay the same as long as they live; thus I can compute it once and cache the result.

The most up-to-date version of the code can be found here. I'm currently trying to implement an elegant and low-contention TValue GetOrCreate(TKey key, Func<TValue> valueCreator) function; will update thi question when I'm done.

Tweeted twitter.com/StackCodeReview/status/1162967488784666624
added 434 characters in body
Source Link
Trauer
  • 801
  • 6
  • 17

Edit 3: the most up-to-date version of the code can be found here. I'm currently trying to implement an elegant and low-contention TValue GetOrCreate(TKey key, Func<TValue> valueCreator) function; will update thi question when I'm done.

P.s.: I received such great feedback, it is kinda hard to choose one as the answer.

Edit 3: the most up-to-date version of the code can be found here. I'm currently trying to implement an elegant and low-contention TValue GetOrCreate(TKey key, Func<TValue> valueCreator) function; will update thi question when I'm done.

P.s.: I received such great feedback, it is kinda hard to choose one as the answer.

Became Hot Network Question
spelling
Source Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 101
Loading
added 236 characters in body
Source Link
Trauer
  • 801
  • 6
  • 17
Loading
Source Link
Trauer
  • 801
  • 6
  • 17
Loading