From e17a8383a678a33c19c31c520eed3716118508a6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 6 Nov 2026 06:23:19 +0530 Subject: [PATCH] Disable garbage collection during startup Python garbage collections triggered while importing modules, parsing the config and creating the first window cost ~10ms and find almost nothing to collect. Disable the collector until the main loop starts, then freeze the, mostly long lived, objects created during startup so they are not repeatedly scanned by later collections. --- kitty/entry_points.py | 6 ++++++ kitty/main.py | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/kitty/entry_points.py b/kitty/entry_points.py index f8e75b922..03a7a7a5a 100644 --- a/kitty/entry_points.py +++ b/kitty/entry_points.py @@ -150,6 +150,12 @@ def main() -> None: if first_arg.startswith('+'): namespaced(['+', first_arg[1:]] + sys.argv[2:]) else: + # Garbage collection during startup costs ~10ms and finds almost + # nothing to collect, so disable it until the main loop starts, + # see _run_app() in main.py + import gc + + gc.disable() from kitty.main import main as kitty_main kitty_main() diff --git a/kitty/main.py b/kitty/main.py index 74e0f5336..acc99f7f2 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # License: GPL v3 Copyright: 2016, Kovid Goyal +import gc import json import locale import os @@ -401,6 +402,12 @@ def _run_app(opts: Options, args: CLIOptions, bad_lines: Sequence[BadLine] = (), _('The startup session was invalid'), _('Loading the start session file {0} failed, with error:\n{1}').format(startup_session_error[1], startup_session_error[0]), ) + # Garbage collection is disabled during startup, see main() in + # entry_points.py. Move all objects created during startup, which are + # mostly long lived, out of the purview of the garbage collector, so + # that they are not repeatedly scanned and re-enable it. + gc.freeze() + gc.enable() try: boss.child_monitor.main_loop() finally: