Skip to content

Commit

Permalink
piecrust: optimized cloning in session
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Oct 22, 2024
1 parent b234cba commit 86b63c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
33 changes: 16 additions & 17 deletions piecrust/src/store/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl CommitHulk {

pub fn to_commit(&self) -> Commit {
let index = self.index.map(|p| unsafe { p.as_ref().unwrap() });
let mut commit = match index {
match index {
Some(p) => Commit {
index: p.clone(),
contracts_merkle: self.contracts_merkle.clone(),
Expand All @@ -54,13 +54,12 @@ impl CommitHulk {
contracts_merkle: self.contracts_merkle.clone(),
maybe_hash: self.maybe_hash,
},
};
for (contract_id, element) in self.index2.contracts().iter() {
commit
.index
.insert_contract_index(&contract_id, element.clone())
}
commit
// for (contract_id, element) in self.index2.contracts().iter() {
// commit
// .index
// .insert_contract_index(&contract_id, element.clone())
// }
}

pub fn fast_clone<'a>(
Expand Down Expand Up @@ -123,8 +122,8 @@ impl CommitHulk {
},
);
}
let (index2, contracts_merkle) = self.get_mutables();
let element = index2.get_mut(&contract_id, None).unwrap();
let (index, contracts_merkle) = self.get_mutables();
let element = index.get_mut(&contract_id, None).unwrap();

element.len = memory.current_len;

Expand Down Expand Up @@ -161,14 +160,14 @@ impl CommitHulk {
}

/*
==========================
index accessors
*/

pub fn remove_contract_index(
&mut self,
contract_id: &ContractId,
) -> Option<ContractIndexElement> {
self.index2.contracts_mut().remove(&contract_id) // todo: may not work
self.index2.contracts_mut().remove(contract_id)
}

pub fn insert_contract_index(
Expand All @@ -187,9 +186,9 @@ impl CommitHulk {
match index {
Some(p) => self
.index2
.get(&contract_id, self.maybe_hash)
.or_else(move || p.get(&contract_id, self.maybe_hash)),
None => self.index2.get(&contract_id, self.maybe_hash),
.get(contract_id, self.maybe_hash)
.or_else(move || p.get(contract_id, self.maybe_hash)),
None => self.index2.get(contract_id, self.maybe_hash),
}
}

Expand All @@ -206,10 +205,10 @@ impl CommitHulk {
let index = self.index.map(|p| unsafe { p.as_ref().unwrap() });
match index {
Some(p) => {
self.index2.contains_key(&contract_id)
|| p.contains_key(&contract_id)
self.index2.contains_key(contract_id)
|| p.contains_key(contract_id)
}
None => self.index2.contains_key(&contract_id),
None => self.index2.contains_key(contract_id),
}
}

Expand Down
9 changes: 1 addition & 8 deletions piecrust/src/store/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,9 @@ impl ContractSession {
let (replier, receiver) = mpsc::sync_channel(1);

let mut contracts = BTreeMap::new();
let base = self.base.as_ref().map(|c| //Commit {
// index: c.index.clone(),
// contracts_merkle: c.contracts_merkle.clone(),
// maybe_hash: c.maybe_hash,
c.to_commit() /* } */);

let mut base_clone = base.as_ref().map(|b| b.to_hulk()); // todo: wasteful, think of a better way
let base = self.base.as_ref().map(|c| c.to_commit());

mem::swap(&mut self.contracts, &mut contracts);
mem::swap(&mut self.base, &mut base_clone);

self.call
.send(Call::Commit {
Expand Down

0 comments on commit 86b63c7

Please sign in to comment.