Speeding up Ruby script startup in Windows or Cygwin - windows

I've got a ruby script which takes about 30 seconds to startup. It takes that much because it tries to load all libraries and stuff.
When I do ruby.exe -v it's instant.
I don't want to touch the original ruby script, which is not written by me.
What are the tricks to speed this process up?
Can I precompile it?
Can I precache all of these files?
I need to do this under Windows or Cygwin.
UPDATE :
Scripts is quite slow in Linux/Mac as well, this condition is not specific to Windows.
This is normal ruby 1.8.7 (similar speed in other ruby versions)
Main bottleneck is loading so many libraries (I removed unrequired files and libraries and decrease the time to drastically but still slow)

I presume the script uses rubygems? (It'll say require "rubygems" if so)
You could try installing minigems (gem install minigems) and then use minigems instead of rubygems - should speed things up a little.

Sorry, but there is no way to compile a ruby script. What sort of stuff is this script loading/doing?
You're right, 30 seconds is quite long. Is this script making calls out to the web or databases that are very expensive? It's hard to believe that libraries would take so long to load
... I just noticed the comments and saw that you got it down to 1-4 seconds...that's very good, especially when coming down from 30. Other comments are right, please post the requires at the top of the script

Related

Is Julia on Visual Studio code run on windows, slow? (Julia indexing Dateframes 0%...)

I am using Julia (1.7.2) on VS code (version 1.65) run on windows 10. Start up time is really slow, showing the line: "Julia indexing Dateframes 0%...". Also package loading takes some time, not that much though. First time I came across this I just let it load, so I don't know exactly how much time it took. Tried uninstall and re-installing but came across the same problem. Anyone faced something similar? Is there something I can do to fix this? My computer details:
What I was advised to do was work in environments. Install the packages to use in an environment, and it would load faster. The problem I perceive is that you have many libraries installed, so it takes time to load.

How to provide an online ruby REPL?

On a site like www.codewars.com, one can run ruby in a sort of sandbox, almost identical to IRB.
How does this actually work?
If the submitted code is eval()d, what's preventing me from submitting a system("rm -rf *") or redefining basic functions so that 50% of the time Array.sort actually runs Array.shuffle?
The simplest and safest solution is to run the Ruby code on separate computer, which you wipe and re-install after every run. This is, however, also a pretty heavyweight solution.
More lightweight, but (almost) as safe, would be using a virtual machine or a container instead of a whole separate computer, and e.g. using a read-only filesystem with a ramfs overlay, which you umount after every run. (Or just throw away and recreate the container.)
You could also use JRuby together with the JVM's security features (or IronRuby with the CLI's). The JVM has sandboxing features for JVM programs, and after all, JRuby is just a Java program like any other.
Lastly, you could write your own Ruby implementation with sandboxing in mind, or modify an existing one. The three options above are fairly simple, this one is hard, because most Ruby implementations aren't designed for sandboxing. TryRuby.Com worked this way, for example, and it took a significant amount of time to update it for Ruby 1.9, because it was originally based on a modified version of MRI, but MRI doesn't support Ruby 1.9. So, the implementation had to be switched to YARV, and a lot of the modifications to make it sandboxing-safe had to re-implemented from scratch. (The JRuby/IronRuby option above is similar to this, but you push off the work of making the implementation sandbox-safe to someone else, e.g. Oracle or Microsoft.)
A not-so-safe but also simple solution would be to run the interpreter under a restricted user account.
Of course, you can combine multiple approaches for defense-in-depth, for example, running a sandboxed interpreter under a restricted user account on a separate VM.
What does not work is to statically analyze the code before running it. The pesky Halting Problem bites us here.

How to unload/un-require gem

I'm working on mobile ⇔ web tests, combining Calabash and Capybara gems. The gems call methods that have the same name (like page), and I find it hard to make my code execute methods of desired files.
Can I un-require a gem? Please let me know if this is possible and whether it is a good idea.
Can I un-require a gem?
No.
require simply runs the file. Period. "Unrequiring" would then basically mean running the file in reverse, undoing everything the code did. But that is impossible. Imagine a very simple gem which only contains this single line:
print "\a"
This will cause the computer to beep once. "Unrequiring" would then mean that everybody who is in the vicinity of the computer would forget that they heard the beep. That's just ridiculous.
There are programming languages, libraries, and calculi, which are explicitly designed such that they can – under very limited, heavily restricted circumstances – be "run in reverse", but Ruby is not such a language.

Slow loading of rails environment

Similar issue to Slow loading rails environment
Loading the rails environment takes quite a bit of time and I'm not sure exactly why.
time ruby -r./config/environment.rb -e ""
real 0m18.590s
user 0m17.200s
sys 0m1.320s
Are there any tools/ways that can help me find why it is spending so much time to load the environment?
The project is fairly large, so I am assuming that it is coming from all the gem dependencies, but I would think that it would be able to be improved somehow.
If you are using Ruby 1.9 then see this blog post it may be the issue you are experiencing. If it is it has to do with the amount of requires in your project and the way that method is implemented in 1.9. There is a patch available to improve this performance.
I tried patching my ruby with the rhnh patch cited above as well as the rvm-patchsets (on independent ruby installs of course) but didn't pick up a lot of performance. But some do it seems so maybe it's a ruby version or lower level issue.
My current workaround, at least in my dev environment, is to use rails-sh to preload the environment one time and then reuse it in your rails/rake commands. It's a big performance pickup. Wrote more details on it in this answer.

Improving Scala script startup time -- client mode?

I'd like to get short Scala scripts running as fast as python scripts do, particularly in terms of script startup time.
Can anyone recommend some ways of doing this, that doesn't involve compiling with GCJ, for instance?
One way I can think of is to run the script using the JVM's client mode, but I can't seem to get this working. An example (known-good) shebang for this would be great.
UPDATE: I'm aware of the other questions, but don't think any workable answers have been found so far, as I'm looking for solutions that work on STANDARD installs, without additional requirements. This is what I was trying to get at with "doesn't involve compiling with GCJ, for instance".
It seems that -client mode is designed for this express purpose, but it's just awkward to activate from scala scripts for some reason.
As many other questions have gone before, if one could only know how to look for them, use Nailgun.
Other ways to improve on script performance is to start fsc at system boot, so it will be available for scripts, and use -savecompiled, to avoid repeated compilation of scripts.
EDIT
You mention -client mode, but I think that's really not a good choice. The will give you a slower Scala compiler, and do little to improve the startup time of the compiler itself, if not Java. Much better to have fsc as daemon, running as -server, and/or save compiled scripts with -savecompiled.
Now, I don't know what problems you are having with -client, but I have read that it doesn't work with 64 bits JVM. Might that be your case?
PS: Looking up similar questions, I noticed JRuby has builtin Nailgun support!
I haven't tried this yet, but scala-native promises near-instant startup because it compiles into a native binary. So one solution is to provide it as a number of binary downloads.
http://www.scala-native.org/en/latest/
I just tried to pass the '-client' parameter through Scala to the JVM this way:
#!/bin/sh
exec scala -J-client "$0" "$#"
!#
args.foreach(println)
It seems to work. Daniel C. Sobral wrote that he read that it doesn't work with 64 bits JVM. I don't know, maybe this is outdated. Anyway it seem to drop the startup time a little bit.
Running:
:~$ time /tmp/testScalasScript arg1
arg1
real 0m2,170s
user 0m2,228s
sys 0m0,217s
This was the fastest run of just a couple of tests. Without it it takes up to 0.5s longer. But this was really a quick test and should be done more systematically to come to meaningful results.
Wasn't there a way to make Scala compile and save the compilation result at the first run of the script for faster reuse? But I don't no for sure at this point of time.
UPDATE:
I just saw that on 'java -help' the option '-client' is not documented (anymore?). Anyway there is no error thrown (which is done on usage of unexisting options). So I am not sure if the '-client' option really has consequences.

Resources