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

View file

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

View file

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