Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Loading SF3 files require decoding individual samples. Decoding is CPU-expensive and could make use of multiple cores. This PR distributes the decoding across multiple threads by using OpenMP tasks. This resolves #710.
The problem here is that the SF3 file is (usually) accessed via a common
FILE*
. Multiple threads seeking and reading around in the file would cause a race condition. Opening theFILE*
multiple times is not an option either, as this is implementation-defined behavior. And since most implementations do not document this case, it becomes undefined behavior.Originally, Marcus and I thought that using a memory-mapped file would be the solution to this. When I started implementing this I realized that this would create a "parallel-world" to the existing sfloader-callback mechanism. Secondly, it cannot be used if a user has set up custom callbacks via
fluid_sfloader_set_callbacks()
. Thirdly, memory-mapping is not supported for every file. If mmapping fails, we would still need to fall back toFILE*
.In this PR, I've chosen to synchronize the calls to fread() instead:
The fseek before the fread is required to ensure the sample is being read from the correct position.
Loading three SF3 file on my machine now takes only 4.5 seconds, rather than 13 seconds single-threaded before.