public
Created

Potentially faster boot

  • Download Gist
application.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# THANKS EVERYONE FOR YOUR BELOVED BENCHMARKS.
# THIS PATCH IS NOW IN RAILS:
#
# https://github.com/rails/rails/commit/885a599303585b796da7a0a1c3ccd0bc5c642134
 
# Please add the following lines after Bundler.require
# and before "class Application < Rails::Application"
# in your config/application.rb
#
# Notice this is just an experiment, don't leave those
# lines there after the experiment. Then please benchmark
# your app boot time in development before and after adding
# those lines. A simple benchmark is:
#
# time script/rails runner "MODEL"
#
# Where MODEL is a model available in your app. Also,
# please run the command above at least three times to
# ensure we don't have fake samples. :)
#
# Please post the results in the comments below of the
# benchmarks before and after adding the code. <3
#
# This patch should work on Rails 3.0, 3.1 and master.
#
# Extra: if you could run your app in development and
# production (in your machine!!) with this patch on
# for a couple minutes and report any errors, you will
# win extra <3 <3 <3.
 
require "active_support/dependencies"
 
ActiveSupport::Dependencies::WatchStack.class_eval do
def watching?
!@watching.empty?
end
end
 
def load_dependency(file)
if ActiveSupport::Dependencies.load? && ActiveSupport::Dependencies.constant_watch_stack.watching?
ActiveSupport::Dependencies.new_constants_in(Object) { yield }
else
yield
end
rescue Exception => exception # errors from loading file
exception.blame_file! file
raise
end

Before

ruby script/rails runner User  12.38s user 2.59s system 62% cpu 23.958 total

After

ruby script/rails runner User  10.45s user 1.99s system 95% cpu 13.000 total

Before:
real 0m14.453s
user 0m12.255s
sys 0m1.289s

After:
real 0m13.132s
user 0m11.948s
sys 0m1.172s

Before:

script/rails runner User  9.24s user 0.40s system 99% cpu 9.680 total

After:

script/rails runner User  9.18s user 0.43s system 99% cpu 9.659 total

Thanks guys, I have updated the gist. Could you please run the command at
least a couple times (if you haven't) to ensure we won't have fake samples?
<3

Before:
real 0m18.012s
user 0m15.093s
sys 0m2.123s

After:
real 0m17.208s
user 0m14.558s
sys 0m2.116s

REE, Rails 3.0.11, bundler 1.1rc, development mode

w/o patch:
real 0m10.562s
user 0m8.585s
sys 0m1.828s

w/ patch:
real 0m10.200s
user 0m8.165s
sys 0m1.888s

@josevalim: Already did that :)

Updated, both are the second of two runs.

Before:
eal 0m13.350s
user 0m12.207s
sys 0m1.131s

After:
real 0m12.888s
user 0m12.037s
sys 0m1.154s

Ruby Enterprise Edition 2011.03
Rails 3.0.11

Before:
7,29s user 1,04s system 57% cpu 14,595 total

After:
6,72s user 0,80s system 95% cpu 7,853 total

oO Looks pretty good! <3

ruby -v
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.6.0], MBARI 0x6770, Ruby Enterprise Edition 2011.03

Before:

time script/rails runner "Edition"
real    0m5.193s
user    0m3.898s
sys 0m1.180s

After:

time script/rails runner "Edition"
real    0m4.829s
user    0m3.506s
sys 0m1.185s

Development (I ran 3 times and took the middle value):

Before:

09:59:25 $ time rails runner User

real    0m8.136s
user    0m7.380s
sys     0m0.727s

After:

10:00:34 $ time rails runner User

real    0m7.793s
user    0m7.075s
sys     0m0.696s

Production (I ran 3 times and took the middle value):

Before:

10:06:42 $ time rails runner -e production User 

real    0m18.210s
user    0m16.507s
sys     0m1.207s

After:

10:05:24 $ time rails runner -e production User 

real    0m18.104s
user    0m16.393s
sys     0m1.214s

rake stats:

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |  1229 |   993 |     149 |     112 |   0 |     6 |
| Helpers              |   687 |   616 |       1 |      76 |  76 |     6 |
| Models               |  3593 |  2712 |     156 |     260 |   1 |     8 |
| Libraries            |  1085 |   805 |      23 |     116 |   5 |     4 |
| Model specs          |  4397 |  3483 |       0 |       0 |   0 |     0 |
| Controller specs     |    26 |    19 |       0 |       1 |   0 |    17 |
| Library specs        |   282 |   216 |       0 |       0 |   0 |     0 |
| Request specs        | 11101 |  7423 |       0 |      39 |   0 |   188 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 22400 | 16267 |     329 |     604 |   1 |    24 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 5126     Test LOC: 11141     Code to Test Ratio: 1:2.2

Some code folders is missing from this stats ;)

Updated. Rails 3.1.0; Ruby 1.9.2-p180

Before:

real 0m17.903s
user 0m15.224s
sys 0m2.113s

After:

real 0m17.580s
user 0m14.889s
sys 0m2.118s

I did 3 or 4 runs, with the patch was always faster. The best one was:

real 0m16.896s
user 0m14.450s
sys 0m2.101s

Rails 3.0.10, Ruby 1.9.3dev
Before : real 0m12.844s user 0m11.525s sys 0m1.153s
After : real 0m12.086s user 0m10.937s sys 0m1.122s
Trying it out in my development environment, will check back in if anything wrong happens :-)

Rails 3.1.3 - Ruby 1.9.3

development environment:

before patch:

$ time script/rails runner "User"
real    0m4.174s
user    0m3.558s
sys 0m0.611s

after patch:

$ time script/rails runner "User"
real    0m3.845s
user    0m3.233s
sys 0m0.584s

production environment:

before patch:

$ time RAILS_ENV=production script/rails runner "User"
real    0m4.163s
user    0m3.590s
sys 0m0.553s

after patch:

$ time RAILS_ENV=production script/rails runner "User"
real    0m4.036s
user    0m3.402s
sys 0m0.612s

Ruby 1.9.3, Rails 3.1.3, development mode

Before:
./script/rails runner News 14,67s user 0,86s system 99% cpu 15,628 total

After:
./script/rails runner News 14,14s user 0,91s system 99% cpu 15,150 total

ruby 1.9.2 - rails 3.0.10

development: w/o
12,34s user 2,12s system 90% cpu 16,053 total

development w/
11,97s user 2,07s system 90% cpu 15,456 total

production w/o
13,06s user 2,19s system 93% cpu 16,353 total

production w
12,82s user 2,06s system 96% cpu 15,398 total

Rails 3.1.3 ruby 1.8.7
before :
time rails runner User

real 0m16.907s
user 0m13.755s
sys 0m2.995s

after :

real 0m13.583s
user 0m10.607s
sys 0m2.820s

1.9.2-p290 + Rails 3.1.3
before:

real 0m33.338s
user 0m25.985s
sys 0m1.974s

after:

real 0m27.726s
user 0m25.320s
sys 0m1.750s

Ruby 1.9.3

before:
script/rails runner "Account" 9.28s user 1.25s system 89% cpu 11.776 total

after:
script/rails runner "Account" 8.67s user 1.03s system 99% cpu 9.768 total

$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]
$ rails -v
Rails 3.1.0

development before patch:

real    0m12.640s
user    0m9.345s
sys     0m1.442s

development after patch:

real    0m10.866s
user    0m8.713s
sys     0m1.483s

production before patch:

real    0m12.030s
user    0m9.267s
sys     0m1.509s

production after patch:

real    0m11.595s
user    0m9.165s
sys     0m1.480s

Before:
real 0m12.152s | user 0m10.729s | sys 0m1.287s

After:
real 0m11.389s | user 0m10.030s | sys 0m1.308s

Mac OS 10.7.2, Ruby 1.9.2p290, Rails 3.0.11:

Before:
real 0m25.132s
user 0m23.358s
sys 0m1.784s

After:
real 0m24.741s
user 0m23.034s
sys 0m1.733s

Mac OS 10.7.2, Ruby 1.9.3p0, Rails 3.0.11:

Before:
real 0m8.456s
user 0m7.332s
sys 0m1.117s

After:
real 0m8.113s
user 0m6.999s
sys 0m1.108s

1.9.3-p0 + Rails 3.0.11

Development:
before:
real 0m12.548s
user 0m11.155s
sys 0m1.349s

after:
real 0m12.112s
user 0m10.663s
sys 0m1.378s

Production:
before:
real 0m12.645s
user 0m11.191s
sys 0m1.413s

after:
real 0m12.095s
user 0m10.689s
sys 0m1.356s

rails 3.1.3, ruby 1.9.3-p0

before
real 0m5.221s
user 0m4.750s
sys 0m0.439s

after
real 0m4.862s
user 0m4.413s
sys 0m0.423s

Before

real 0m8.655s
user 0m7.804s
sys 0m0.796s

After

real 0m7.866s
user 0m7.100s
sys 0m0.736s

before
./script/rails runner User 13,54s user 2,10s system 64% cpu 24,390 total

after
./script/rails runner User 12,82s user 1,83s system 90% cpu 16,219 total

ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]

before

real    0m7.799s
user    0m6.404s
sys 0m0.795s

after

real    0m6.726s
user    0m5.885s
sys 0m0.799s

No big difference for me.

Rails 3.1.0, ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.2.0]
Before:

    30.98s user 2.12s system 97% cpu 33.891 total

After:

    30.61s user 1.80s system 100% cpu 32.404 total

Ruby 1.9.3, Rails-3.1.3 (Ubuntu 11.10, 64bit)

before:
real 0m5.028s
user 0m4.720s
sys 0m0.268s

after:
real 0m4.929s
user 0m4.636s
sys 0m0.252s

ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.1.0]

  • before

    real 0m13.079s
    user 0m10.771s
    sys 0m1.721s

  • after

    real 0m12.095s
    user 0m10.242s
    sys 0m1.693s

Ruby: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
Rails: Rails 3.1.1
Development ENV

Before:
real 0m13.778s
user 0m8.749s
sys 0m0.460s

real 0m8.871s
user 0m8.513s
sys 0m0.320s

real 0m8.963s
user 0m8.569s
sys 0m0.356s

real 0m8.948s
user 0m8.545s
sys 0m0.364s

After:
real 0m8.167s
user 0m7.728s
sys 0m0.400s

real 0m8.159s
user 0m7.844s
sys 0m0.276s

real 0m8.165s
user 0m7.784s
sys 0m0.340s

real 0m8.159s
user 0m7.720s
sys 0m0.400s

Rails 3.1.0

ruby:
interpreter: "ruby"
version: "1.9.3p0"
date: "2011-10-30"
platform: "x86_64-linux"
patchlevel: "2011-10-30 revision 33570"
full_version: "ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]"

Before:
real 0m7.793s
user 0m7.248s
sys 0m0.476s

After

real 0m7.309s
user 0m6.988s
sys 0m0.264s

Ran it 10 times:

for run in {1..10}
do
  script/rails runner User
done

Rails 3.1.3, bundler 1.0.21, development mode, RVM ruby1.9.2-p290
Before

real    3m27.369s
user    2m50.650s
sys 0m21.136s

After

real    3m18.477s
user    2m46.363s
sys 0m20.933s

Rails 3.1.3, bundler 1.0.21, development mode, RVM ruby1.9.3-p0
Before

real    1m50.195s
user    1m28.461s
sys 0m12.872s

After

real    1m46.806s
user    1m25.438s
sys 0m12.962s
$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]

development...

Before

$ time script/rails runner User

real    0m13.371s
user    0m10.275s
sys 0m1.728s

After

$ time script/rails runner User

real    0m11.144s
user    0m9.378s
sys 0m1.596s

Before:
real 0m14.468s
user 0m10.218s
sys 0m4.134s

After:
real 0m13.001s
user 0m8.859s
sys 0m4.082s

Rails 3.1.3, ruby 1.9.3-p0, Ubuntu 64.

Before
real 0m10.501s
user 0m9.930s
sys 0m0.510s

real 0m10.483s
user 0m9.970s
sys 0m0.480s

real 0m10.367s
user 0m9.930s
sys 0m0.420s

After
real 0m9.989s
user 0m9.520s
sys 0m0.440s

real 0m9.919s
user 0m9.470s
sys 0m0.420s

real 0m9.898s
user 0m9.440s
sys 0m0.440s

Version

ruby 1.9.3dev (2011-09-23 revision 33323) [x86_64-darwin11.2.0]

in Development

Before

real 0m9.325s 0m9.256s 0m9.335s
user 0m8.650s 0m8.597s 0m8.666s
sys 0m0.666s 0m0.648s 0m0.660s

After

real 0m8.853s 0m8.722s 0m8.861s
user 0m8.191s 0m8.072s 0m8.206s
sys 0m0.652s 0m0.639s 0m0.644s

Rails: 3.1.1 Ruby: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]

Before:

script/rails runner "Model"  7.65s user 0.94s system 41% cpu 20.672 total
script/rails runner "Model"  7.47s user 0.72s system 62% cpu 13.196 total
script/rails runner "Model"  7.52s user 0.71s system 62% cpu 13.246 total

After:

script/rails runner "Model"  7.19s user 0.69s system 61% cpu 12.871 total
script/rails runner "Model"  7.09s user 0.71s system 60% cpu 12.806 total
script/rails runner "Model"  7.11s user 0.70s system 60% cpu 12.804 total

Seems to run fine with the patch under both development and production environments.

Versions

Ruby 1.9.3-p0
Rails 3.1.3
MacBook Pro i7

Before

real    0m4.029s
user    0m3.675s
sys     0m0.348s

After

real    0m3.718s
user    0m3.363s
sys     0m0.347s

rails 3.1.3, ruby 1.9.2p290
in development I got

BEFORE:
real 0m14.467s
user 0m12.999s
sys 0m1.112s

AFTER:
real 0m14.047s
user 0m12.677s
sys 0m1.102s

Before:
script/rails runner "Expense" 7.26s user 1.05s system 91% cpu 9.072 total

After:
script/rails runner "Expense" 6.70s user 0.99s system 99% cpu 7.750 total

Ruby 1.9.2p136, Rails 3.0.10

Before:

    real    0m32.044s
    user    0m20.624s
    sys     0m2.281s

After:

    real    0m20.476s
    user    0m18.217s
    sys     0m1.981s

Ruby 1.9.2p290 [i686-linux], Rails 3.1.3

rails runner Blog

Before:

real    1m4.910s
user    1m1.544s
sys     0m1.520s

After:

real    1m2.696s
user    1m0.812s
sys     0m1.388s

Ruby:

ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin11.2.0], MBARI 0x6770, Ruby Enterprise Edition 2011.03

Rails

3.0.3

Run WITHOUT:

8.35s user 1.73s system 92% cpu 10.908 total
8.34s user 1.71s system 96% cpu 10.451 total
8.48s user 1.80s system 87% cpu 11.790 total

Run WITH:

7.60s user 1.66s system 96% cpu 9.610 total
7.48s user 1.68s system 95% cpu 9.604 total
7.50s user 1.69s system 94% cpu 9.776 total

ruby 1.9.2p290
Rails 3.1.1

Before
real 0m28.290s
user 0m24.812s
sys 0m2.765s

After
real 0m27.185s
user 0m24.077s
sys 0m2.730s

Rails 3.1.3, Ruby 1.9.3

Before:

real  5.888s
user  4.904s
sys   0.619s

After:

real  5.284s
user  4.720s
sys   0.560s

Ruby 1.9.2p136, Rails 3.0.10

Before:

real    0m36.151s
user    0m19.340s
sys     0m2.418s

After:

real    0m22.452s
user    0m18.401s
sys     0m1.996s

Development mode, REE, 3.0.10, script/rails runner User

Before:
real 0m14.498s
user 0m10.630s
sys 0m3.006s

After:
real 0m12.922s
user 0m9.591s
sys 0m2.893s

Ruby 1.9.3p0, Rails 3.1.3

Before:

real  14.01s
user  12.22s
sys   1.73s

After:

real  13.70s
user  11.73s
sys   1.78s

Before:

script/rails runner "Call"  8.48s user 1.05s system 96% cpu 9.900 total
script/rails runner "Call"  8.50s user 0.86s system 99% cpu 9.374 total
script/rails runner "Call"  8.41s user 0.83s system 99% cpu 9.254 total

After:

script/rails runner "Call"  7.85s user 0.81s system 99% cpu 8.663 total
script/rails runner "Call"  7.92s user 0.84s system 99% cpu 8.763 total
script/rails runner "Call"  7.67s user 0.77s system 99% cpu 8.449 total

I'll leave the patch in development mode and report back if I have any errors.

Ruby 1.9.3-p0, Rails 3.1.3

Before:

5.30s user 0.70s system 99% cpu 6.009 total
5.49s user 0.72s system 98% cpu 6.337 total
5.34s user 0.69s system 99% cpu 6.050 total

After:

4.95s user 0.72s system 99% cpu 5.686 total
4.96s user 0.71s system 99% cpu 5.685 total
5.04s user 0.71s system 99% cpu 5.789 total

Will be leaving the patch in today and I'll report any issues...

Ruby 1.9.2, Rails 3.1, openSUSE 12.1

Before


real 0m30.473s
user 0m29.551s
sys 0m0.771s


real 0m31.978s
user 0m30.984s
sys 0m0.892s


real 0m31.855s
user 0m30.922s
sys 0m0.823s



After


real 0m30.095s
user 0m29.201s
sys 0m0.810s


real 0m30.118s
user 0m29.303s
sys 0m0.731s


real 0m30.385s
user 0m29.494s
sys 0m0.805s


With JRuby 1.6.4

Before

λ  time script/rails runner Advisor

real    0m11.627s
user    0m23.053s
sys 0m0.967s

After

 λ  time script/rails runner Advisor

real    0m11.437s
user    0m23.101s
sys 0m0.987s

No much difference.

Sorry... ignore the error i reported... accidentally deleted the Bundler.require... doh

ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]
Rails 3.0.9

Before:
13,37s user 1,03s system 100% cpu 14,397 total
13,42s user 1,05s system 99% cpu 14,468 total
13,48s user 1,06s system 100% cpu 14,534 total

After:
13,21s user 1,07s system 99% cpu 14,394 total
13,37s user 1,06s system 99% cpu 14,433 total
13,34s user 1,06s system 100% cpu 14,391 total

No actual improvement noticed...

Rails 3.0.10
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.1.0]

Before:

 real    0m38.392s
 user    0m35.857s
 sys     0m2.511s

After:

 real    0m36.849s
 user    0m34.325s
 sys     0m2.443s 

Same app with ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.1.0]

Before:

 real    0m17.713s 
 user    0m14.797s
 sys     0m1.444s

After:

 real    0m15.556s
 user    0m14.086s
 sys     0m1.400s

Thanks everyone for your beloved benchmarks. This patch is now in Rails:

https://github.com/rails/rails/commit/885a599303585b796da7a0a1c3ccd0bc5c642134

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.