Skip to content

perf(cli): load mtcute only when a command calls Telegram - #226

Merged
neoromantic merged 1 commit into
mainfrom
perf/lazy-mtcute
Nov 19, 2026
Merged

neoromantic merged 1 commit into
mainfrom
perf/lazy-mtcute

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

What and why

Every tg start imported @mtcute/bun, 644 modules, because the command tree is built eagerly. That was 0.32 s of the 0.47 s of CPU a start took, even for --version, --help and commands that answer from the cache. The e2e suite starts the CLI about a thousand times, so that cost also made up most of its CI time.

Now the command tree loads no mtcute module (129 modules instead of 913):

  • services/telegram keeps only types at the top. getDefaultConfig, requireDefaultConfig, createDefaultClientFactory, createClient and getClientForAccount are async. They import mtcute when they build a client or parse TELEGRAM_PROXY. A client is cached as its promise, so two first calls for one account share it.
  • Calls into Telegram import mtcute where they run. That covers the RPC error checks of the rate limits, tg send, the peer resolver and the media download, the TL helpers of tg api, the upload of tg send --file and the download stream. They all run after a call to Telegram.
  • tg daemon start imports the daemon runtime. tg daemon status, tg status and messages wait import only the PID file module.
  • isPermanentSyncError moves from sync-retry.ts, which tg status reads for the retry limit, to sync-worker-utils.ts, where the worker classifies job failures. Its test moves with it.

Factories that tests pass in (auth, media download, the lazy history client) may still return a client directly. The protected test files of open PRs don't change.

CPU per start, best of 5 before after
tg --version (source) 0.49 s 0.08 s
tg chats list (source) 0.50 s 0.10 s
tg status (source) 0.50 s 0.11 s
dist/tg --version (compiled) 0.24 s 0.18 s

src/cli.test.ts fails when the command tree loads mtcute. I checked this by putting back a static tl import in the rate limits. AGENTS.md states the rule. Open PRs that add import { tl } from "@mtcute/bun" in a command module will need import type plus await import() after a rebase, and the new test says so.

How I checked it

  • bun run check.

  • Both suites in 4 workers: every test passes except the 3 skill.test.ts tests that can't fail as root (unchanged).

  • bun run build: the compiled dist/tg runs --version. With an invalid TELEGRAM_PROXY, tg daemon start still logs Configuration error: ConfigError: Invalid TELEGRAM_PROXY URL. That path loads mtcute through the dynamic import inside the binary.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

🤖 Generated with Claude Code

Every tg start imported @mtcute/bun, 644 modules, through the eagerly
built command tree: 0.32 s of the 0.47 s of CPU a start took, even for
--version and for commands that answer from the cache. Now the command
tree loads none of it (129 modules instead of 913):

- services/telegram: types only at the top. getDefaultConfig,
  requireDefaultConfig, createDefaultClientFactory, createClient and
  getClientForAccount are async and import mtcute when they build a
  client or parse TELEGRAM_PROXY. A client is cached as its promise, so
  two first calls for one account get the same client.
- The RPC error checks of the rate limits, tg send, the peer resolver
  and the media download, the TL helpers of tg api, the upload of tg send
  --file and the download stream import mtcute where they run, after a
  call to Telegram.
- tg daemon start imports the daemon runtime; tg daemon status, tg
  status and messages wait take the PID file module alone.
- isPermanentSyncError moves from sync-retry.ts, which tg status reads
  for the retry limit, to sync-worker-utils.ts, where the worker
  classifies job failures; its test moves with it.

Factories that tests pass in (auth, media download, the lazy history
client) may still return a client directly.

CPU per start, source install, best of 5: --version 0.49 -> 0.08 s,
chats list 0.50 -> 0.10 s, status 0.50 -> 0.11 s. The compiled binary,
already one bundle, goes 0.24 -> 0.18 s. src/cli.test.ts fails when the
command tree loads mtcute (checked by putting back a static tl import in
the rate limits), and AGENTS.md states the rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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