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

simplify raw-loops into while-loops #338

Merged
merged 1 commit into from
Aug 23, 2023
Merged
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
8 changes: 2 additions & 6 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ pub fn node_from_stream(allocator: &mut Allocator, f: &mut Cursor<&[u8]>) -> io:
let mut ops = vec![ParseOp::SExp];

let mut b = [0; 1];
loop {
let op = ops.pop();
if op.is_none() {
break;
}
match op.unwrap() {
while let Some(op) = ops.pop() {
match op {
ParseOp::SExp => {
f.read_exact(&mut b)?;
if b[0] == CONS_BOX_MARKER {
Expand Down
8 changes: 2 additions & 6 deletions src/serde/de_br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ pub fn node_from_stream_backrefs(
let mut ops = vec![ParseOp::SExp];

let mut b = [0; 1];
loop {
let op = ops.pop();
if op.is_none() {
break;
}
match op.unwrap() {
while let Some(op) = ops.pop() {
match op {
ParseOp::SExp => {
f.read_exact(&mut b)?;
if b[0] == CONS_BOX_MARKER {
Expand Down
155 changes: 75 additions & 80 deletions src/serde/de_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,97 +125,92 @@ pub fn parse_triples<R: Read>(
let mut tree_hashes = Vec::new();
let mut op_stack = vec![ParseOpRef::ParseObj];
let mut cursor: u64 = 0;
loop {
match op_stack.pop() {
None => {
break;
}
Some(op) => match op {
ParseOpRef::ParseObj => {
let mut b: [u8; 1] = [0];
f.read_exact(&mut b)?;
let start = cursor;
cursor += 1;
let b = b[0];
if b == CONS_BOX_MARKER {
let index = r.len();
let new_obj = ParsedTriple::Pair {
start,
end: 0,
right_index: 0,
};
r.push(new_obj);
if calculate_tree_hashes {
tree_hashes.push([0; 32])
}
op_stack.push(ParseOpRef::SaveEnd(index));
op_stack.push(ParseOpRef::ParseObj);
op_stack.push(ParseOpRef::SaveRightIndex(index));
op_stack.push(ParseOpRef::ParseObj);
} else {
let (start, end, atom_offset, tree_hash) = {
if b <= MAX_SINGLE_BYTE {
(
start,
start + 1,
0,
tree_hash_for_byte(b, calculate_tree_hashes),
)
} else {
let (atom_offset, atom_size) = decode_size_with_offset(f, b)?;
let end = start + (atom_offset as u64) + atom_size;
let h = skip_or_sha_bytes(f, atom_size, calculate_tree_hashes)?;
(start, end, atom_offset as u32, h)
}
};
if calculate_tree_hashes {
tree_hashes.push(tree_hash.expect("failed unwrap"))
while let Some(op) = op_stack.pop() {
match op {
ParseOpRef::ParseObj => {
let mut b: [u8; 1] = [0];
f.read_exact(&mut b)?;
let start = cursor;
cursor += 1;
let b = b[0];
if b == CONS_BOX_MARKER {
let index = r.len();
let new_obj = ParsedTriple::Pair {
start,
end: 0,
right_index: 0,
};
r.push(new_obj);
if calculate_tree_hashes {
tree_hashes.push([0; 32])
}
op_stack.push(ParseOpRef::SaveEnd(index));
op_stack.push(ParseOpRef::ParseObj);
op_stack.push(ParseOpRef::SaveRightIndex(index));
op_stack.push(ParseOpRef::ParseObj);
} else {
let (start, end, atom_offset, tree_hash) = {
if b <= MAX_SINGLE_BYTE {
(
start,
start + 1,
0,
tree_hash_for_byte(b, calculate_tree_hashes),
)
} else {
let (atom_offset, atom_size) = decode_size_with_offset(f, b)?;
let end = start + (atom_offset as u64) + atom_size;
let h = skip_or_sha_bytes(f, atom_size, calculate_tree_hashes)?;
(start, end, atom_offset as u32, h)
}
let new_obj = ParsedTriple::Atom {
start,
end,
atom_offset,
};
cursor = end;
r.push(new_obj);
};
if calculate_tree_hashes {
tree_hashes.push(tree_hash.expect("failed unwrap"))
}
let new_obj = ParsedTriple::Atom {
start,
end,
atom_offset,
};
cursor = end;
r.push(new_obj);
}
}
ParseOpRef::SaveEnd(index) => match &mut r[index] {
ParsedTriple::Pair {
start: _,
end,
right_index,
} => {
if calculate_tree_hashes {
let h = sha_blobs(&[
&[2],
&tree_hashes[index + 1],
&tree_hashes[*right_index as usize],
]);
tree_hashes[index] = h;
}
*end = cursor;
}
_ => {
panic!("internal error: SaveEnd")
}
ParseOpRef::SaveEnd(index) => match &mut r[index] {
},
ParseOpRef::SaveRightIndex(index) => {
let new_index = r.len() as u32;
match &mut r[index] {
ParsedTriple::Pair {
start: _,
end,
end: _,
right_index,
} => {
if calculate_tree_hashes {
let h = sha_blobs(&[
&[2],
&tree_hashes[index + 1],
&tree_hashes[*right_index as usize],
]);
tree_hashes[index] = h;
}
*end = cursor;
*right_index = new_index;
}
_ => {
panic!("internal error: SaveEnd")
}
},
ParseOpRef::SaveRightIndex(index) => {
let new_index = r.len() as u32;
match &mut r[index] {
ParsedTriple::Pair {
start: _,
end: _,
right_index,
} => {
*right_index = new_index;
}
_ => {
panic!("internal error: SaveRightIndex")
}
panic!("internal error: SaveRightIndex")
}
}
},
}
}
}
Ok((
Expand Down
39 changes: 16 additions & 23 deletions src/serde/object_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,23 @@ impl<'a, T: Clone> ObjectCache<'a, T> {
/// as necessary
fn calculate(&mut self, root_node: &NodePtr) {
let mut obj_list = vec![*root_node];
loop {
match obj_list.pop() {
None => {
return;
}
Some(node) => {
let v = self.get_from_cache(&node);
match v {
Some(_) => {}
None => match (self.f)(self, self.allocator, node) {
None => match self.allocator.sexp(node) {
SExp::Pair(left, right) => {
obj_list.push(node);
obj_list.push(left);
obj_list.push(right);
}
_ => panic!("f returned `None` for atom"),
},
Some(v) => {
self.set(&node, v);
}
},
while let Some(node) = obj_list.pop() {
let v = self.get_from_cache(&node);
match v {
Some(_) => {}
None => match (self.f)(self, self.allocator, node) {
None => match self.allocator.sexp(node) {
SExp::Pair(left, right) => {
obj_list.push(node);
obj_list.push(left);
obj_list.push(right);
}
_ => panic!("f returned `None` for atom"),
},
Some(v) => {
self.set(&node, v);
}
}
},
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/serde/read_cache_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ impl ReadCacheLookup {
seen_ids.insert(id);
let mut partial_paths = vec![(*id, vec![])];

loop {
if partial_paths.is_empty() {
break;
}
while !partial_paths.is_empty() {
let mut new_partial_paths = vec![];
for (node, path) in partial_paths.iter_mut() {
if *node == self.root_hash {
Expand Down
8 changes: 2 additions & 6 deletions src/serde/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,8 @@ pub fn tree_hash_from_stream(f: &mut Cursor<&[u8]>) -> io::Result<[u8; 32]> {
let mut ops = vec![ParseOp::SExp];

let mut b = [0; 1];
loop {
let op = ops.pop();
if op.is_none() {
break;
}
match op.unwrap() {
while let Some(op) = ops.pop() {
match op {
ParseOp::SExp => {
f.read_exact(&mut b)?;
if b[0] == CONS_BOX_MARKER {
Expand Down
Loading