Skip to content

Commit

Permalink
feat: optimize controller attr lookups (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Dec 21, 2024
1 parent 5cca30c commit f01e609
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dank_mids/_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ def _post_future_cleanup(self) -> None:
try:
with controller.pools_closed_lock:
# This will have already taken place in a full json batch of multicalls
controller.pending_eth_calls.pop(self.block)
controller._pending_eth_calls_pop(self.block)
except KeyError:
pass

Expand Down
15 changes: 10 additions & 5 deletions dank_mids/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ def __init__(self, w3: Web3) -> None:
)
"""A dictionary of pending Multicalls by block. The Multicalls hold all pending eth_calls."""

self._pending_eth_calls_pop = self.pending_eth_calls.pop
self._pending_eth_calls_copy = self.pending_eth_calls.copy
self._pending_eth_calls_clear = self.pending_eth_calls.clear
self._pending_eth_calls_values = self.pending_eth_calls.values

self.pending_rpc_calls = JSONRPCBatch(self)
"""A JSONRPCBatch containing all pending rpc requests."""

Expand Down Expand Up @@ -270,8 +275,8 @@ async def execute_batch(self) -> None:
and executes them as a single batch.
"""
with self.pools_closed_lock: # Do we really need this? # NOTE: yes we do
multicalls = self.pending_eth_calls.copy()
self.pending_eth_calls.clear()
multicalls = self._pending_eth_calls_copy()
self._pending_eth_calls_clear()
rpc_calls = self.pending_rpc_calls
self.pending_rpc_calls = JSONRPCBatch(self)
demo_logger.info(f"executing dank batch (current cid: {self.call_uid.latest})") # type: ignore
Expand All @@ -290,7 +295,7 @@ def queue_is_full(self) -> bool:
with self.pools_closed_lock:
if ENVS.OPERATION_MODE.infura: # type: ignore [attr-defined]
return sum(len(call) for call in self.pending_rpc_calls) >= ENVS.MAX_JSONRPC_BATCH_SIZE # type: ignore [attr-defined,operator]
eth_calls = sum(len(calls) for calls in self.pending_eth_calls.values())
eth_calls = sum(len(calls) for calls in self._pending_eth_calls_values())
other_calls = sum(len(call) for call in self.pending_rpc_calls)
return eth_calls + other_calls >= self.batcher.step

Expand All @@ -304,8 +309,8 @@ def early_start(self):
are queued to form a full batch.
"""
with self.pools_closed_lock:
self.pending_rpc_calls.extend(self.pending_eth_calls.values(), skip_check=True)
self.pending_eth_calls.clear()
self.pending_rpc_calls.extend(self._pending_eth_calls_values(), skip_check=True)
self._pending_eth_calls_clear()
self.pending_rpc_calls.start()

def reduce_multicall_size(self, num_calls: int) -> None:
Expand Down

0 comments on commit f01e609

Please sign in to comment.