rubyinstaller2 icon indicating copy to clipboard operation
rubyinstaller2 copied to clipboard

Very slow on startup

Open hyrious opened this issue 5 months ago • 1 comments

I'm not sure if this is the right place to ask...

It takes at least 1 second to start ruby.exe on my PC while it starts instantly on other OS. So I opened process monitor to see what actually happens:

Image (runs ruby -e 1)

  1. My ruby's arch is x64-mingw-ucrt.
  2. It is obvious that ruby loads its stdlib on start, there're a lot of files. For each file, it seems ruby.exe will query fs info of every parent path to get a normalized path.
  3. There're a lot of duplicated works, the CreateFile .. CloseFile pattern occurs about a hundred of times to test the same folder. Each task is very quick, but when there're 100 tasks it will cost > 1 second.

Additional contexts:

  1. I noticed that this code in ruby's source code may be doing these path checking works.

  2. For comparison, I also profiles node.exe -r ./a.js, which is quite fast:

    Image

    The reasons behind, which I could come up with, are that:

    • node.exe intentionally bundles all it's stdlib into the binary, that reduces a lot of time to load the hundreds of files.
    • As of loading a single file, node.exe does very little work (4 winapi calls) where ruby.exe does a lot (8 * N calls (where N is the dir depth of a file path) + 1 CreateFileMapping which seems to lock the readers of a file).

hyrious avatar Jul 13 '25 03:07 hyrious

This is a long-standing problem with Ruby on Windows. (e.g. https://bugs.ruby-lang.org/issues/19378)

I've had good experience using Bootsnap, especially the path pre-scanning feature (https://github.com/rails/bootsnap?tab=readme-ov-file#path-pre-scanning), perhaps it's suitable for your application, too.

daniel-rikowski avatar Sep 19 '25 09:09 daniel-rikowski