rbenv / ruby-build

A tool to download, compile, and install Ruby on Unix-like systems.
https://rbenv.org/man/ruby-build.1
MIT License
3.89k stars 785 forks source link

On Ubuntu 24.04 the launch of Ruby built using ruby-build is noticeably slower #2394

Closed appomsk closed 5 months ago

appomsk commented 5 months ago

Kubuntu 24.04. After building 3.3.1 on my pretty old machine and discovering that it's start is almost 10 times slower then python and almost 30 times slower then perl I decided to check it with version 3.2 and find out that ruby-build version is one and half time slower than system one. I belive it shouldn't be such. Ruby is already very slow.

rbenv global         
3.2.4

ruby -v              
ruby 3.2.4 (2024-04-23 revision af471c0e01) [x86_64-linux]

hyperfine 'ruby -e 1'
Benchmark 1: ruby -e 1
  Time (mean ± σ):     163.0 ms ±   1.1 ms    [User: 104.9 ms, System: 59.5 ms]
  Range (min … max):   160.7 ms … 165.2 ms    18 runs

/usr/bin/ruby -v
ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [x86_64-linux-gnu]

hyperfine '/usr/bin/ruby -e 1'
Benchmark 1: /usr/bin/ruby -e 1
  Time (mean ± σ):     106.4 ms ±   1.1 ms    [User: 82.8 ms, System: 23.5 ms]
  Range (min … max):   104.2 ms … 109.2 ms    27 runs
hsbt commented 5 months ago

Can you try it with $RBENV_ROOT/versions/3.2.3/bin/ruby? rbenv uses shell shims for invoking ruby or other related commands.

$ hyperfine "ruby -e 1"
Benchmark 1: ruby -e 1
  Time (mean ± σ):     166.6 ms ± 143.7 ms    [User: 37.1 ms, System: 20.4 ms]
  Range (min … max):   113.8 ms … 575.1 ms    10 runs

$ hyperfine "~/.local/share/rbenv/versions/3.2.3/bin/ruby -e 1"
Benchmark 1: ~/.local/share/rbenv/versions/3.2.3/bin/ruby -e 1
  Time (mean ± σ):      50.7 ms ±  38.0 ms    [User: 26.6 ms, System: 13.2 ms]
  Range (min … max):    39.6 ms … 187.9 ms    15 runs
appomsk commented 5 months ago

Well. So it's shims... Just want Ruby to be a bit faster. I apologize for the anxiety. You can close the issue.

hyperfine '.rbenv/versions/3.2.4/bin/ruby -e 1'
Benchmark 1: .rbenv/versions/3.2.4/bin/ruby -e 1
  Time (mean ± σ):     110.1 ms ±   2.8 ms    [User: 84.5 ms, System: 25.1 ms]
  Range (min … max):   107.3 ms … 118.5 ms    26 runs

hyperfine '/usr/bin/ruby -e 1'
Benchmark 1: /usr/bin/ruby -e 1
  Time (mean ± σ):     106.7 ms ±   1.0 ms    [User: 82.3 ms, System: 24.1 ms]
  Range (min … max):   104.1 ms … 108.7 ms    27 runs
mislav commented 5 months ago

The slowness of rbenv as opposed to invoking a ruby binary directly is known and documented (linked from the README): https://github.com/rbenv/rbenv/wiki/Comparison-of-version-managers

mislav commented 5 months ago

Just want Ruby to be a bit faster

100 ms for a process startup can indeed feel slow, but sometimes that can't be helped with the Ruby interpreter. Depending on your system resources, 50–100 ms might be a reasonable amount of startup time. On my machine, it's more like 30 ms.

If you want to further speed up Ruby, you can disable it loading Rubygems with --disable-gems. Here it is on my Apple M1 machine:

$ hyperfine --warmup 5 '~/.rbenv/versions/3.2.2/bin/ruby --disable-gems -e 1'

  Time (mean ± σ):       6.2 ms ±   0.4 ms    [User: 3.6 ms, System: 2.1 ms]
  Range (min … max):     5.8 ms …   7.6 ms    360 runs

Of course, a ruby interpreted started this way is less useful because it won't be able to load any gem dependenices.