Skip to content
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
5 changes: 5 additions & 0 deletions examples/arena_compact_tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ fn arena_create() -> Result<(), std::io::Error> {
println!("len {:.2?}", tree.get_data().len());
// pathmap::alloc_tracking::read().print();
// pathmap::alloc_tracking::reset();
pathmap::timed_span::print_counters();
pathmap::timed_span::reset_counters();

let start = Instant::now();
let mut zipper = tree.read_zipper();
Expand All @@ -38,6 +40,8 @@ fn arena_create() -> Result<(), std::io::Error> {
assert!(zipper.descend_to_existing(path) == path.len());
// assert_eq!(zipper.path(), path);
}
pathmap::timed_span::print_counters();

println!("checked act in {:.2?}", start.elapsed());
let start = Instant::now();
let tree2 = ArenaCompactTree::from_zipper(tree.read_zipper(), |_v| 0);
Expand Down Expand Up @@ -78,6 +82,7 @@ fn arena_dump() -> Result<(), std::io::Error> {
assert!(zipper.descend_to_existing(path) == path.len());
// assert_eq!(zipper.path(), path);
}
pathmap::timed_span::print_counters();
println!("checked act in {:.2?}", start.elapsed());
let start = Instant::now();
let tree2 = ArenaCompactTree::from_zipper(tree.read_zipper(), |_v| 0);
Expand Down
23 changes: 22 additions & 1 deletion src/arena_compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ use std::{io::Write, hash::Hasher};
use crate::{
morphisms::Catamorphism,
utils::{BitMask, ByteMask, find_prefix_overlap},
timed_span::timed_span,
zipper::{
Zipper, ZipperValues, ZipperForking, ZipperAbsolutePath, ZipperIteration,
ZipperMoving, ZipperPathBuffer, ZipperReadOnlyValues,
Expand Down Expand Up @@ -1664,10 +1665,13 @@ impl<'tree, Storage> ZipperMoving for ACTZipper<'tree, Storage>
where Storage: AsRef<[u8]>
{
/// Returns `true` if the zipper cannot ascend further, otherwise returns `false`
fn at_root(&self) -> bool { self.path.len() <= self.origin_depth }
fn at_root(&self) -> bool {
self.path.len() <= self.origin_depth
}

/// Resets the zipper's focus back to the root
fn reset(&mut self) {
timed_span!(Reset);
// self.ascend(self.path.len() - self.origin_depth);
self.path.truncate(self.origin_depth);
self.cur_node = self.tree.get_node(self.stack[0].node_id).0;
Expand All @@ -1682,6 +1686,7 @@ where Storage: AsRef<[u8]>
///
/// WARNING: This is not a cheap method. It may have an order-N cost
fn val_count(&self) -> usize {
timed_span!(ValueCount);
let mut zipper = self.clone();
zipper.reset();
let mut count = 0;
Expand All @@ -1699,6 +1704,7 @@ where Storage: AsRef<[u8]>
/// Returns `true` if the zipper points to an existing path within the tree, otherwise `false`. The
/// zipper's location will be updated, regardless of whether or not the path exists within the tree.
fn descend_to<P: AsRef<[u8]>>(&mut self, path: P) -> bool {
timed_span!(DescendTo);
let path = path.as_ref();
let depth = path.len();
let descended = self.descend_to_existing(path);
Expand All @@ -1718,6 +1724,7 @@ where Storage: AsRef<[u8]>
/// existing path after this method returns, unless the method was called with the focus on a
/// non-existent path.
fn descend_to_existing<P: AsRef<[u8]>>(&mut self, path: P) -> usize {
timed_span!(DescendToExisting);
self.descend_cond(path.as_ref(), false)
}

Expand All @@ -1729,12 +1736,14 @@ where Storage: AsRef<[u8]>
/// If the focus is already on a value, this method will descend to the *next* value along
/// the path.
fn descend_to_value<K: AsRef<[u8]>>(&mut self, path: K) -> usize {
timed_span!(DescendToValue);
self.descend_cond(path.as_ref(), true)
}

/// Moves the zipper one byte deeper into the trie. Identical in effect to [descend_to](Self::descend_to)
/// with a 1-byte key argument
fn descend_to_byte(&mut self, k: u8) -> bool {
timed_span!(DescendToByte);
self.descend_to(&[k])
}

Expand All @@ -1746,6 +1755,7 @@ where Storage: AsRef<[u8]>
/// to the trie. This method should only be used as part of a directed traversal operation, but
/// index-based paths may not be stored as locations within the trie.
fn descend_indexed_byte(&mut self, idx: usize) -> bool {
timed_span!(DescendIndexedBranch);
if self.invalid > 0 {
return false;
}
Expand Down Expand Up @@ -1800,12 +1810,14 @@ where Storage: AsRef<[u8]>
/// NOTE: This method should have identical behavior to passing `0` to [descend_indexed_byte](ZipperMoving::descend_indexed_byte),
/// although with less overhead
fn descend_first_byte(&mut self) -> bool {
timed_span!(DescendFirstByte);
self.descend_indexed_byte(0)
}

/// Descends the zipper's focus until a branch or a value is encountered. Returns `true` if the focus
/// moved otherwise returns `false`
fn descend_until(&mut self) -> bool {
timed_span!(DescendUntil);
self.trace_pos();
let mut descended = false;
'descend: while self.child_count() == 1 {
Expand Down Expand Up @@ -1856,6 +1868,7 @@ where Storage: AsRef<[u8]>
/// If the root is fewer than `n` steps from the zipper's position, then this method will stop at
/// the root and return `false`
fn ascend(&mut self, mut steps: usize) -> bool {
timed_span!(Ascend);
self.trace_pos();
if !self.ascend_invalid(Some(steps)) {
return false;
Expand All @@ -1882,29 +1895,34 @@ where Storage: AsRef<[u8]>

/// Ascends the zipper up a single byte. Equivalent to passing `1` to [ascend](Self::ascend)
fn ascend_byte(&mut self) -> bool {
timed_span!(AscendByte);
self.ascend(1)
}

/// Ascends the zipper to the nearest upstream branch point or value. Returns `true` if the zipper
/// focus moved upwards, otherwise returns `false` if the zipper was already at the root
fn ascend_until(&mut self) -> bool {
timed_span!(AscendUntil);
self.ascend_to_branch(true)
}

/// Ascends the zipper to the nearest upstream branch point, skipping over values along the way. Returns
/// `true` if the zipper focus moved upwards, otherwise returns `false` if the zipper was already at the
/// root
fn ascend_until_branch(&mut self) -> bool {
timed_span!(AscendUntilBranch);
self.ascend_to_branch(false)
}

#[inline]
fn to_next_sibling_byte(&mut self) -> bool {
timed_span!(ToNextSiblingByte);
self.to_sibling(true)
}

#[inline]
fn to_prev_sibling_byte(&mut self) -> bool {
timed_span!(ToPrevSiblingByte);
self.to_sibling(false)
}

Expand All @@ -1920,6 +1938,7 @@ where Storage: AsRef<[u8]>
///
/// Returns a reference to the value or `None` if the zipper has encountered the root.
fn to_next_val(&mut self) -> bool {
timed_span!(ToNextVal);
while self.to_next_step() {
if self.is_val() {
return true;
Expand All @@ -1939,6 +1958,7 @@ where Storage: AsRef<[u8]>
///
/// See: [to_next_k_path](ZipperIteration::to_next_k_path)
fn descend_first_k_path(&mut self, k: usize) -> bool {
timed_span!(DescendFirstKPath);
for ii in 0..k {
if !self.descend_first_byte() {
self.ascend(ii);
Expand All @@ -1960,6 +1980,7 @@ where Storage: AsRef<[u8]>
///
/// See: [descend_first_k_path](ZipperIteration::descend_first_k_path)
fn to_next_k_path(&mut self, k: usize) -> bool {
timed_span!(ToNextKPath);
let mut depth = k;
'outer: loop {
while depth > 0 && self.child_count() <= 1 {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub mod fuzzer;
#[cfg(feature = "counters")]
pub mod counters;

/// Feature for code instrumentation and optimization
pub mod timed_span;

/// Shims to allow the use of a custom [`Allocator`](std::alloc::Allocator) type, if running with the `nightly` feature. Does nothing otherwise
pub mod alloc;

Expand Down
2 changes: 1 addition & 1 deletion src/path_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn deserialize_paths_<V: TrieValue, A: Allocator, WZ : ZipperWriting<V, A>,
deserialize_paths(wz, source, |_, _| v.clone())
}

pub fn deserialize_paths<V: TrieValue, A: Allocator, WZ : ZipperWriting<V, A>, R: std::io::Read, F: Fn(usize, &[u8]) -> V>(mut wz: WZ, mut source: R, fv: F) -> std::io::Result<DeserializationStats> {
pub fn deserialize_paths<V: TrieValue, A: Allocator, WZ : ZipperWriting<V, A>, R: std::io::Read, F: Fn(usize, &[u8]) -> V>(mut wz: WZ, source: R, fv: F) -> std::io::Result<DeserializationStats> {
let mut submap = PathMap::new_in(wz.alloc());
let r = for_each_deserialized_path(source, |k, p| {
let v = fv(k, p);
Expand Down
Loading