Very slow on startup
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:
(runs
ruby -e 1)
- My ruby's arch is
x64-mingw-ucrt. - 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.
- There're a lot of duplicated works, the
CreateFile .. CloseFilepattern 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:
-
I noticed that this code in ruby's source code may be doing these path checking works.
-
For comparison, I also profiles
node.exe -r ./a.js, which is quite fast:The reasons behind, which I could come up with, are that:
node.exeintentionally 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.exedoes very little work (4 winapi calls) whereruby.exedoes a lot (8 * N calls (where N is the dir depth of a file path) + 1CreateFileMappingwhich seems to lock the readers of a file).
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.