hn.today

Adopting Python 3.15's lazy imports: a 4× faster CLI, and where they didn't help

namegarage.com4 points0 comments
Screenshot of Adopting Python 3.15's lazy imports: a 4× faster CLI, and where they didn't help

A domain-management package moved to Python 3.15.2 and used explicit lazy imports to reduce startup work across its command-line tool, Textual terminal interface, and MCP server. The biggest opportunity was the CLI: its help command imported heavyweight components for site generation, registrar access, and database operations that help never used. Measurements showed that making all imports lazy could greatly reduce startup, but global lazy mode was rejected because import-time effects - such as loading environment variables from a .env file - can make deferral unsafe. Instead, the team built a fail-closed check around Meta’s Lifeguard analyzer. It validates each lazy statement, tracks approved exceptions and their effects, and runs when relevant files change; reviewers still assess the analyzer’s limits.

Some imports stayed eager because ordinary module initialization immediately uses their names: factory functions in lookup tables, values in function defaults, base classes, or imports inside try blocks. Tests also needed eager imports to preserve established monkey-patching behavior, so the test process applies a filter and separately checks lazy startup in subprocesses. Both test modes passed all 14,342 tests. In nine-run median measurements, CLI help fell from 1,314 to 326 milliseconds, about four times faster, and stopped loading several large packages. TUI help improved from 1,768 to 1,406 milliseconds, while MCP server import time dropped from 3,341 to 2,827 milliseconds and registered the same 94 tools. The smaller gains reflect dependencies that are still required during entry-point setup; further improvement would require restructuring those components, not simply adding more lazy imports.

Read on namegarage.com0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.