Skip to content

Commit

Permalink
Removes unsafe vector allocators
Browse files Browse the repository at this point in the history
Note to self: before merging this, we should do a bit
more benchmarking to see how this actually affects perf.

From #163, it seems negligble.
  • Loading branch information
mattxwang committed Aug 3, 2023
1 parent af21705 commit a1b2c83
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/backing_store/bump_table.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A unique table based on a bump allocator and robin-hood hashing
//! this is the primary unique table for storing all nodes

use crate::{backing_store::UniqueTable, util::*};
use crate::backing_store::UniqueTable;
use bumpalo::Bump;
use rustc_hash::FxHasher;
use std::{
Expand Down Expand Up @@ -105,7 +105,7 @@ where
{
/// reserve a robin-hood table capable of holding at least `sz` elements
pub fn new() -> BackedRobinhoodTable<'a, T> {
let v: Vec<HashTableElement<T>> = zero_vec(DEFAULT_SIZE);
let v: Vec<HashTableElement<T>> = vec![HashTableElement::default(); DEFAULT_SIZE];

BackedRobinhoodTable {
tbl: v,
Expand Down Expand Up @@ -135,7 +135,7 @@ where
pub fn grow(&mut self) {
let new_sz = (self.cap + 1).next_power_of_two();
self.cap = new_sz;
let old = mem::replace(&mut self.tbl, zero_vec(new_sz));
let old = mem::replace(&mut self.tbl, vec![HashTableElement::default(); new_sz]);
let c = self.cap;
for i in old.iter() {
propagate(&mut self.tbl, self.cap, i.clone(), (i.hash as usize) % c);
Expand Down
4 changes: 2 additions & 2 deletions src/repr/var_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! in the order occur first in the BDD, starting from the root.
//! Lower numbers occur first in the order (i.e., closer to the root)

use crate::{repr::var_label::VarLabel, util};
use crate::repr::var_label::VarLabel;
use std::fmt::{Debug, Display};

#[derive(Debug, Clone)]
Expand All @@ -19,7 +19,7 @@ impl VarOrder {
/// Creates a new variable order (elements that occur first in the vector
/// occur first in the order)
pub fn new(order: Vec<VarLabel>) -> VarOrder {
let mut v = util::malloc_vec(order.len());
let mut v = vec![0; order.len()];
let mut pos_to_var = Vec::new();
for i in 0..order.len() {
v[order[i].value() as usize] = i;
Expand Down
23 changes: 0 additions & 23 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ pub mod hypergraph;
pub mod lru;
pub mod semirings;

use std::ptr;

/// A generic bit-field which makes it easier to get and set
/// bit-level fields
#[macro_export]
Expand All @@ -30,24 +28,3 @@ macro_rules! BITFIELD {
)+}
}
}

/// custom allocations for zeroed vectors
pub fn zero_vec<T>(sz: usize) -> Vec<T> {
let mut v: Vec<T> = Vec::with_capacity(sz);
unsafe {
let vec_ptr = v.as_mut_ptr();
ptr::write_bytes(vec_ptr, 0, sz);
v.set_len(sz);
}
v
}

/// custom allocation of a non-initialized vector
#[allow(clippy::uninit_vec)] // intentional!
pub fn malloc_vec<T>(sz: usize) -> Vec<T> {
let mut v: Vec<T> = Vec::with_capacity(sz);
unsafe {
v.set_len(sz);
}
v
}

0 comments on commit a1b2c83

Please sign in to comment.