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

Investigate contract inefficiencies #3540

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

fselmo
Copy link
Collaborator

@fselmo fselmo commented Nov 27, 2024

What was wrong?

  • When we init contracts, we currently have an overhead of initializing 3 ENS contracts per contract instance. If a user doesn't use the ENS contracts, this ends up being very unnecessary overhead. Instead of instantiating the ENS contracts at every contract init (w3.ens), set all initializing vars to w3._ens which is the reference that w3.ens looks for. This leaves the check for an empty w3._ens followed by subsequent instantiation for when w3.ens is called directly by the user, not for any internal setup.
  • We are needlessly re-creating contract functions both for the Contract init, as well as for the ContractCaller. The caller is instantiated in the Contract init when we set self.caller = ContractCaller(...). Pass along the already built ContractFunctions to the ContractCaller so that it doesn't have to re-build them.

How was it fixed?

  • Use reference to w3._ens rather than w3.ens (which initializes an ENS instance if it is empty).
  • Allow passing contract_functions: ContractFunctions to the ContractCaller class so that we can pass the functions around instead of rebuilding them everywhere they are needed.

Todo:

Cute Animal Picture

0_o

- ``web3.ens`` invokes the ``@property`` getter on ``ens``. This
  getter sets ``self._ens`` on the ``web3`` object if it is empty.
  We do not need to do invoke this getter until ``ens`` is actually
  called. Instead, use the pointer to ``self._ens`` directly to avoid
  overhead when the majority of uses may not even be using ENS.
- When initializing a contract, we create the ``ContractFunction`` objects
  for each function and assign them to the ``contract.functions`` attribute.
  Then we instantiate the ``contract.caller`` and inside the ``ContractCaller``
  init, we re-instantiate each contract function. Pass these around instead
  which will prevent so many calls for validation. They are already built,
  so there is no reason to build them again.
@fselmo fselmo force-pushed the investigate-contract-inefficiencies branch 2 times, most recently from d716d52 to 926a200 Compare November 27, 2024 02:38
@fselmo fselmo force-pushed the investigate-contract-inefficiencies branch from 926a200 to bf62ecf Compare November 27, 2024 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant