fpco / inline-c

284 stars 51 forks source link

Change pure calls to use the faster unsafeDupablePerformIO #117

Closed Ofenhed closed 3 years ago

Ofenhed commented 3 years ago

Changing from unsafePerformIO to unsafeDupablePerformIO removes the thread safety of pure functions calls, which means that expensive CPU core memory synchronization is not performed. This means that pure functions may be called more than once if called at the same time from multiple threads. This change is safe as long as the documentation is followed, since the documentation asserts that called functions are not allowed to have side effects.

This fixes overhead reported in #115.

bitonic commented 3 years ago

@Ofenhed thanks a lot for the investigation and fix.

Could you amend the "BEWARE" comment for pure? Maybe something along the lines of

Also note that the function might be called multiple times, given that unsafeDupablePerformIO is used to call the provided C code.

bitonic commented 3 years ago

Actually, re-reading the comment for unsafeDupablePerformIO, I think an ever longer caveat is warranted, something like

Also note that the function might be called multiple times, given that unsafeDupablePerformIO is used to call the provided C code. Please refer to the documentation for unsafeDupabelPerformIO for more details. unsafeDupablePerformIO is used to ensure good performance using the threaded runtime.

bitonic commented 3 years ago

Oh and last request -- please link the issue above the code usage of unsafeDupablePerformIO, so that we remember why it's there :).

Ofenhed commented 3 years ago

I mostly used your text. What do you think about it now?