From ed21ef4f95808d3e755d037c4a5052baa7fc579a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 6 Nov 2026 06:23:19 +0530 Subject: [PATCH] Do not import the launch module when there are no watchers Creating the first window imported kitty.launch, which is slow to import, just to find out there are no watchers to load. This delayed the launch of the first child by a few ms. --- kitty/window.py | 12 ++++++++---- kitty_tests/layout.py | 4 +++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/kitty/window.py b/kitty/window.py index cd1eb629b..7e655d32e 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -776,13 +776,17 @@ class GlobalWatchers: raw_spec = get_options().watcher if raw_spec == self.options_spec: return self.ans - from .launch import load_watch_modules - spec = raw_spec.copy() if self.extra: spec[self.extra] = self.extra - ans = load_watch_modules(spec.keys()) - self.ans = Watchers() if ans is None else ans + if spec: + # Importing launch is slow, so do it only when there are watchers + from .launch import load_watch_modules + + ans = load_watch_modules(spec.keys()) + self.ans = Watchers() if ans is None else ans + else: + self.ans = Watchers() self.options_spec = raw_spec.copy() return self.ans diff --git a/kitty_tests/layout.py b/kitty_tests/layout.py index f94959a7f..3eb2cce8a 100644 --- a/kitty_tests/layout.py +++ b/kitty_tests/layout.py @@ -1751,10 +1751,12 @@ class TestWindowHelpers(BaseTest): opts.watcher = {'a.py': 'a.py'} self.assertIs(g(), w1) opts.watcher = {} + num_calls = len(calls) ans = g() self.assertIsNot(ans, w1) self.assertIsInstance(ans, Watchers) - self.ae(calls[-1], ()) + # with no watchers, the slow to import launch module is not used + self.ae(len(calls), num_calls) # changing the extra watcher after first use must take effect g.set_extra('y.py') self.assertIs(g(), w1)