Skip to content

Commit

Permalink
chore: replace Vec<bool> with bitvec (#2360)
Browse files Browse the repository at this point in the history
bitvec is 8x smaller than Vec<bool>, tests show there's no performance
diff

---------

Signed-off-by: BubbleCal <[email protected]>
  • Loading branch information
BubbleCal authored May 22, 2024
1 parent 0fce54e commit ee58018
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ half = { "version" = "2.4.1", default-features = false, features = [
"num-traits",
"std",
] }
bitvec = "1.0.1"
bytes = "1.4"
byteorder = "1.5"
clap = { version = "4", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions rust/lance-index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ arrow-schema.workspace = true
arrow-select.workspace = true
async-recursion.workspace = true
async-trait.workspace = true
bitvec.workspace = true
datafusion-common.workspace = true
datafusion-expr.workspace = true
datafusion-physical-expr.workspace = true
Expand Down
11 changes: 6 additions & 5 deletions rust/lance-index/src/vector/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::collections::BinaryHeap;
use std::sync::Arc;

use arrow_schema::{DataType, Field};
use bitvec::vec::BitVec;
use lance_core::Result;

pub mod builder;
Expand Down Expand Up @@ -150,15 +151,15 @@ pub trait Graph {

/// Array-based visited list (faster than HashSet)
pub struct Visited<'a> {
visited: &'a mut [bool],
visited: &'a mut BitVec,
recently_visited: Vec<u32>,
}

impl<'a> Visited<'a> {
pub fn insert(&mut self, node_id: u32) {
let node_id_usize = node_id as usize;
if !self.visited[node_id_usize] {
self.visited[node_id_usize] = true;
self.visited.set(node_id_usize, true);
self.recently_visited.push(node_id);
}
}
Expand All @@ -172,22 +173,22 @@ impl<'a> Visited<'a> {
impl<'a> Drop for Visited<'a> {
fn drop(&mut self) {
for node_id in self.recently_visited.iter() {
self.visited[*node_id as usize] = false;
self.visited.set(*node_id as usize, false);
}
self.recently_visited.clear();
}
}

#[derive(Debug, Clone)]
pub struct VisitedGenerator {
visited: Vec<bool>,
visited: BitVec,
capacity: usize,
}

impl VisitedGenerator {
pub fn new(capacity: usize) -> Self {
Self {
visited: vec![false; capacity],
visited: BitVec::repeat(false, capacity),
capacity,
}
}
Expand Down

0 comments on commit ee58018

Please sign in to comment.