Stream: helpdesk (published)

Topic: thread safe caches


view this post on Zulip Maarten (Jun 01 2021 at 11:16):

I've been running into issues with WignerSymbols.jl, where one of the necessary operations is calculating the prime factorization of certain numbers. To speed this up you'd want to cache any found prime factors, and this cache is exactly what makes it thread unsafe.

As far as I can tell, there are 2 options. Either work with a lock, but that would slow down the existing code, even in situations where it was already called in a threadsafe way. The other way would be to work with thread-local caches (someone tried this https://github.com/Jutho/WignerSymbols.jl/pull/13 but memory consumption gets out of hand).

Ideally you would have some array type, for which getindex is fast, and as long as this index is < length(array), it is garantueed to be threadsafe. Modifying the array is allowed to be slower (getting a lock or something). I think a normal array does not have this property, because push! may cause the entire array to be re-allocated somewhere.

I have a few questions :


Last updated: Oct 02 2023 at 04:34 UTC