SciML / RuntimeGeneratedFunctions.jl

Functions generated at runtime without world-age issues or overhead
https://docs.sciml.ai/RuntimeGeneratedFunctions/stable/
MIT License
100 stars 14 forks source link

Thread safety? #8

Closed simeonschaub closed 4 years ago

simeonschaub commented 4 years ago

The current approach just creates and modifies a global Dict, which can cause problems when creating RuntimeGeneratedFunctions from different threads. I think a workaround for this might be to just create an array of Threads.nthreads() Dicts instead and only let the constructor modify the Dict at Threads.threadid(). If the same function is created on two different threads, we would of course have to store the expression twice, but that shouldn't be too bad.

c42f commented 4 years ago

I think just using a global lock for this would be fine?

simeonschaub commented 4 years ago

That should work as well. I was only worried about the overhead of such a global lock, but perhaps that is relatively small compared to creating RuntimeGeneratedFunctions in the first place.

c42f commented 4 years ago

Overhead should be insignificant; it's only hit when constructing and compiling the RuntimeGeneratedFunction. If those are hot code paths, you've probably got other problems ;-)

Actually I think the compiler still holds various locks so the @generated part may be locked as a side effect of something else. But not so for the construction side of things.

In general the compiler doesn't seem to like locks held inside @generated, but hopefully #11 is a valid way to do this.