Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Perf: Don't create intermediate tuples in normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed Apr 26, 2019
1 parent c221409 commit 0838ffb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
7 changes: 5 additions & 2 deletions trinity/protocol/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
ReceiptsBundles = Tuple[Tuple[Tuple[Receipt, ...], Tuple[Hash32, Dict[Hash32, bytes]]], ...]

# (BlockBody, (txn_root, txn_trie_data), uncles_hash)
BlockBodyBundles = Tuple[Tuple[

BlockBodyBundle = Tuple[
BlockBody,
Tuple[Hash32, Dict[Hash32, bytes]],
Hash32,
], ...]
]

BlockBodyBundles = Tuple[BlockBodyBundle, ...]
28 changes: 12 additions & 16 deletions trinity/protocol/eth/normalizers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import (
Iterable,
Tuple,
)

from eth_utils.toolz import (
compose,
from eth_utils import (
to_tuple,
)
from eth.db.trie import make_trie_root_and_nodes
from eth_hash.auto import keccak
Expand All @@ -13,6 +14,7 @@
BaseNormalizer,
)
from trinity.protocol.common.types import (
BlockBodyBundle,
BlockBodyBundles,
NodeDataBundles,
ReceiptsBundles,
Expand All @@ -26,7 +28,7 @@ class GetNodeDataNormalizer(BaseNormalizer[Tuple[bytes, ...], NodeDataBundles]):

@staticmethod
def normalize_result(msg: Tuple[bytes, ...]) -> NodeDataBundles:
node_keys = tuple(map(keccak, msg))
node_keys = map(keccak, msg)
result = tuple(zip(node_keys, msg))
return result

Expand All @@ -36,23 +38,17 @@ class ReceiptsNormalizer(BaseNormalizer[ReceiptsByBlock, ReceiptsBundles]):

@staticmethod
def normalize_result(message: ReceiptsByBlock) -> ReceiptsBundles:
trie_roots_and_data = tuple(map(make_trie_root_and_nodes, message))
trie_roots_and_data = map(make_trie_root_and_nodes, message)
return tuple(zip(message, trie_roots_and_data))


class GetBlockBodiesNormalizer(BaseNormalizer[Tuple[BlockBody, ...], BlockBodyBundles]):
is_normalization_slow = True

@staticmethod
def normalize_result(msg: Tuple[BlockBody, ...]) -> BlockBodyBundles:
uncles_hashes = tuple(map(
compose(keccak, rlp.encode),
tuple(body.uncles for body in msg)
))
transaction_roots_and_trie_data = tuple(map(
make_trie_root_and_nodes,
tuple(body.transactions for body in msg)
))

body_bundles = tuple(zip(msg, transaction_roots_and_trie_data, uncles_hashes))
return body_bundles
@to_tuple
def normalize_result(msg: Tuple[BlockBody, ...]) -> Iterable[BlockBodyBundle]:
for body in msg:
uncle_hashes = keccak(rlp.encode(body.uncles))
transaction_root_and_nodes = make_trie_root_and_nodes(body.transactions)
yield body, transaction_root_and_nodes, uncle_hashes

0 comments on commit 0838ffb

Please sign in to comment.