funKTionale icon indicating copy to clipboard operation
funKTionale copied to clipboard

Memoization thread safe?

Open frankandrobot opened this issue 7 years ago • 1 comments

I used memoization in Scala before and had issues when two different threads tried to call the memoized function. (The issue was that the memo used a non-thread safe "regular" HashMap and that caused it misbehave. However, I also tried creating a custom memo implementation using ConcurrentHashMap and I seem to recall that had performance issues).

The question---is the underlying memo implementation thread safe?

frankandrobot avatar Dec 28 '16 03:12 frankandrobot

The current memoization implementation seems to be using ConcurrentHashMap for memorizing results. It's safe to use it in a multi-threaded environment, but there are situations where the memoized function might be evaluated multiple times for the same parameters instead of using the memorized value.

If there's no result computed for a given parameter and the memoized function is called from multiple threads at the same time with the same parameter, all of those calls might compute their own version of the result and attempt to store it. However, only the first computation to reach the hash-map will actually store anything. The rest of the computations will not store the result, but they do return them.

If the function you want to memoize doesn't have side effects (i.e. the function result only depends on its parameters), you wont see any difference in results. However, if there are side-effects (e.g. you use the current time from System.currentTimeMillis inside the function), you might see different values being memorized than what's being returned from the memoized function IF you access the memoized function at the same time from multiple threads.

jkpl avatar Dec 28 '16 11:12 jkpl