Skip to content

Commit

Permalink
Merge pull request #15 from drdo/rustfmt
Browse files Browse the repository at this point in the history
Rustfmt
  • Loading branch information
drdo authored May 28, 2024
2 parents 1cf57f5 + 2222b6c commit fc58eb7
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 420 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Rustfmt Check
run: cargo fmt --check
- name: Build
run: cargo build --verbose
- name: Run tests
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/scripts/target
.DS_Store
.idea
/scripts/target
/target
19 changes: 10 additions & 9 deletions benches/cache.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
use std::cell::Cell;
use std::fs;
use std::path::PathBuf;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use uuid::Uuid;
use redu::cache::{Cache, SnapshotGroup};

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use redu::cache::tests::*;
use redu::cache::{Cache, SnapshotGroup};
use uuid::Uuid;

pub fn criterion_benchmark(c: &mut Criterion) {

c.bench_function("merge filetree", |b| {
let filetree0 = Cell::new(generate_filetree(black_box(6), black_box(12)));
let filetree1 = Cell::new(generate_filetree(black_box(5), black_box(14)));
let filetree0 =
Cell::new(generate_filetree(black_box(6), black_box(12)));
let filetree1 =
Cell::new(generate_filetree(black_box(5), black_box(14)));
b.iter(move || filetree0.take().merge(black_box(filetree1.take())));
});

c.bench_function("create and save group", |b| {
let file: PathBuf = Uuid::new_v4().to_string().into();
{
let mut cache = Cache::open(&file).unwrap();
b.iter(move || {
let mut group = SnapshotGroup::new();
group.add_snapshot(
"foo".into()
, generate_filetree(black_box(6), black_box(12))
"foo".into(),
generate_filetree(black_box(6), black_box(12)),
);
cache.save_snapshot_group(group).unwrap()
});
Expand Down
8 changes: 8 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
max_width = 80
match_arm_blocks = false
error_on_line_overflow = true
imports_granularity = "Module"
overflow_delimited_expr = true
group_imports = "StdExternalCrate"
use_small_heuristics = "Max"
use_field_init_shorthand = true
113 changes: 63 additions & 50 deletions src/cache/filetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ pub struct EntryExistsError;

impl FileTree {
pub fn new() -> FileTree {
FileTree {
size: 0,
children: HashMap::new(),
}
FileTree { size: 0, children: HashMap::new() }
}

pub fn insert(
&mut self,
path: &Utf8Path,
size: usize,
) -> Result<(), EntryExistsError>
{
) -> Result<(), EntryExistsError> {
let (mut breadcrumbs, remaining) = {
let (breadcrumbs, remaining) = self.find(path);
(breadcrumbs, remaining.map(Ok).unwrap_or(Err(EntryExistsError))?)
Expand All @@ -38,16 +34,19 @@ impl FileTree {
}
let mut current = unsafe { &mut **breadcrumbs.last().unwrap() };
for c in remaining.iter() {
current = current.children.entry(Box::from(c)).or_insert(FileTree::new());
current =
current.children.entry(Box::from(c)).or_insert(FileTree::new());
current.size = size;
}
Ok(())
}

pub fn merge(self, other: FileTree) -> Self {
fn sorted_children(filetree: FileTree) -> Vec<(Box<str>, FileTree)> {
let mut children = filetree.children.into_iter().collect::<Vec<_>>();
children.sort_unstable_by(|(name0, _), (name1, _)| name0.cmp(name1));
let mut children =
filetree.children.into_iter().collect::<Vec<_>>();
children
.sort_unstable_by(|(name0, _), (name1, _)| name0.cmp(name1));
children
}

Expand All @@ -65,21 +64,27 @@ impl FileTree {
children.insert(name1, tree1);
}
}
(None, Some((name, tree))) => { children.insert(name, tree); }
(Some((name, tree)), None) => { children.insert(name, tree); }
(None, None) => { break; }
(None, Some((name, tree))) => {
children.insert(name, tree);
}
(Some((name, tree)), None) => {
children.insert(name, tree);
}
(None, None) => {
break;
}
}
}
FileTree { size, children }
}

pub fn iter(&self) -> Iter {
Iter {
stack: vec![Breadcrumb {
path: None,
size: self.size,
children: self.children.iter(),
}]
}],
}
}

Expand All @@ -90,8 +95,7 @@ impl FileTree {
fn find(
&mut self,
path: &Utf8Path,
) -> (Vec<*mut FileTree>, Option<Utf8PathBuf>)
{
) -> (Vec<*mut FileTree>, Option<Utf8PathBuf>) {
let mut breadcrumbs: Vec<*mut FileTree> = vec![self];
let mut prefix = Utf8PathBuf::new();
for c in path.iter() {
Expand All @@ -106,8 +110,11 @@ impl FileTree {
}
let remaining_path = {
let suffix = path.strip_prefix(prefix).unwrap();
if suffix.as_str().is_empty() { None }
else { Some(suffix.to_path_buf()) }
if suffix.as_str().is_empty() {
None
} else {
Some(suffix.to_path_buf())
}
};
(breadcrumbs, remaining_path)
}
Expand Down Expand Up @@ -139,31 +146,33 @@ impl<'a> Iterator for Iter<'a> {
loop {
match self.stack.pop() {
None => break None,
Some(mut breadcrumb) => {
match breadcrumb.children.next() {
None =>
break breadcrumb.path.map(|p| {
let dir = Directory{ path: p, size: breadcrumb.size };
Entry::Directory(dir)
}),
Some((name, tree)) => {
let new_path = breadcrumb.path_extend(name);
self.stack.push(breadcrumb);
if tree.children.is_empty() {
break Some(Entry::File(File { path: new_path, size: tree.size }))
} else {
let new_breadcrumb = {
Breadcrumb {
path: Some(new_path),
size: tree.size,
children: tree.children.iter(),
}
};
self.stack.push(new_breadcrumb);
}
Some(mut breadcrumb) => match breadcrumb.children.next() {
None =>
break breadcrumb.path.map(|p| {
let dir =
Directory { path: p, size: breadcrumb.size };
Entry::Directory(dir)
}),
Some((name, tree)) => {
let new_path = breadcrumb.path_extend(name);
self.stack.push(breadcrumb);
if tree.children.is_empty() {
break Some(Entry::File(File {
path: new_path,
size: tree.size,
}));
} else {
let new_breadcrumb = {
Breadcrumb {
path: Some(new_path),
size: tree.size,
children: tree.children.iter(),
}
};
self.stack.push(new_breadcrumb);
}
}
}
},
}
}
}
Expand Down Expand Up @@ -207,7 +216,7 @@ mod tests {
assert_eq!(filetree.insert("a/1/x/1".into(), 1), Ok(()));
filetree
}

#[test]
fn insert_uniques_0() {
let mut entries = example_tree_0().iter().collect::<Vec<_>>();
Expand Down Expand Up @@ -245,15 +254,15 @@ mod tests {
Entry::File(File { path: "a/2/x/0".into(), size: 7 }),
]);
}

#[test]
fn insert_existing() {
let mut filetree = example_tree_0();
assert_eq!(filetree.insert("".into(), 1), Err(EntryExistsError));
assert_eq!(filetree.insert("a/0".into(), 1), Err(EntryExistsError));
assert_eq!(filetree.insert("a/0/z/0".into(), 1), Err(EntryExistsError));
}

#[test]
fn merge_test() {
let filetree = example_tree_0().merge(example_tree_1());
Expand All @@ -275,7 +284,7 @@ mod tests {
Entry::File(File { path: "a/2/x/0".into(), size: 7 }),
]);
}

#[test]
fn merge_reflexivity() {
assert_eq!(example_tree_0().merge(example_tree_0()), example_tree_0());
Expand All @@ -284,13 +293,17 @@ mod tests {

#[test]
fn merge_associativity() {
assert_eq!(example_tree_0().merge(example_tree_1()).merge(example_tree_2()),
example_tree_0().merge(example_tree_1().merge(example_tree_2())));
assert_eq!(
example_tree_0().merge(example_tree_1()).merge(example_tree_2()),
example_tree_0().merge(example_tree_1().merge(example_tree_2()))
);
}

#[test]
fn merge_commutativity() {
assert_eq!(example_tree_0().merge(example_tree_1()),
example_tree_1().merge(example_tree_0()));
assert_eq!(
example_tree_0().merge(example_tree_1()),
example_tree_1().merge(example_tree_0())
);
}
}
Loading

0 comments on commit fc58eb7

Please sign in to comment.