Skip to content

Commit

Permalink
refactor: remove some reallocations from decoder (#126)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Trifonov <trifonov.vp@gmail.com>
  • Loading branch information
vladimir-trifonov and lastminutedev authored Mar 26, 2024
1 parent 71cd5b5 commit cab64b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
14 changes: 3 additions & 11 deletions trace_decoder/src/compact/compact_prestate_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,6 @@ impl ParserState {
) -> CompactParsingResult<usize> {
traverser.get_next_n_elems_into_buf(MAX_WITNESS_ENTRIES_NEEDED_TO_MATCH_A_RULE, buf);

// TODO: There is a decent amount of code duplication with the matches and the
// calls to `invalid_witness_err`. We should condense this...

// TODO: These clones are really bad, but we will clean this up once it works.
match buf[0].clone() {
WitnessEntry::Instruction(Instruction::EmptyRoot) => {
Self::traverser_replace_prev_n_nodes_entry_helper(1, traverser, NodeEntry::Empty)
Expand All @@ -418,13 +414,13 @@ impl ParserState {
Self::traverser_replace_prev_n_nodes_entry_helper(
1,
traverser,
NodeEntry::Leaf(k, LeafNodeData::Value(v.clone().into())),
NodeEntry::Leaf(k, LeafNodeData::Value(v.into())),
)
}
WitnessEntry::Instruction(Instruction::Extension(k)) => {
traverser.get_prev_n_elems_into_buf(1, buf);

match buf[0].clone() {
match &buf[0] {
WitnessEntry::Node(node) => Self::traverser_replace_prev_n_nodes_entry_helper(
2,
traverser,
Expand All @@ -434,11 +430,7 @@ impl ParserState {
}
}
WitnessEntry::Instruction(Instruction::Code(c)) => {
Self::traverser_replace_prev_n_nodes_entry_helper(
1,
traverser,
NodeEntry::Code(c.clone()),
)
Self::traverser_replace_prev_n_nodes_entry_helper(1, traverser, NodeEntry::Code(c))
}
WitnessEntry::Instruction(Instruction::AccountLeaf(k, n, b, has_code, has_storage)) => {
let (n_nodes_to_replace, account_node_code, s_trie) = match (has_code, has_storage)
Expand Down
26 changes: 9 additions & 17 deletions trace_decoder/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,26 +511,18 @@ impl ProcessedBlockTrace {
withdrawals: Vec<(Address, U256)>,
dummies_already_added: bool,
) -> TraceParsingResult<()> {
let withdrawals_with_hashed_addrs_iter = withdrawals
.iter()
.map(|(addr, v)| (*addr, hash(addr.as_bytes()), *v));
let withdrawals_with_hashed_addrs_iter = || {
withdrawals
.iter()
.map(|(addr, v)| (*addr, hash(addr.as_bytes()), *v))
};

match dummies_already_added {
// If we have no actual dummy proofs, then we create one and append it to the
// end of the block.
false => {
// TODO: Decide if we want this allocation...
// To avoid double hashing the addrs, but I don't know if the extra `Vec`
// allocation is worth it.
let withdrawals_with_hashed_addrs: Vec<_> =
withdrawals_with_hashed_addrs_iter.collect();

// Dummy state will be the state after the final txn. Also need to include the
// account nodes that were accessed by the withdrawals.
let withdrawal_addrs = withdrawals_with_hashed_addrs
.iter()
.cloned()
.map(|(_, h_addr, _)| h_addr);
let withdrawal_addrs =
withdrawals_with_hashed_addrs_iter().map(|(_, h_addr, _)| h_addr);
let mut withdrawal_dummy = create_dummy_gen_input_with_state_addrs_accessed(
other_data,
extra_data,
Expand All @@ -539,7 +531,7 @@ impl ProcessedBlockTrace {
)?;

Self::update_trie_state_from_withdrawals(
withdrawals_with_hashed_addrs,
withdrawals_with_hashed_addrs_iter(),
&mut final_trie_state.state,
)?;

Expand All @@ -552,7 +544,7 @@ impl ProcessedBlockTrace {
}
true => {
Self::update_trie_state_from_withdrawals(
withdrawals_with_hashed_addrs_iter,
withdrawals_with_hashed_addrs_iter(),
&mut final_trie_state.state,
)?;

Expand Down

0 comments on commit cab64b4

Please sign in to comment.