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: optimize molecule usage #640

Merged
merged 1 commit into from
Apr 12, 2022
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
5 changes: 2 additions & 3 deletions crates/mem-pool/src/sync/mq/gw_kafka.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::Result;
use async_trait::async_trait;
use gw_types::{
packed::{RefreshMemBlockMessage, RefreshMemBlockMessageUnion},
packed::{RefreshMemBlockMessage, RefreshMemBlockMessageReader, RefreshMemBlockMessageUnion},
prelude::{Builder, Entity, Reader},
};
use rdkafka::{
Expand Down Expand Up @@ -134,8 +134,7 @@ impl Consume for Consumer {
);

if let Some(payload) = payload {
let refresh_msg = RefreshMemBlockMessage::from_slice(payload)?;
let reader = refresh_msg.as_reader();
let reader = RefreshMemBlockMessageReader::from_slice(payload)?;
let refresh_msg = reader.to_enum();
match &refresh_msg {
gw_types::packed::RefreshMemBlockMessageUnionReader::NextL2Transaction(
Expand Down
16 changes: 6 additions & 10 deletions crates/store/src/mem_pool_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use gw_db::{
schema::{Col, COLUMNS, COLUMN_ACCOUNT_SMT_BRANCH, COLUMN_ACCOUNT_SMT_LEAF, COLUMN_META},
};
use gw_types::{
packed,
prelude::{Entity, FromSliceShouldBeOk, Pack, Reader, Unpack},
from_box_should_be_ok, packed,
prelude::{Entity, FromSliceShouldBeOk, Pack, Unpack},
};

use crate::{
Expand Down Expand Up @@ -115,11 +115,9 @@ impl MemStore {

pub fn get_mem_block_account_count(&self) -> Result<Option<u32>, Error> {
match self.get(COLUMN_META, META_MEM_SMT_COUNT_KEY) {
Some(slice) => {
let count =
packed::Uint32Reader::from_slice_should_be_ok(slice.as_ref()).to_entity();
Ok(Some(count.unpack()))
}
Some(slice) => Ok(Some(
packed::Uint32Reader::from_slice_should_be_ok(&slice).unpack(),
)),
None => Ok(None),
}
}
Expand All @@ -144,9 +142,7 @@ impl MemStore {

pub fn get_mem_pool_block_info(&self) -> Result<Option<packed::BlockInfo>, Error> {
match self.get(COLUMN_META, META_MEM_BLOCK_INFO) {
Some(slice) => Ok(Some(
packed::BlockInfoReader::from_slice_should_be_ok(slice.as_ref()).to_entity(),
)),
Some(slice) => Ok(Some(from_box_should_be_ok!(packed::BlockInfoReader, slice))),
None => Ok(None),
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/store/src/state/mem_state_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::Result;
use gw_common::{error::Error as StateError, smt::SMT, state::State, H256};
use gw_db::schema::{COLUMN_DATA, COLUMN_SCRIPT, COLUMN_SCRIPT_PREFIX};
use gw_traits::CodeStore;
use gw_types::from_box_should_be_ok;
use gw_types::{
bytes::Bytes,
packed::{self, AccountMerkleState},
Expand Down Expand Up @@ -109,7 +110,7 @@ impl<'a> CodeStore for MemStateTree<'a> {
self.db()
.get(COLUMN_SCRIPT, script_hash.as_slice())
.or_else(|| self.db().get(COLUMN_SCRIPT, script_hash.as_slice()))
.map(|slice| packed::ScriptReader::from_slice_should_be_ok(slice.as_ref()).to_entity())
.map(|slice| from_box_should_be_ok!(packed::ScriptReader, slice))
}

fn get_script_hash_by_short_script_hash(&self, script_hash_prefix: &[u8]) -> Option<H256> {
Expand Down
3 changes: 2 additions & 1 deletion crates/store/src/state/state_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use gw_db::schema::{COLUMN_DATA, COLUMN_SCRIPT, COLUMN_SCRIPT_PREFIX};
use gw_traits::CodeStore;
use gw_types::{
bytes::Bytes,
from_box_should_be_ok,
packed::{self, AccountMerkleState, Byte32},
prelude::*,
};
Expand Down Expand Up @@ -236,7 +237,7 @@ impl<'a> CodeStore for StateTree<'a> {
fn get_script(&self, script_hash: &H256) -> Option<packed::Script> {
self.db()
.get(COLUMN_SCRIPT, script_hash.as_slice())
.map(|slice| packed::ScriptReader::from_slice_should_be_ok(slice.as_ref()).to_entity())
.map(|slice| from_box_should_be_ok!(packed::ScriptReader, slice))
}

fn get_script_hash_by_short_script_hash(&self, script_hash_prefix: &[u8]) -> Option<H256> {
Expand Down
70 changes: 21 additions & 49 deletions crates/store/src/traits/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use gw_db::schema::{
use gw_types::offchain::global_state_from_slice;
use gw_types::packed::{Script, WithdrawalKey};
use gw_types::{
from_box_should_be_ok,
packed::{self, ChallengeTarget, TransactionKey},
prelude::*,
};
Expand Down Expand Up @@ -70,19 +71,15 @@ pub trait ChainStore: KVStoreRead {
.get(COLUMN_META, META_LAST_VALID_TIP_BLOCK_HASH_KEY)
.expect("get last valid tip block hash");

let byte32 = packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref()).to_entity();
let byte32 = packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref());
Ok(byte32.unpack())
}

fn get_tip_block_hash(&self) -> Result<H256, Error> {
let slice = self
.get(COLUMN_META, META_TIP_BLOCK_HASH_KEY)
.expect("get tip block hash");
Ok(
packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref())
.to_entity()
.unpack(),
)
Ok(packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref()).unpack())
}

fn get_tip_block(&self) -> Result<packed::L2Block, Error> {
Expand All @@ -94,9 +91,7 @@ pub trait ChainStore: KVStoreRead {
let block_number: packed::Uint64 = number.pack();
match self.get(COLUMN_INDEX, block_number.as_slice()) {
Some(slice) => Ok(Some(
packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref())
.to_entity()
.unpack(),
packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref()).unpack(),
)),
None => Ok(None),
}
Expand All @@ -105,19 +100,15 @@ pub trait ChainStore: KVStoreRead {
fn get_block_number(&self, block_hash: &H256) -> Result<Option<u64>, Error> {
match self.get(COLUMN_INDEX, block_hash.as_slice()) {
Some(slice) => Ok(Some(
packed::Uint64Reader::from_slice_should_be_ok(slice.as_ref())
.to_entity()
.unpack(),
packed::Uint64Reader::from_slice_should_be_ok(slice.as_ref()).unpack(),
)),
None => Ok(None),
}
}

fn get_block(&self, block_hash: &H256) -> Result<Option<packed::L2Block>, Error> {
match self.get(COLUMN_BLOCK, block_hash.as_slice()) {
Some(slice) => Ok(Some(
packed::L2BlockReader::from_slice_should_be_ok(slice.as_ref()).to_entity(),
)),
Some(slice) => Ok(Some(from_box_should_be_ok!(packed::L2BlockReader, slice))),
None => Ok(None),
}
}
Expand All @@ -135,9 +126,7 @@ pub trait ChainStore: KVStoreRead {
) -> Result<Option<packed::TransactionInfo>, Error> {
let tx_info_opt = self
.get(COLUMN_TRANSACTION_INFO, tx_hash.as_slice())
.map(|slice| {
packed::TransactionInfoReader::from_slice_should_be_ok(slice.as_ref()).to_entity()
});
.map(|slice| from_box_should_be_ok!(packed::TransactionInfoReader, slice));
Ok(tx_info_opt)
}

Expand All @@ -147,15 +136,12 @@ pub trait ChainStore: KVStoreRead {
) -> Result<Option<packed::L2Transaction>, Error> {
Ok(self
.get(COLUMN_TRANSACTION, tx_key.as_slice())
.map(|slice| {
packed::L2TransactionReader::from_slice_should_be_ok(slice.as_ref()).to_entity()
}))
.map(|slice| from_box_should_be_ok!(packed::L2TransactionReader, slice)))
}

fn get_transaction_receipt(&self, tx_hash: &H256) -> Result<Option<packed::TxReceipt>, Error> {
if let Some(slice) = self.get(COLUMN_TRANSACTION_INFO, tx_hash.as_slice()) {
let info =
packed::TransactionInfoReader::from_slice_should_be_ok(slice.as_ref()).to_entity();
let info = from_box_should_be_ok!(packed::TransactionInfoReader, slice);
let tx_key = info.key();
self.get_transaction_receipt_by_key(&tx_key)
} else {
Expand All @@ -169,9 +155,7 @@ pub trait ChainStore: KVStoreRead {
) -> Result<Option<packed::TxReceipt>, Error> {
Ok(self
.get(COLUMN_TRANSACTION_RECEIPT, key.as_slice())
.map(|slice| {
packed::TxReceiptReader::from_slice_should_be_ok(slice.as_ref()).to_entity()
}))
.map(|slice| from_box_should_be_ok!(packed::TxReceiptReader, slice)))
}

fn get_withdrawal(
Expand All @@ -190,9 +174,7 @@ pub trait ChainStore: KVStoreRead {
) -> Result<Option<packed::WithdrawalInfo>, Error> {
let withdrawal_info_opt = self
.get(COLUMN_WITHDRAWAL_INFO, withdrawal_hash.as_slice())
.map(|slice| {
packed::WithdrawalInfoReader::from_slice_should_be_ok(slice.as_ref()).to_entity()
});
.map(|slice| from_box_should_be_ok!(packed::WithdrawalInfoReader, slice));
Ok(withdrawal_info_opt)
}

Expand All @@ -202,21 +184,18 @@ pub trait ChainStore: KVStoreRead {
) -> Result<Option<packed::WithdrawalRequestExtra>, Error> {
Ok(self
.get(COLUMN_WITHDRAWAL, withdrawal_key.as_slice())
.map(|slice| {
packed::WithdrawalRequestExtraReader::from_slice_should_be_ok(slice.as_ref())
.to_entity()
}))
.map(|slice| from_box_should_be_ok!(packed::WithdrawalRequestExtraReader, slice)))
}

fn get_l2block_committed_info(
&self,
block_hash: &H256,
) -> Result<Option<packed::L2BlockCommittedInfo>, Error> {
match self.get(COLUMN_L2BLOCK_COMMITTED_INFO, block_hash.as_slice()) {
Some(slice) => Ok(Some(
packed::L2BlockCommittedInfoReader::from_slice_should_be_ok(slice.as_ref())
.to_entity(),
)),
Some(slice) => Ok(Some(from_box_should_be_ok!(
packed::L2BlockCommittedInfoReader,
slice
))),
None => Ok(None),
}
}
Expand All @@ -227,8 +206,7 @@ pub trait ChainStore: KVStoreRead {
) -> Result<Option<Vec<packed::DepositRequest>>, Error> {
match self.get(COLUMN_BLOCK_DEPOSIT_REQUESTS, block_hash.as_slice()) {
Some(slice) => Ok(Some(
packed::DepositRequestVecReader::from_slice_should_be_ok(slice.as_ref())
.to_entity()
from_box_should_be_ok!(packed::DepositRequestVecReader, slice)
.into_iter()
.collect(),
)),
Expand Down Expand Up @@ -271,17 +249,15 @@ pub trait ChainStore: KVStoreRead {
) {
Some(slice) => {
let block_hash = packed::Byte32VecReader::from_slice_should_be_ok(slice.as_ref());
Ok(Some(block_hash.to_entity().unpack()))
Ok(Some(block_hash.unpack()))
}
None => Ok(None),
}
}

fn get_asset_script(&self, script_hash: &H256) -> Result<Option<Script>, Error> {
match self.get(COLUMN_ASSET_SCRIPT, script_hash.as_slice()) {
Some(slice) => Ok(Some(
packed::ScriptReader::from_slice_should_be_ok(slice.as_ref()).to_entity(),
)),
Some(slice) => Ok(Some(from_box_should_be_ok!(packed::ScriptReader, slice))),
None => Ok(None),
}
}
Expand All @@ -292,9 +268,7 @@ pub trait ChainStore: KVStoreRead {
) -> Result<Option<packed::L2Transaction>, Error> {
Ok(self
.get(COLUMN_MEM_POOL_TRANSACTION, tx_hash.as_slice())
.map(|slice| {
packed::L2TransactionReader::from_slice_should_be_ok(slice.as_ref()).to_entity()
}))
.map(|slice| from_box_should_be_ok!(packed::L2TransactionReader, slice)))
}

fn get_mem_pool_transaction_receipt(
Expand All @@ -303,9 +277,7 @@ pub trait ChainStore: KVStoreRead {
) -> Result<Option<packed::TxReceipt>, Error> {
Ok(self
.get(COLUMN_MEM_POOL_TRANSACTION_RECEIPT, tx_hash.as_slice())
.map(|slice| {
packed::TxReceiptReader::from_slice_should_be_ok(slice.as_ref()).to_entity()
}))
.map(|slice| from_box_should_be_ok!(packed::TxReceiptReader, slice)))
}

fn get_mem_pool_withdrawal(
Expand Down
5 changes: 2 additions & 3 deletions crates/store/src/transaction/store_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,9 @@ impl StoreTransaction {
// TODO: prune db state
pub fn get_reverted_block_hashes(&self) -> Result<HashSet<H256>, Error> {
let iter = self.get_iter(COLUMN_REVERTED_BLOCK_SMT_LEAF, IteratorMode::End);
let to_byte32 = iter.map(|(key, _value)| {
packed::Byte32Reader::from_slice_should_be_ok(key.as_ref()).to_entity()
let to_h256 = iter.map(|(key, _value)| {
packed::Byte32Reader::from_slice_should_be_ok(key.as_ref()).unpack()
});
let to_h256 = to_byte32.map(|byte32| byte32.unpack());

Ok(to_h256.collect())
}
Expand Down
8 changes: 8 additions & 0 deletions crates/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ cfg_if::cfg_if! {
use alloc::string;
}
}

#[macro_export]
macro_rules! from_box_should_be_ok {
($r:ty, $b:ident) => {{
<$r>::from_slice_should_be_ok(&$b);
<$r as gw_types::prelude::Reader>::Entity::new_unchecked($b.into())
}};
}