Skip to content

Commit

Permalink
perf: ape_node plugin load time improvement (#2378)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 9, 2024
1 parent 0c9e5f1 commit e31c426
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit e31c426

Please sign in to comment.