Skip to content

Commit

Permalink
feat: cache method len (#281)
Browse files Browse the repository at this point in the history
* feat: cache method len

this was frequently recomputed and is now cached

* chore: `black .`

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
BobTheBuidler and github-actions[bot] authored Nov 16, 2024
1 parent 41bd939 commit b32b6a7
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions dank_mids/_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ async def _debug_daemon(self) -> None:
"""


@lru_cache(maxsize=None)
def _get_len_for_method(method: str) -> int:
# NOTE: These are totally arbitrary, used to reduce frequency of giant batches/responses
if self.method == "eth_getTransactionReceipt":
return 10
elif any(m in self.method for m in ["eth_getCode" "eth_getBlockBy", "eth_getTransaction"]):
return 6
return 1


@lru_cache(maxsize=None)
def _should_batch_method(method: str) -> bool:
return all(bypass not in method for bypass in BYPASS_METHODS)
Expand Down Expand Up @@ -195,16 +205,8 @@ def __eq__(self, __o: object) -> bool:
return self.uid == __o.uid if isinstance(__o, self.__class__) else False

def __len__(self) -> int:
# we dont need to consider this for v small batch sizes
if ENVS.MAX_JSONRPC_BATCH_SIZE > 50: # type: ignore [operator]
# NOTE: These are totally arbitrary
if self.method == "eth_getTransactionReceipt":
return 10
elif any(
m in self.method for m in ["eth_getCode" "eth_getBlockBy", "eth_getTransaction"]
):
return 6
return 1
# NOTE: We dont need to consider this for very small batch sizes since the requests/responses will never get too large
return _get_len_for_method(self.method) if ENVS.MAX_JSONRPC_BATCH_SIZE > 50 else 1 # type: ignore [operator]

def __repr__(self) -> str:
return (
Expand Down

0 comments on commit b32b6a7

Please sign in to comment.