From c9e14dc8a3be31f7dd4755ebbc94ddbed6e176f0 Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Mon, 30 Sep 2024 11:16:43 +0200 Subject: [PATCH] piecrust: write commit performance optimizations --- piecrust/CHANGELOG.md | 2 ++ piecrust/src/store.rs | 26 ++++++++++++-------------- piecrust/src/store/session.rs | 1 - 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/piecrust/CHANGELOG.md b/piecrust/CHANGELOG.md index 7a73d808..9d59b27c 100644 --- a/piecrust/CHANGELOG.md +++ b/piecrust/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Improved commit writing time [#388] - Changed state representation to improve commit performance [#342] ## [0.24.0] - 2024-09-04 @@ -465,6 +466,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 +[#388]: https://github.com/dusk-network/piecrust/issues/388 [#371]: https://github.com/dusk-network/piecrust/issues/371 [#359]: https://github.com/dusk-network/piecrust/issues/359 [#357]: https://github.com/dusk-network/piecrust/issues/357 diff --git a/piecrust/src/store.rs b/piecrust/src/store.rs index 295dffd6..df49eb2e 100644 --- a/piecrust/src/store.rs +++ b/piecrust/src/store.rs @@ -360,7 +360,7 @@ pub(crate) enum Call { Commit { contracts: BTreeMap, base: Option, - replier: mpsc::SyncSender>, + replier: mpsc::SyncSender>, }, GetCommits { replier: mpsc::SyncSender>, @@ -510,7 +510,7 @@ fn write_commit>( commits: &mut BTreeMap, base: Option, commit_contracts: BTreeMap, -) -> io::Result { +) -> io::Result { let root_dir = root_dir.as_ref(); let mut index = base @@ -530,14 +530,14 @@ fn write_commit>( // Don't write the commit if it already exists on disk. This may happen if // the same transactions on the same base commit for example. - if let Some(commit) = commits.get(&root) { - return Ok(commit.clone()); + if commits.contains_key(&root) { + return Ok(root); } write_commit_inner(root_dir, index, commit_contracts, root_hex, base).map( |commit| { - commits.insert(root, commit.clone()); - commit + commits.insert(root, commit); + root }, ) } @@ -622,14 +622,12 @@ fn write_commit_inner, S: AsRef>( } let index_main_path = index_path_main(directories.main_dir, commit_id)?; - let index_bytes = rkyv::to_bytes::<_, 128>(&index) - .map_err(|err| { - io::Error::new( - io::ErrorKind::InvalidData, - format!("Failed serializing index file: {err}"), - ) - })? - .to_vec(); + let index_bytes = rkyv::to_bytes::<_, 128>(&index).map_err(|err| { + io::Error::new( + io::ErrorKind::InvalidData, + format!("Failed serializing index file: {err}"), + ) + })?; fs::write(index_main_path.clone(), index_bytes)?; Ok(Commit { index }) diff --git a/piecrust/src/store/session.rs b/piecrust/src/store/session.rs index bde49f44..9f86dc1e 100644 --- a/piecrust/src/store/session.rs +++ b/piecrust/src/store/session.rs @@ -155,7 +155,6 @@ impl ContractSession { receiver .recv() .expect("The receiver should always receive a reply") - .map(|c| *c.index.root()) } /// Returns path to a file representing a given commit and page.