index: avoid unsafe buffer sharing in Log_file#358
Merged
craigfe merged 1 commit intomirage:mainfrom Oct 11, 2021
Merged
Conversation
6771e45 to
0a7b05c
Compare
Member
|
What's the mutex impact on the performance? Wouldn't it be better to have a scratch buffer by thread? |
0a7b05c to
5154202
Compare
Member
Author
|
@samoht: I took some measurements of that :-) Yes, that mutex has a ~5% performance impact on very write-intensive workloads like the replay benchmarks when the log size is large (but I couldn't see any impact for other workloads). I've changed the implementation to allocate new buffers on entry to |
mirage#355 introduced a small optimisation to re-use a "local" scratch buffer when decoding values from the log file. This is actually unsafe: during the merge, the asynchronous merge thread and the main writer thread can attempt concurrent reads from the log file, causing contention over the scratch buffer. This can be observed by inserting `Thread.yield` calls inside the `Value.decode` implementation and then stress-testing the interface (e.g. by running the replay benchmarks). This commit ensures that each call to a `Log_file` function gets its own scratch buffer, ensuring safe concurrent access without introducing potential contention issues.
5154202 to
4e3ba1e
Compare
Log_file with a mutexLog_file
Ngoguey42
approved these changes
Oct 11, 2021
craigfe
added a commit
to craigfe/irmin
that referenced
this pull request
Oct 12, 2021
This contains mirage/index#358, which doesn't change the API but is an important bug-fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
#355 introduced a small optimisation to re-use a "local" scratch buffer when decoding values from the log file.
This is actually unsafe: during the merge, the asynchronous merge thread and the main writer thread can attempt concurrent reads from the log file, causing contention over the scratch buffer. This can be observed by inserting
Thread.yieldcalls inside theValue.decodeimplementation and then stress-testing the interface (e.g. by running the replay benchmarks).This commit ensures that each call to a
Log_filefunction gets its own scratch buffer, ensuring safe concurrent access without introducing potential contention issues.Note: I initially tried to just wrap
t.scratch_bufin a mutex (0a7b05c), but this was a ~5% performance regression in the replay benchmarks due to contention over the lock. The alternative of just allocating on entry toLog_filehas a relatively small cost.