Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed lockfree frozen vec being too small. #81

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fixed lockfree frozen vec being too small.
  • Loading branch information
davidv1992 committed Jan 29, 2025
commit 9c75ab55715a19c8d3b985fe752a2426e4f389ee
6 changes: 3 additions & 3 deletions src/index_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
}

/// Returns a reference to the value corresponding to the key.
///
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<K>`].
///
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
}

/// Returns the index corresponding to the key
///
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<K>`].
///
Expand Down Expand Up @@ -175,7 +175,7 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
}

/// Returns a reference to the key, along with its index and a reference to its value
///
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<K>`].
///
Expand Down
8 changes: 4 additions & 4 deletions src/index_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S> {
// }

/// Returns a reference to the value passed as argument if present in the set.
///
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<T>`].
///
Expand Down Expand Up @@ -146,10 +146,10 @@ impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S> {
}

/// Returns the index corresponding to the value if present in the set
///
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<T>`].
///
///
/// # Examples
///
/// ```
Expand All @@ -176,7 +176,7 @@ impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S> {

/// Returns a reference to the value passed as argument if present in the set,
/// along with its index
///
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<T>`].
///
Expand Down
6 changes: 4 additions & 2 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,13 +665,15 @@ const fn buffer_index(i: usize) -> usize {
(((usize::BITS + 1 - (i + 1).leading_zeros()) >> 1) - 1) as usize
}

const NUM_BUFFERS: usize = (usize::BITS >> 2) as usize;
/// Each buffer covers 2 bits of address space, so we need half as many
/// buffers as bits in a usize.
const NUM_BUFFERS: usize = (usize::BITS / 2) as usize;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I had an off by one here in my head because I always calculated the total buffer size including the buffer at index NUM_BUFFERS.

Maybe we should grow buffers faster instead of increasing the number of buffers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buffers are already growing at 4x each time it grows, which is quite aggresive. Not increasing the buffer size would require an 8x growth factor, which just seems excessive to me. Especially given that a few non-breaking versions ago (1.7.1/1.8.1) this was only growing 2x at each cutover point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this also means that in some cases 8 times more storage would be used than is strictly required to store the elements. I think that that is not worth the small reduction in number of pointers used.


/// Append-only threadsafe version of `std::vec::Vec` where
/// insertion does not require mutable access.
/// Does not lock for reading, only allows `Copy` types and
/// will spinlock on pushes without affecting reads.
/// Note that this data structure is `64` pointers large on
/// Note that this data structure is `34` pointers large on
/// 64 bit systems,
/// in contrast to `Vec` which is `3` pointers large.
pub struct LockFreeFrozenVec<T: Copy> {
Expand Down