kitty icon indicating copy to clipboard operation
kitty copied to clipboard

[RFC] Reducing startup latency for kittens/remote control commands

Open kovidgoyal opened this issue 1 year ago • 0 comments

Hi all,

I have implemented in master a set of features that reduce startup latency when running kittens and remote control commands by 30-50 milliseconds. They work basically by avoiding spinning up fresh copies of python interpreters. Instead forks of a "prewarmed" zygote process are used.

Sadly because of the complexities of how UNIX job handling/terminal devices work this was rather a lot of fairly low-level code. So I would appreciate some testing/comments on the functionality, before I release it.

To test simply install a recent kitty nightly (see https://sw.kovidgoyal.net/kitty/binary/#customizing-the-installation). You should notice a significant reduction in the kitten and remote control startup time. For example, on my system:

time icat --bad-option                         
Unknown option: --bad-option
real	0.029

time KITTY_PREWARM_SOCKET= icat --bad-option   
Unknown option: --bad-option
real	0.063

We see the time without prewarming is 63ms with prewarming its 29ms. To me personally, even more significant than the time reduction, is all the CPU cycles/energy we dont consume by avoiding repeated python initialization.

I should note that there are a couple of downsides:

  1. There are two new env vars kitty injects into every child process KITTY_PREWARM_SOCKET and KITTY_PREWARM_SOCKET_REAL_TTY
  2. Prewarming works by running the actual code of the kitten in a forked worker with its own PTY and transferring data back and forth between this PTY and the real PTY device, which has some overhead and more importantly could break is some edge cases.

Original motivation for this feature and discussions about it are in #5159

kovidgoyal avatar Aug 06 '22 02:08 kovidgoyal