July 30th, 2024

Troubleshooting: Terminal Lag

Tavis Ormandy addresses slow xterm performance on Windows, identifying a window fading effect as a key issue. He implements a caching solution, reducing startup time to approximately 366ms, comparable to Fedora.

Read original articleLink Icon
CuriosityAppreciationFrustration
Troubleshooting: Terminal Lag

Tavis Ormandy discusses troubleshooting slow xterm performance on Windows compared to Fedora Linux. He notes that while xterm opens in about 300ms on Fedora, it takes approximately 1600ms on Windows, primarily due to a window fading effect that adds around 200ms to the startup time. Ormandy identifies that the X410 server is responsible for this animation, which he cannot disable. By using a debugger to modify the behavior of the window attributes, he reduces the delay significantly.

Further analysis reveals that filesystem performance under WSL is not the issue, as it performs slightly faster on Windows. He investigates various components, including the X server and font loading times, concluding that the toolbar feature in xterm, enabled on Windows but disabled on Fedora, contributes to the delay. After experimenting with various xterm settings, he finds a combination that improves startup time by about 200ms.

Ultimately, Ormandy implements a caching solution using deferred mapping to keep xterm processes running in the background, which allows for near-instant terminal access. This adjustment reduces the startup time to about 366ms, making it comparable to Fedora's performance. He expresses satisfaction with the solution, although he acknowledges that it may not be widely applicable to others.

Related

X debut 40 years ago (1984)

X debut 40 years ago (1984)

Robert W. Scheifler introduced the X window system in June 1984 for the VS100 Unix server, offering improved performance over W. The system was stable, with the Laboratory for Computer Science already transitioning to X and developing applications. Scheifler encouraged experimentation and welcomed volunteers for documentation contributions.

Four lines of code it was four lines of code

Four lines of code it was four lines of code

The programmer resolved a CPU utilization issue by removing unnecessary Unix domain socket code from a TCP and TLS service handler. This debugging process emphasized meticulous code review and system interaction understanding.

X Window System at 40

X Window System at 40

The X Window System, released in 1984 by Bob Scheifler, celebrated its 40th anniversary. It evolved into X11 in 1987, gaining popularity for its open-source nature and compatibility with existing applications. Reflecting on its impact, commentators highlight its resilience and continued relevance.

X Window System at 40

X Window System at 40

The X Window System, originating in 1984, celebrated its 40th anniversary. Key points include its evolution, open-source nature, and debates on successors like Wayland. Its lasting impact on computing environments is acknowledged.

Iconography of the X Window System: The Boot Stipple

Iconography of the X Window System: The Boot Stipple

The article explores the iconic stipple pattern in the X Window System's boot-up screen, symbolizing system evolution and complexity. It discusses its historical significance, disappearance from modern Linux distributions, and efforts for reintroduction.

AI: What people are saying
The comments reflect a mix of technical insights and personal experiences related to xterm performance and UI latency.
  • Several users express curiosity and appreciation for learning new debugging techniques and tools, such as the Microsoft Console Debugger and `hyperfine`.
  • There is a consensus that while the reduced startup time of 366ms is an improvement, it still feels slow to some users, lacking a "snappy" experience.
  • Some commenters share their own performance measurements and methodologies, highlighting variations in terminal startup times across different setups.
  • Discussion includes the broader implications of UI latency across various devices, emphasizing a desire for more responsive applications.
  • Users reflect on the value of optimizing performance, considering the cumulative time savings over years of usage.
Link Icon 13 comments
By @vijucat - 7 months
Love such articles where I learn something new. cdb is completely new to me. It's apparently the Microsoft Console Debugger. For others like me who were wondering how `eb win32u!NtUserSetLayeredWindowAttributes c3` neutered the window animation:

"By executing this command, you are effectively replacing the first byte of the `NtUserSetLayeredWindowAttributes` function with a `ret` instruction. This means that any call to `NtUserSetLayeredWindowAttributes` will immediately return without executing any of its original code. This can be used to bypass or disable the functionality of this function"

(Thanks to GitHub Copilot for that)

Also see https://learn.microsoft.com/en-us/windows-hardware/drivers/d...

By @txdv - 7 months
So the root cause of the slowness was not found, it was just circumvented by keeping 3 xterms open and just using hiding/showing them?
By @aardshark - 7 months
300ms for startup still sounds slow to me. Not ridiculously so, but it won't give that snappy feeling.
By @pcthrowaway - 7 months
> I’ve been using this configuration for a few days, so far it’s working great. I haven’t noticed any issues running it this way.

The journey was very useful, even the destination may be pretty specific to your needs. The process of how to go about debugging minor annoyances like this is really hard to learn about.

By @zokier - 7 months
Just for fun I did film some video footage from my 60Hz monitor to see how quickly my terminal starts up. Seems like 2-3 frames to show up the terminal window, and 1-2 frames to show shell prompt. So 50 ms - 83 ms. This is with foot terminal on Sway.

My very unscientific methodology was to run

    $ echo hello && foot
in a terminal and measure the time between the hello text appearing and the new window appearing. Looking at my video, the time from physical key press to "hello" text appearing might be 20ish ms but that is less clear, so about 100 ms total from key press to shell prompt.

This is pretty much completely untuned setup, I haven't done any tweaks to improve the figures. Enabling foot server might shave some milliseconds, but tbh I don't feel that's necessary.

It'd be fun to do this with better camera, and also with better monitors. Idk how difficult it would be to mod in some LED to keyboard to capture the exact moment the key is activated, just trying to eyeball the key movement is not very precise.

By @mberning - 7 months
This is a tour de force on the type of curiosity it takes to be really successful with computers.
By @abraae - 7 months
I'm at the tail end of my career, so working on efficiency gains like this doesn't usually add up for me.

However I was interested in knowing whether it does for the author.

Assuming he/she does suffer this 1300 ms delay "hundreds" of times a day (let's say 200) and for the sake of argument they use their computer 300 days a year and have 20 years of such work ahead of them with this config, then this inefficiency will total 1300 x 200 x 300 x 20 / 1000 / 60 / 60 hours wasted during author's lifetime - some 430 hours.

So well worth the effort to fix!

By @jd3 - 7 months
I'm so distracted by latency that I run my macOS with vsync disabled 24/7 (through Quartz Debug).

When I used to use Windows 10+ years ago, I had decent luck using xming + cygwin + Cygwin/X + bblean to run xterm in a minimal latency/lag environment.

I also launch Chrome/Spotify/Slack desktop using:

$ open -a open -a Google\ Chrome --args --disable-gpu-vsync --disable-smooth-scrolling

By @Borg3 - 7 months
Very nice article, I love such debugging. I sometimes do it myself too.

Anyway, this also made me think about general bloat we have in new OSes and programs. Im still on old OS running spinning rust and bash here starts instantly when cache is hot. I think GUI designers lost an engineer touch...

By @aardshark - 7 months
300ms for startup still sounds slow to me. Not ridiculously so, but it won't give that snappy feeling.
By @tonymet - 7 months
We need a community of those obsessed with responsive applications. UI latency irks me on every device. Not only computers and smart phones, but now TVs, refrigerators, cars all have atrocious UI latency.

Great debugging work to come up with a solution!

By @pikseladam - 7 months
it was a fun read
By @aftbit - 7 months
Upvote just for teaching me about the existence of `hyperfine`.

    $ hyperfine 'alacritty -e true'
    Benchmark 1: alacritty -e true
      Time (mean ± σ):      84.1 ms ±   4.9 ms    [User: 40.1 ms, System: 30.8 ms]
      Range (min … max):    80.5 ms … 104.4 ms    32 runs
    
    $ hyperfine 'xterm -e true'
    Benchmark 1: xterm -e true
      Time (mean ± σ):      81.9 ms ±   2.6 ms    [User: 21.7 ms, System: 7.9 ms]
      Range (min … max):    74.9 ms …  87.1 ms    37 runs
    
    $ hyperfine 'wezterm -e true'
    Benchmark 1: wezterm -e true
      Time (mean ± σ):     211.7 ms ±  13.4 ms    [User: 41.4 ms, System: 60.0 ms]
      Range (min … max):   190.5 ms … 240.5 ms    15 runs