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

Some low_hanging_fruit #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 9 additions & 14 deletions src/swapvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ pub enum Compression {

/// Configure when and how the vector should swap.
///
/// The file creation will happen after max(swap_after, batch_size)
/// The file creation will happen after max(`swap_after`, `batch_size`)
/// elements.
///
/// Keep in mind, that if the temporary file exists,
/// after ever batch_size elements, at least one write (syscall)
/// after ever `batch_size` elements, at least one write (syscall)
/// will happen.
#[derive(Debug, Clone)]
pub struct SwapVecConfig {
Expand Down Expand Up @@ -128,11 +128,7 @@ impl<T: Serialize + for<'a> Deserialize<'a>> Debug for SwapVec<T> {
f,
"SwapVec {{elements_in_ram: {}, elements_in_file: {}, filedescriptor: {:#?}}}",
self.vector.len(),
self.tempfile
.as_ref()
.map(|x| x.batch_info.len())
.unwrap_or(0)
* self.config.batch_size,
self.tempfile.as_ref().map_or(0, |x| x.batch_info.len()) * self.config.batch_size,
self.tempfile.as_ref().map(|x| x.file.as_raw_fd())
)
}
Expand Down Expand Up @@ -169,7 +165,7 @@ where
}

/// Check if a file has been created.
/// Is false if element count is below swap_after and below batch_size
/// Is false if element count is below `swap_after` and below `batch_size`
pub fn written_to_file(&self) -> bool {
self.tempfile.is_some()
}
Expand All @@ -186,21 +182,20 @@ where
}
}

/// Basically elements pushed // batch_size
/// Basically elements pushed // `batch_size`
pub fn batches_written(&self) -> usize {
match self.tempfile.as_ref() {
None => 0,
Some(f) => f.batch_info.len(),
}
self.tempfile.as_ref().map_or(0, |f| f.batch_info.len())
}

fn after_push_work(&mut self) -> Result<(), SwapVecError> {
if self.vector.len() < self.config.batch_size {
return Ok(());
}

if self.tempfile.is_none() && self.vector.len() < self.config.swap_after {
return Ok(());
}

// Do action
if self.tempfile.is_none() {
let tf = tempfile::tempfile()?;
Expand All @@ -215,7 +210,7 @@ where
.collect::<Vec<_>>();

let mut batch_hash = DefaultHasher::new();
batch.iter().for_each(|x| x.hash(&mut batch_hash));
batch.hash(&mut batch_hash);

let buffer = bincode::serialize(&batch)?;
self.tempfile.as_mut().unwrap().write_all(&buffer)?;
Expand Down
15 changes: 4 additions & 11 deletions src/swapveciter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct CheckedFileRead {
pub batch_info_rev: Vec<BatchInfo>,
}

/// Iterator for SwapVec.
/// Iterator for `SwapVec`.
///
/// Items might be read from disk,
/// so every item is wrapped in a `Result`.
Expand Down Expand Up @@ -128,15 +128,8 @@ impl<T: Serialize + for<'a> Deserialize<'a> + Hash> Iterator for SwapVecIter<T>
return Some(Ok(item));
}

let next_in_batch = self.next_in_batch();
if let Err(err) = next_in_batch {
return Some(Err(err));
}
if let Ok(Some(item)) = next_in_batch {
return Some(Ok(item));
}

// File has been exhausted.
self.last_elements.pop_front().map(|x| Ok(x))
self.next_in_batch()
.transpose()
.or_else(|| self.last_elements.pop_front().map(Ok))
}
}