I’ve been working on improving the startup time of Posting recently. Posting is a TUI application built with Textual, and written in Python, for managing and sending HTTP requests.
This post is an overview of some of the techniques (and hacks) I used and the gains made, including:
Using python -X importtime to measure import times. Reducing the scope of import statements, so that they don’t run when they’re not needed. Patching dependencies to prune the import tree. Using pyinstrument and it’s new flamegraph view to find improvements. Extracting expensive-to-import classes into their own modules, isolating other code from them. Lazy loading of Textual widgets. Using C-based extensions where possible. Most of the techniques are Python-specific, and almost all of them boil down to deferring work until it’s actually required.