Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Optimize CLI commands performance #104

Open
nikitabobko opened this issue Jan 6, 2024 · 2 comments
Open

Optimize CLI commands performance #104

nikitabobko opened this issue Jan 6, 2024 · 2 comments
Labels
problem Neither a bug, nor a feature request triaged The issue makes sense to maintainers

Comments

@nikitabobko
Copy link
Owner

aerospace list-workspaces is slow. Well, aerospace list-workspaces is ~100ms which by itself is fine.

(1) But 100ms is the result when AeroSpace is idle. Currently AeroSpace performs all operations in one thread. Considering that Ax calls are blocking, 100ms becomes worse when a lot of Ax calls are performed.

It can be tested with alt-w = ['workspace W', 'exec-and-forget bash -c "time ~/.bin/aerospace list-workspaces" 2> ~/log']. I have 9 windows opened, and I get 300ms in ~/log. 300ms is not that nice

(2) The problem becomes worse when users need to call aerospace list-workspaces several times (E.g. to get all workspaces and focused workspaces). list-workspaces is designed to be called several times. Maybe the design could be reconsidered, or --json flag could be provided. With JSON format, AeroSpace could dump out all the info it knows about workspaces in a structured way.

Reported at: #81

nikitabobko added a commit that referenced this issue Jan 8, 2024
Motivation:
1. Small optimization. One less client-server hoop.
   #104

   Before the commit

      (debug, idle)
      time aerospace list-workspaces --all
      real    0m0.041s
      user    0m0.002s
      sys     0m0.001s

      (debug, busy, 8 windows)
      alt-w = ['workspace W', 'exec-and-forget bash -c "time /Users/bobko/a/AeroSpace/LocalPackage/.build/debug/aerospace list-workspaces --all" 2> ~/log']
      real    0m0.165s
      user    0m0.002s
      sys     0m0.001s

   After the commit

      (debug, idle)
      time aerospace list-workspaces --all
      real    0m0.024s
      user    0m0.002s
      sys     0m0.002s

      (debug, busy, 8 windows)
      alt-w = ['workspace W', 'exec-and-forget bash -c "time /Users/bobko/a/AeroSpace/LocalPackage/.build/debug/aerospace list-workspaces --all" 2> ~/log']
      real    0m0.145s
      user    0m0.004s
      sys     0m0.004s

2. Now if server and client have different versions, but understand each
   other, the error is not reported
@nikitabobko nikitabobko added the problem Neither a bug, nor a feature request label Mar 21, 2024
@nikitabobko nikitabobko changed the title Optimize query commands performance Optimize CLI commands performance Aug 14, 2024
jakenvac pushed a commit to jakenvac/AeroSpace that referenced this issue Aug 16, 2024
Motivation:
1. Small optimization. One less client-server hoop.
   nikitabobko#104

   Before the commit

      (debug, idle)
      time aerospace list-workspaces --all
      real    0m0.041s
      user    0m0.002s
      sys     0m0.001s

      (debug, busy, 8 windows)
      alt-w = ['workspace W', 'exec-and-forget bash -c "time /Users/bobko/a/AeroSpace/LocalPackage/.build/debug/aerospace list-workspaces --all" 2> ~/log']
      real    0m0.165s
      user    0m0.002s
      sys     0m0.001s

   After the commit

      (debug, idle)
      time aerospace list-workspaces --all
      real    0m0.024s
      user    0m0.002s
      sys     0m0.002s

      (debug, busy, 8 windows)
      alt-w = ['workspace W', 'exec-and-forget bash -c "time /Users/bobko/a/AeroSpace/LocalPackage/.build/debug/aerospace list-workspaces --all" 2> ~/log']
      real    0m0.145s
      user    0m0.004s
      sys     0m0.004s

2. Now if server and client have different versions, but understand each
   other, the error is not reported
@liamaharon
Copy link

Note this gets extremely bad when CPU usage is saturated (e.g. when compiling a large Rust project), Aerospace becomes completely unresponsive.

The 'fix' I have in place for now is limiting the number of CPUs available to rustc.

@nikitabobko nikitabobko added the triaged The issue makes sense to maintainers label Oct 27, 2024
@CheariX
Copy link

CheariX commented Nov 23, 2024

I am using numerous aerospace CLI commands for my daily workflows (e.g., cycling through windows of active applications, focusing particular windows, etc.). The ~100 ms CLI became somewhat annoying to me since I started to feel the delay when I have ~300 ms in my scripts.

For this purpose, I experimented with some hacky cache:

on-focus-changed = [
    'exec-and-forget aerospace list-windows --focused --format "%{window-id}" > /tmp/aerospace_cache_focused.json', 
    'exec-and-forget aerospace list-windows --all --json --format "%{window-id} %{window-title} %{app-bundle-id} %{app-name} %{app-pid} %{workspace} %{monitor-id} %{monitor-name}"  > /tmp/aerospace_cache_windows.json'
]

From there on, my scripts simply use cat $file | jq.
The impact is remarkable. My scripts now feel totally different.

I was wondering if some internal caching mechanism like that would be useful in general. For example, the focused window will not change until the "on-focus-changed" event is triggered. However, if I run it 3 times in a row, each run takes approximately 100 milliseconds.
I know, this does not work for every case, e.g. a window's title could change in the background, but it could at least improve performance for all event-based changes.
(Maybe the list-* commands could have either a --fresh or a --cached flag).

badprince71 added a commit to badprince71/AeroSpace that referenced this issue Mar 9, 2025
Motivation:
1. Small optimization. One less client-server hoop.
   nikitabobko/AeroSpace#104

   Before the commit

      (debug, idle)
      time aerospace list-workspaces --all
      real    0m0.041s
      user    0m0.002s
      sys     0m0.001s

      (debug, busy, 8 windows)
      alt-w = ['workspace W', 'exec-and-forget bash -c "time /Users/bobko/a/AeroSpace/LocalPackage/.build/debug/aerospace list-workspaces --all" 2> ~/log']
      real    0m0.165s
      user    0m0.002s
      sys     0m0.001s

   After the commit

      (debug, idle)
      time aerospace list-workspaces --all
      real    0m0.024s
      user    0m0.002s
      sys     0m0.002s

      (debug, busy, 8 windows)
      alt-w = ['workspace W', 'exec-and-forget bash -c "time /Users/bobko/a/AeroSpace/LocalPackage/.build/debug/aerospace list-workspaces --all" 2> ~/log']
      real    0m0.145s
      user    0m0.004s
      sys     0m0.004s

2. Now if server and client have different versions, but understand each
   other, the error is not reported
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
problem Neither a bug, nor a feature request triaged The issue makes sense to maintainers
Projects
None yet
Development

No branches or pull requests

3 participants