caffeine lock issue for using get with mappingFunction #605
-
As caffeine use JAVA 8 concurrenthashmap as the data structure, when we use the method get(K key, Function<? super K, ? extends V> mappingFunction), it will invoke concurrenthashmap compute method, so it will lock the head node in the map bucket . So always mappingFunction is time consuming operation like fetch from database, then any other operations like put which hash to the same head node will be blocked by waiting lock. Is that cost too much? I see guava use a fake node to insert first ,after that judge if the node is loading then blocked in the same entry, it reduce the lock range to one specific node. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Most of the time this isn't a problem because the number of reads is much higher than writes. This library's focus on an excellent hit rate means fewer misses and therefore fewer loads. Since the map will evenly distribute entries there is also a good amount of inherent write concurrency. If profiling your application does indicate a bottleneck then there are two very simple approaches that can fix the problem.
|
Beta Was this translation helpful? Give feedback.
Most of the time this isn't a problem because the number of reads is much higher than writes. This library's focus on an excellent hit rate means fewer misses and therefore fewer loads. Since the map will evenly distribute entries there is also a good amount of inherent write concurrency. If profiling your application does indicate a bottleneck then there are two very simple approaches that can fix the problem.
initialCapacity
to increase the number of bins. The implementation docs of the map state,