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

perf(store): optimize chunker cloning in stores fixing stack overflow #129

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
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions monofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ typed-builder.workspace = true

[dev-dependencies]
test-log.workspace = true
gag = "1.0"
os_pipe = "1.1"
39 changes: 3 additions & 36 deletions monofs/examples/flatfs_monofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ use clap::Parser;
use monofs::{
filesystem::{Dir, Entity, File},
store::FlatFsStore,
utils,
};
use monoutils_store::{ipld::cid::Cid, IpldStore, Storable};
use std::future::Future;
use std::pin::Pin;
use tokio::io::AsyncWriteExt;

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -92,7 +91,7 @@ async fn main() -> Result<()> {

// Always display the current filesystem structure
println!("\nFilesystem contents:");
print_dir_contents(&root, 0).await?;
utils::print_dir_tree(&root).await?;

Ok(())
}
Expand Down Expand Up @@ -158,7 +157,7 @@ async fn perform_example_operations<S: IpldStore + Send + Sync + 'static>(

// 3. List contents recursively
println!("\n3. Listing filesystem contents:");
print_dir_contents(root, 0).await?;
utils::print_dir_tree(root).await?;

// 4. Demonstrate file operations
println!("\n4. Demonstrating file operations:");
Expand All @@ -178,35 +177,3 @@ async fn perform_example_operations<S: IpldStore + Send + Sync + 'static>(

Ok(())
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

/// Recursively prints the contents of a directory with proper indentation
fn print_dir_contents<S: IpldStore + Send + Sync + 'static>(
dir: &Dir<S>,
depth: usize,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>> {
Box::pin(async move {
let indent = " ".repeat(depth);

for (name, link) in dir.get_entries() {
let entity = link.resolve_entity(dir.get_store().clone()).await?;

match entity {
Entity::Dir(subdir) => {
println!("{}📁 {}/", indent, name);
print_dir_contents(&subdir, depth + 1).await?;
}
Entity::File(file) => {
let size = file.get_size().await?;
println!("{}📄 {} ({} bytes)", indent, name, size);
}
_ => println!("{}🔗 {}", indent, name),
}
}

Ok(())
})
}
2 changes: 1 addition & 1 deletion monofs/lib/filesystem/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub const DIR_TYPE_TAG: &str = "monofs.dir";
/// Entities in `monofs` are designed to be immutable and clone-on-write meaning writes create
/// forks of the entity.
#[derive(Clone)]
pub struct Dir<S>
pub struct Dir<S>
where
S: IpldStore,
{
Expand Down
1 change: 1 addition & 0 deletions monofs/lib/filesystem/dir/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ mod tests {
// Create a complex rename scenario and verify store state
dir.find_or_create("state_test/source/file.txt", true)
.await?;

// Create target directory first
dir.find_or_create("state_test/target", false).await?;

Expand Down
33 changes: 22 additions & 11 deletions monofs/lib/store/flatfsstore.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::Arc;
use std::{collections::HashSet, path::PathBuf, pin::Pin};

use async_trait::async_trait;
Expand Down Expand Up @@ -111,11 +112,15 @@ where
{
#[builder(setter(into))]
path: PathBuf,

dir_levels: DirLevels,
#[builder(default = C::default())]
chunker: C,
#[builder(default = L::default())]
layout: L,

#[builder(default = Arc::new(C::default()))]
chunker: Arc<C>,

#[builder(default = Arc::new(L::default()))]
layout: Arc<L>,

#[builder(default = true)]
enable_refcount: bool,
}
Expand Down Expand Up @@ -890,18 +895,24 @@ mod tests {

// ======================== Tree Structure ========================
//
// root[value: 5, rc: 0]
// root[val: 5, rc: 0]
// / \
// / \
// / \
// middle1[value: 3, rc: 1] middle2[value: 4, rc: 1]
// / \ / \
// / \ / \
// / \ / \
// leaf1[value: 1, rc: 1] leaf2[value: 2, rc: 2] \
// / \
// / \
// middle1[val: 3, rc: 1] middle2[val: 4, rc: 1]
// / \ / \
// / \ / \
// / \ / \
// / \ / \
// / \ / \
// leaf1[val: 1, rc: 1] leaf2[val: 2, rc: 2] \
// | | \
// | | \
// | | \
// data1[rc: 1] data2[rc: 1] data3[rc: 1]
// | | \
// data1[rc: 1] data2[rc: 1] data3[rc: 1]
//
// ===============================================================

Expand Down
Loading