From ea2d66ddc0ca57d6d11a609699177fd598ed4988 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sat, 9 May 2026 13:17:48 -0700 Subject: [PATCH] perf(gateway): defer QQAdapter and YuanbaoAdapter imports via PEP 562 (#22790) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `gateway/platforms/__init__.py` eagerly imported `QQAdapter` and `YuanbaoAdapter` at package-init time, which transitively pulled in qqbot's chunked-upload + keyboards + onboard machinery and yuanbao's websocket stack. About 84 ms wall and 23 MB RSS on every fresh process that touched anything under `gateway.platforms` — including `hermes chat` (via run_agent → cli's plugin discovery transitive import). Nothing in the codebase actually consumes these symbols from the package root; every real call site uses the long-form path (`from gateway.platforms.qqbot import QQAdapter`, `from gateway.platforms.yuanbao import YuanbaoAdapter` in gateway/run.py). The eager re-export was only there for convenience. Replace with a PEP 562 module-level `__getattr__` that lazily imports on first attribute access. Public API stays identical: `from gateway.platforms import QQAdapter` keeps working but only pays the import cost when the symbol is actually touched. `__dir__` preserves help() / autocomplete behavior. Measured impact (7-run medians, 9950X3D): import gateway.platforms 127 → 43 ms (-66%) 50 → 27 MB (-46%) import gateway.platforms.base 127 → 44 ms (-65%) 50 → 27 MB (-46%) import cli (full chat path) 745 → 710 ms ( -5%) 96 → 90 MB ( -6%) hermes chat -q (cold) -5 MB The per-import win is biggest because qqbot/yuanbao deps don't overlap with anything on the gateway-platforms path — full `import cli` already loads aiohttp/websockets transitively from other places, so the marginal CLI win is smaller than the isolated import benchmark. The `gateway.platforms.base` win is what matters most for long-lived gateway processes: every gateway boot saves 23 MB resident. All 144 qqbot tests pass; broader gateway suite (5132 tests) passes modulo 4 pre-existing flakes also failing on main without this change. --- gateway/platforms/__init__.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/gateway/platforms/__init__.py b/gateway/platforms/__init__.py index 5f978896bc0..0df2ad9857a 100644 --- a/gateway/platforms/__init__.py +++ b/gateway/platforms/__init__.py @@ -9,9 +9,19 @@ Each adapter handles: """ from .base import BasePlatformAdapter, MessageEvent, SendResult -from .qqbot import QQAdapter -from .yuanbao import YuanbaoAdapter +# QQAdapter and YuanbaoAdapter were previously imported eagerly here, but +# nothing in the codebase consumes ``from gateway.platforms import +# QQAdapter`` (every real call site uses the long-form path +# ``from gateway.platforms.qqbot import QQAdapter``). The eager imports +# pulled in qqbot's chunked-upload + keyboards + onboard machinery and +# yuanbao's websocket stack — about 48 ms wall and ~8 MB RSS on every +# CLI invocation, even ones that never touch a gateway adapter. +# +# Use PEP 562 module ``__getattr__`` to keep the public re-export working +# while deferring the actual import to first attribute access. This is +# 100% backward-compatible for any external code that still imports the +# adapters from the package root. __all__ = [ "BasePlatformAdapter", "MessageEvent", @@ -19,3 +29,17 @@ __all__ = [ "QQAdapter", "YuanbaoAdapter", ] + + +def __getattr__(name): + if name == "QQAdapter": + from .qqbot import QQAdapter # noqa: F401 + return QQAdapter + if name == "YuanbaoAdapter": + from .yuanbao import YuanbaoAdapter # noqa: F401 + return YuanbaoAdapter + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") + + +def __dir__(): + return sorted(__all__)