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: ape_node plugin load time improvement #2378

Merged
merged 2 commits into from
Nov 9, 2024
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
8 changes: 8 additions & 0 deletions docs/userguides/developing_plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ from ape import plugins
# Here, we register our provider plugin so we can use it in 'ape'.
@plugins.register(plugins.ProviderPlugin)
def providers():
# NOTE: By keeping this import local, we avoid slower plugin load times.
from ape_my_plugin.provider import MyProvider

# NOTE: 'MyProvider' defined in a prior code-block.
yield "ethereum", "local", MyProvider
```
Expand All @@ -69,6 +72,11 @@ This decorator hooks into ape core and ties everything together by looking for a
Then, it will loop through these potential `ape` plugins and see which ones have created a plugin type registration.
If the plugin type registration is found, then `ape` knows this package is a plugin and attempts to process it according to its registration interface.

```{warning}
Ensure your plugin's `__init__.py` file imports quickly by keeping all expensive imports in the hook functions locally.
This helps Ape register plugins faster, which is required when checking for API implementations.
```

### CLI Plugins

The `ape` CLI is built using the python package [click](https://palletsprojects.com/p/click/).
Expand Down
20 changes: 17 additions & 3 deletions src/ape_node/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from ape import plugins

from .provider import EthereumNetworkConfig, EthereumNodeConfig, GethDev, Node
from .query import OtterscanQueryEngine


@plugins.register(plugins.Config)
def config_class():
from ape_node.provider import EthereumNodeConfig

return EthereumNodeConfig


@plugins.register(plugins.ProviderPlugin)
def providers():
from ape_node.provider import EthereumNetworkConfig, GethDev, Node

networks_dict = EthereumNetworkConfig().model_dump()
networks_dict.pop("local")
for network_name in networks_dict:
Expand All @@ -21,9 +22,22 @@ def providers():

@plugins.register(plugins.QueryPlugin)
def query_engines():
from ape_node.query import OtterscanQueryEngine

yield OtterscanQueryEngine


def __getattr__(name: str):
if name == "OtterscanQueryEngine":
from ape_node.query import OtterscanQueryEngine

return OtterscanQueryEngine

import ape_node.provider as module

return getattr(module, name)


__all__ = [
"EthereumNetworkConfig",
"EthereumNodeConfig",
Expand Down
Loading