Repository navigation
perf(cli): load mtcute only when a command calls Telegram - #226
Merged
Merged
Conversation
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]>
This was referenced Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
Every
tgstart 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,--helpand 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/telegramkeeps only types at the top.getDefaultConfig,requireDefaultConfig,createDefaultClientFactory,createClientandgetClientForAccountare async. They import mtcute when they build a client or parseTELEGRAM_PROXY. A client is cached as its promise, so two first calls for one account share it.tg send, the peer resolver and the media download, the TL helpers oftg api, the upload oftg send --fileand the download stream. They all run after a call to Telegram.tg daemon startimports the daemon runtime.tg daemon status,tg statusandmessages waitimport only the PID file module.isPermanentSyncErrormoves fromsync-retry.ts, whichtg statusreads for the retry limit, tosync-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.
tg --version(source)tg chats list(source)tg status(source)dist/tg --version(compiled)src/cli.test.tsfails when the command tree loads mtcute. I checked this by putting back a statictlimport in the rate limits.AGENTS.mdstates the rule. Open PRs that addimport { tl } from "@mtcute/bun"in a command module will needimport typeplusawait 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.tstests that can't fail as root (unchanged).bun run build: the compileddist/tgruns--version. With an invalidTELEGRAM_PROXY,tg daemon startstill logsConfiguration 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: …) unlessAGENTS.mdsays otherwise🤖 Generated with Claude Code