Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support sending batch messages in a single sketchybar -m call #17

Closed
azuwis opened this issue Aug 24, 2021 · 17 comments
Closed

Support sending batch messages in a single sketchybar -m call #17

azuwis opened this issue Aug 24, 2021 · 17 comments

Comments

@azuwis
Copy link
Contributor

azuwis commented Aug 24, 2021

The current method to config SketchyBar is quite slow. It take 2.4 second for the following simple sketchybarrc to finish.

~/.config/sketchybar/sketchybarrc
############## BAR ##################
sketchybar -m config height               25
sketchybar -m config position             top
sketchybar -m config padding_left         10
sketchybar -m config padding_right        10
sketchybar -m config bar_color            0x44000000 #0xaf202020

############## SCRIPT CACHING ############
sketchybar -m default cache_scripts on

############## SPACES ###############
sketchybar -m default icon_font "Hack Nerd Font:Bold:17.0"
sketchybar -m default icon_color 0xffffffff
sketchybar -m default label_font "Hack Nerd Font:Bold:14.0"
sketchybar -m default label_color 0xffffffff
sketchybar -m default label_padding_left 4
sketchybar -m default icon_padding_left 16

sketchybar -m add component space code left
sketchybar -m set code associated_display 1
sketchybar -m set code associated_space 1
sketchybar -m set code icon_font "Hack Nerd Font:Bold:20.0"
sketchybar -m set code icon 
sketchybar -m set code icon_highlight_color 0xff48aa2a
sketchybar -m set code icon_padding_left 0
sketchybar -m set code label code
sketchybar -m set code click_script "yabai -m space --focus 1"

sketchybar -m add component space writing left
sketchybar -m set writing associated_display 1
sketchybar -m set writing associated_space 2
sketchybar -m set writing icon_highlight_color 0xfffab402
sketchybar -m set writing icon 
sketchybar -m set writing label tex
sketchybar -m set writing click_script "yabai -m space --focus 2"

sketchybar -m add component space reading left
sketchybar -m set reading associated_display 1
sketchybar -m set reading associated_space 3
sketchybar -m set reading icon_highlight_color 0xff7fe5f0
sketchybar -m set reading icon 
sketchybar -m set reading label web
sketchybar -m set reading click_script "yabai -m space --focus 3"

sketchybar -m add component space entertainment left
sketchybar -m set entertainment associated_display 1
sketchybar -m set entertainment associated_space 4
sketchybar -m set entertainment icon_font "Hack Nerd Font:Bold:19.0"
sketchybar -m set entertainment icon_highlight_color 0xfffff68f
sketchybar -m set entertainment icon 
sketchybar -m set entertainment label idle
sketchybar -m set entertainment click_script "yabai -m space --focus 4"

sketchybar -m add component space zoom left
sketchybar -m set zoom associated_display 2
sketchybar -m set zoom associated_space 5
sketchybar -m set zoom icon V
sketchybar -m set zoom icon_padding_left 0
$ bash -c 'time ~/.config/sketchybar/sketchybarrc'
real	0m2.393s
user	0m0.632s
sys	0m1.107s

Simply run sketchybar -m 48 times (same as the sketchybarrc above) takes about the same time:

$ bash -c 'time (for i in {1..48}; do sketchybar -m >&/dev/null; done)'
real	0m2.083s
user	0m0.628s
sys	0m1.092s

I suggest add a new batch message to send all messages in a single fork:

#!/bin/sh
sketchybar -m batch <<EOF
config height               25
config position             top
config padding_left         10
config padding_right        10
config bar_color            0x44000000 #0xaf202020
EOF
@azuwis
Copy link
Contributor Author

azuwis commented Aug 24, 2021

For comparison, yabai takes 1/10 times:

$ bash -c 'time (for i in {1..48}; do yabai -m >&/dev/null; done)'
real	0m0.227s
user	0m0.166s
sys	0m0.049s

@FelixKratz
Copy link
Owner

Yes, there is still something to do in terms of performance, see #4

@FelixKratz
Copy link
Owner

When I run sketchybar 48 times it compares to the time it takes to invoke yabai 48 times quite closely actually. Do you run sketchybar in debug mode with address sanitization turned on?

$ bash -c 'time (for i in {1..48}; do sketchybar -m >&/dev/null; done)'

real	0m0.468s
user	0m0.267s
sys	0m0.120s
$ bash -c 'time (for i in {1..48}; do yabai -m >&/dev/null; done)'

real	0m0.424s
user	0m0.279s
sys	0m0.127s

So maybe it is enough to speed up the message handling for the duration of the initial loading of the configuration.

@azuwis
Copy link
Contributor Author

azuwis commented Aug 31, 2021

Strange.

I use nix to manage macOS, and write some package definitions to compile and run sketchybar and spacebar:

https://github.com/azuwis/nix-config/blob/darwin/pkgs/sketchybar/default.nix
https://github.com/azuwis/nix-config/blob/darwin/pkgs/spacebar/default.nix

spacebar works fine:

$ bash -c 'time (for i in {1..48}; do spacebar -m >&/dev/null; done)'
real	0m0.211s
user	0m0.151s
sys	0m0.048s

The main different is sketchybar need clang 12 (nixpkgs.clang12Stdenv), while spacebar need clang 11 (nixpkgs.stdenv).

Maybe nix causes the problem.

@cmacrae Any idea?

$ nix-info -m           
 - system: `"aarch64-darwin"`
 - host os: `Darwin 20.6.0, macOS 11.5.2`
 - multi-user?: `yes`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.3.15`
 - channels(azuwis): `"darwin, home-manager, nixpkgs-21.11pre312229.08ef0f28e3a"`
 - channels(root): `""`
 - nixpkgs: `/Users/azuwis/.nix-defexpr/channels/nixpkgs`

@FelixKratz
Copy link
Owner

I have never used nix before, how is the source code compiled exactly? Which c flags are used when installing with nix? If it just executes the „make“ command it will produce the very(!) slow debug version of the app. The release flags are only set up correctly when running „make install“

@azuwis
Copy link
Contributor Author

azuwis commented Aug 31, 2021

I checked the makefile of spacebar, it does not have -fsanitize=address -fsanitize=undefined.

Can you please move debug related flags to maybe make debug?

AFAIK, lots of build systems just run make to build the binary, like Debian(debhelper), Archlinux(aur), Nix(nixpkgs).

@FelixKratz
Copy link
Owner

FelixKratz commented Sep 1, 2021

This is what I was suspecting all along. I have restructured the makefile to create the optimized executable with make 606939d. Is is faster now?

@azuwis
Copy link
Contributor Author

azuwis commented Sep 1, 2021

It's much faster now:

$ bash -c 'time (for i in {1..48}; do sketchybar -m >&/dev/null; done)'
real	0m0.210s
user	0m0.150s
sys	0m0.048s

Are you still planning to implement sending batch messages? I'll leave this issue open if so.

@FelixKratz
Copy link
Owner

I don‘t think batch messages are a priority right now. I have implemented the ability to „freeze“ the bar on startup, this will improve startup times at bit. Simply add
sketchybar -m freeze on
At the beginning of sketchybarrc and
sketchybar -m freeze off
Just before the
sketchybar -m update
This should improve initial loading by a bit.

@FelixKratz
Copy link
Owner

Batch messages are implemented 05bc579. See README or updated example sketchybarrc for more info.

@azuwis
Copy link
Contributor Author

azuwis commented Sep 7, 2021

A simple benchmark:

Before and after azuwis/nix-config@e0adfe5

$ hyperfine -w 20 -m 100 ./status.sh
Benchmark #1: ./status.sh
  Time (mean ± σ):      49.6 ms ±   1.1 ms    [User: 21.5 ms, System: 12.6 ms]
  Range (min … max):    46.6 ms …  52.3 ms    100 runs
$ hyperfine -w 20 -m 100 ./status.sh
Benchmark #1: ./status.sh
  Time (mean ± σ):      41.9 ms ±   1.2 ms    [User: 16.0 ms, System: 10.7 ms]
  Range (min … max):    38.6 ms …  44.0 ms    100 runs

It seems each sketchybar -m invoke takes about 3~4ms.

@azuwis
Copy link
Contributor Author

azuwis commented Sep 7, 2021

The sketchybarrc startup time reduced by 100ms after using batch(azuwis/nix-config@76e623a) 👏

$ hyperfine -m 10 /nix/store/0ribgmwm4pipysj4vwzqghw5w4wxbppz-sketchybarrc   
Benchmark #1: /nix/store/0ribgmwm4pipysj4vwzqghw5w4wxbppz-sketchybarrc
  Time (mean ± σ):     254.2 ms ±   3.3 ms    [User: 176.4 ms, System: 61.4 ms]
  Range (min … max):   244.2 ms … 257.2 ms    12 runs
$ hyperfine -m 10 /nix/store/qvyhkfq9dkvmgf9zrfl6kg5szfxc04i4-sketchybarrc
Benchmark #1: /nix/store/qvyhkfq9dkvmgf9zrfl6kg5szfxc04i4-sketchybarrc
  Time (mean ± σ):     145.3 ms ±   2.3 ms    [User: 98.6 ms, System: 36.7 ms]
  Range (min … max):   136.3 ms … 149.5 ms    21 runs

@azuwis
Copy link
Contributor Author

azuwis commented Sep 7, 2021

Idea for further improvement, batch for multiple bar items:

sketchybar -m batch \
           --add item item1 right \
           --set item1 key1=value1 \
                       script=script1 \
           --subscribe item1 event1 \
           --set item2 key2=values ...

@azuwis
Copy link
Contributor Author

azuwis commented Sep 7, 2021

sketchybar -m batch --set item label='' does not work, unlike sketchybar -m set item label ''.

@FelixKratz
Copy link
Owner

Fixed in 24fd74b.

@FelixKratz
Copy link
Owner

Idea for further improvement, batch for multiple bar items:

sketchybar -m batch \
           --add item item1 right \
           --set item1 key1=value1 \
                       script=script1 \
           --subscribe item1 event1 \
           --set item2 key2=values ...

This is now possible. You can essentially fully configure in one call since 95923db. Should be even faster then.

@azuwis
Copy link
Contributor Author

azuwis commented Sep 12, 2021

Wow, my startup time reduced to 15ms from 250ms(no batch).

$ hyperfine -w 10 -m 50 /nix/store/q48kq672kc7hs6gl9rfvynknandyi6f9-sketchybarrc
Benchmark #1: /nix/store/q48kq672kc7hs6gl9rfvynknandyi6f9-sketchybarrc
  Time (mean ± σ):      14.4 ms ±   0.5 ms    [User: 7.7 ms, System: 4.9 ms]
  Range (min … max):    13.4 ms …  18.4 ms    186 runs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants