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

distrobox-enter: improve cmd composition speed #1649

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

TigerGorilla2
Copy link
Contributor

@TigerGorilla2 TigerGorilla2 commented Jan 5, 2025

TLDR:
distrobox-enter is slow. I did some timing of the code using the original but timed and slightly modified but eventually came to the improvements proposed by this PR.

Long version:

I had noticed that my command to launch a terminal

distrobox-enter home -- foot

has become slower but especially so on my laptop, taking multiple seconds.

I then eliminated foot by running time distrobox-enter home -- true. It was still slow. So I tried to launch the podman-exec manually and it only took ~250ms instead of 2-3 seconds.

Now I was pretty sure it was on distrobox' side and figured out which part of the distrobox-enter script is taking so much time using the original but timed . The slowest part is in this loop

My first idea was to replace the grep -q " " by e.g.

case ...
    *" "*) ...
    *) ...
esac

to check if a line had a space. This did improve it to close to one second total runtime (-1sec)

Timings from my machine
[david@david-laptop:/tmp/dbx-wuOy]$ git co slower
Switched to branch 'slower'

[david@david-laptop:/tmp/dbx-wuOy]$ time ./distrobox-enter home -- true
before gen-cmd took 300ms
gen-cmd took 116ms

composing cmd (bottleneck) took 1813ms
prepending container cmd took 33ms
launching the cmd in container took 289ms

real	0m2.597s
user	0m1.299s
sys	0m1.511s

[david@david-laptop:/tmp/dbx-wuOy]$ time ./distrobox-enter home -- true
before gen-cmd took 278ms
gen-cmd took 215ms

composing cmd (bottleneck) took 1994ms
prepending container cmd took 41ms
launching the cmd in container took 383ms

real	0m2.965s
user	0m1.464s
sys	0m1.728s

[david@david-laptop:/tmp/dbx-wuOy]$ git co faster
Switched to branch 'faster'

[david@david-laptop:/tmp/dbx-wuOy]$ time ./distrobox-enter home -- true
before gen-cmd took 136ms
gen-cmd took 103ms

composing cmd (bottleneck) took 644ms
prepending container cmd took 24ms
launching the cmd in container took 261ms

real	0m1.213s
user	0m0.591s
sys	0m0.677s

[david@david-laptop:/tmp/dbx-wuOy]$

I still wasn't fully satisfied and wanted to remove any ifs and cases. Which resulted in this PR. This relies on the fact, that any container-manager flag can be given using --flagname=VALUE (note the =). The changes also have the side effect of making --additional-flags accepting both styles (using = or a space) of sepearting the flag name from the value.

Some more timings including this PR (branch 'even-faster')
[nix-shell:/tmp/dbx-wuOy]$ git checkout slower
Switched to branch slower

[nix-shell:/tmp/dbx-wuOy]$ hyperfine ./distrobox-enter home -- true
Benchmark 1: ./distrobox-enter home -- true
  Time (mean ± σ):      3.673 s ±  1.024 s    [User: 1.857 s, System: 2.190 s]
  Range (min … max):    2.053 s …  4.531 s    10 runs


[nix-shell:/tmp/dbx-wuOy]$ git checkout faster
Switched to branch faster

[nix-shell:/tmp/dbx-wuOy]$ hyperfine ./distrobox-enter home -- true
Benchmark 1: ./distrobox-enter home -- true
  Time (mean ± σ):      2.141 s ±  0.693 s    [User: 1.002 s, System: 1.343 s]
  Range (min … max):    1.389 s …  3.123 s    10 runs

[nix-shell:/tmp/dbx-wuOy]$ git checkout even-faster
Switched to branch even-faster
Your branch is up to date with origin/even-faster.

[nix-shell:/tmp/dbx-wuOy]$ hyperfine ./distrobox-enter home -- true
Benchmark 1: ./distrobox-enter home -- true
  Time (mean ± σ):     633.9 ms ± 173.3 ms    [User: 420.5 ms, System: 195.1 ms]
  Range (min … max):   417.9 ms … 802.6 ms    10 runs

@TigerGorilla2 TigerGorilla2 marked this pull request as ready for review January 5, 2025 15:46
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

Successfully merging this pull request may close these issues.

1 participant