Repository navigation
ThreadSafeBitSet.set: skip the CAS when the bit is already set (performance fix) - #871
Merged
Sunjeet merged 1 commit intoOct 14, 2026
Conversation
Author
|
@Sunjeet @jasonk000 can you please review this one. Thanks |
Member
|
This looks great @arpitmalhotra Can you extend this to Luna flags similar issues in |
…dy in the target state HollowTypeWriteState.add() calls currentCyclePopulated.set(ordinal) for every record written, including the common case where the same record is written many times in a cycle (e.g. shared strings, tags or category ids referenced from many collections). set() always performed a compareAndSet, even when the bit was already set, which forces the cache line holding that word into the exclusive state on every call. For small, frequently referenced types the populated-ordinals bitset is only a few words, so with many populating threads those words become heavily contended. Profiling a 64-core producer showed ~80% of HollowTypeWriteState.add CPU in this CAS loop (AtomicLongArray.compareAndSet via ThreadSafeBitSet.set). Return early from set() when the bit is already set, and from clear() when the bit is already clear. The read is a volatile read of the same word, so a change made by another thread is observed and both methods keep their existing semantics; when the bit does need to change the CAS loop is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
arpitmalhotra
force-pushed
the
fix/threadsafebitset-skip-cas-when-set
branch
from
October 13, 2026 23:42
15f1023 to
7e75011
Compare
Author
|
Thanks @jasonk000, updated to cover the clear() too. Also opened a separate PR for the HollowSparseIntegerSet https://github.com/Netflix/hollow/pull/872/changes. Thanks for flagging |
Sunjeet
approved these changes
Oct 14, 2026
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.
HollowTypeWriteState.add() calls currentCyclePopulated.set(ordinal) for every record written, including the common case where the same record is written many times in a cycle (e.g. shared strings, tags or category ids referenced from many collections). set() always performed a compareAndSet, even when the bit was already set, which forces the cache line holding that word into the exclusive state on every call.
For small, frequently referenced types the populated-ordinals bitset is only a few words, so with many populating threads those words become heavily contended. Profiling a 64-core producer showed ~80% of HollowTypeWriteState.add CPU in this CAS loop (AtomicLongArray.compareAndSet via ThreadSafeBitSet.set).
Return early when the bit is already set. The read is a volatile read of the same word, so a bit set by another thread is observed and the method keeps its existing semantics; when the bit is not yet set the CAS loop is unchanged.