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

Zerocopy RsVec #14

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 11 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ env:
RUSTFLAGS: -C target-cpu=native

jobs:
build:

test_serde:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose --features "simd serde"
- name: Run tests
run: cargo test --verbose --features "simd serde"
test_zerocopy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose --all-features
run: cargo build --verbose --features "simd zerocopy"
- name: Run tests
run: cargo test --verbose --all-features
run: cargo test --verbose --features "simd zerocopy"
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ exclude = [
]

[dependencies]
anybytes = { version = "0.11.0", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
zerocopy = { version = "0.7.35", optional = true, features = ["derive"] }

[dev-dependencies]
# benchmarking
criterion = { version = "0.5.1", features = ["html_reports"] }
rand = { version = "0.8", features = ["alloc"] }

[features]
simd = []
docsrs = [] # special feature for docs.rs to enable doc_auto_cfg on nightly
simd = []
serde = ["dep:serde"]
zerocopy = ["dep:anybytes", "dep:zerocopy"]

[[bench]]
name = "rank"
Expand Down
32 changes: 25 additions & 7 deletions src/bit_vec/fast_rs_vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use std::mem::size_of;

#[cfg(feature = "zerocopy")]
use anybytes::PackedSlice;

#[cfg(all(
feature = "simd",
target_arch = "x86_64",
Expand Down Expand Up @@ -42,6 +45,7 @@ const SELECT_BLOCK_SIZE: usize = 1 << 13;
/// always stores the number zero, which serves as a sentinel value to avoid special-casing the
/// first block in a super-block (which would be a performance hit due branch prediction failures).
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "zerocopy", repr(C), derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct BlockDescriptor {
zeros: u16,
Expand All @@ -51,6 +55,7 @@ struct BlockDescriptor {
/// This allows the `BlockDescriptor` to store the number of zeros in a much smaller
/// space. The `zeros` field is the number of zeros up to the super-block.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "zerocopy", repr(C), derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct SuperBlockDescriptor {
zeros: usize,
Expand All @@ -60,6 +65,7 @@ struct SuperBlockDescriptor {
/// the i * `SELECT_BLOCK_SIZE`'th 0- and 1-bit in the bitvector. Those indices may be very far apart.
/// The indices do not point into the bit-vector, but into the super-block vector.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "zerocopy", repr(C), derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct SelectSuperBlockDescriptor {
index_0: usize,
Expand All @@ -85,13 +91,25 @@ struct SelectSuperBlockDescriptor {
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RsVec {
data: Vec<u64>,
len: usize,
pub(crate) rank0: usize,
pub(crate) rank1: usize,
#[cfg(not(feature = "zerocopy"))]
data: Vec<u64>,
#[cfg(not(feature = "zerocopy"))]
blocks: Vec<BlockDescriptor>,
#[cfg(not(feature = "zerocopy"))]
super_blocks: Vec<SuperBlockDescriptor>,
#[cfg(not(feature = "zerocopy"))]
select_blocks: Vec<SelectSuperBlockDescriptor>,
pub(crate) rank0: usize,
pub(crate) rank1: usize,
#[cfg(feature = "zerocopy")]
data: PackedSlice<u64>,
#[cfg(feature = "zerocopy")]
blocks: PackedSlice<BlockDescriptor>,
#[cfg(feature = "zerocopy")]
super_blocks: PackedSlice<SuperBlockDescriptor>,
#[cfg(feature = "zerocopy")]
select_blocks: PackedSlice<SelectSuperBlockDescriptor>,
}

impl RsVec {
Expand Down Expand Up @@ -212,11 +230,11 @@ impl RsVec {
}

RsVec {
data: vec.data,
data: vec.data.into(),
len: vec.len,
blocks,
super_blocks,
select_blocks,
blocks: blocks.into(),
super_blocks: super_blocks.into(),
select_blocks: select_blocks.into(),
// the last block may contain padding zeros, which should not be counted
rank0: total_zeros + current_zeros - ((WORD_SIZE - (vec.len % WORD_SIZE)) % WORD_SIZE),
rank1: vec.len
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
//! - `serde` (disabled by default): Enables serialization and deserialization support for all
//! data structures in this crate using the `serde` crate.

#[cfg(all(feature = "serde", feature = "zerocopy"))]
compile_error!("`serde` and `zerocopy` are mutually excusive features");

pub use bit_vec::fast_rs_vec::RsVec;
pub use bit_vec::BitVec;
pub use elias_fano::EliasFanoVec;
Expand Down
Loading