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.
This commit is contained in:
Kovid Goyal 2026-11-06 06:23:19 +05:30
parent a2af3dadea
commit e17a8383a6
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 13 additions and 0 deletions

View file

@ -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()

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
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: