Skip to content

Instantly share code, notes, and snippets.

@josevalim
Created December 6, 2011 11:54
Show Gist options
  • Save josevalim/1437939 to your computer and use it in GitHub Desktop.
Save josevalim/1437939 to your computer and use it in GitHub Desktop.
Potentially faster boot
# 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
@kulesa
Copy link

kulesa commented Dec 6, 2011

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

@fpauser
Copy link

fpauser commented Dec 6, 2011

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

@ileitch
Copy link

ileitch commented Dec 6, 2011

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

Copy link

ghost commented Dec 6, 2011

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

@pellegrino
Copy link

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

@hemju
Copy link

hemju commented Dec 6, 2011

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

@jtadeulopes
Copy link

$ 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

@tauil
Copy link

tauil commented Dec 6, 2011

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

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

@PotHix
Copy link

PotHix commented Dec 6, 2011

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

@argami
Copy link

argami commented Dec 6, 2011

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

@mjtko
Copy link

mjtko commented Dec 6, 2011

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.

@jschwindt
Copy link

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

@redrick
Copy link

redrick commented Dec 6, 2011

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

@joemsak
Copy link

joemsak commented Dec 6, 2011

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

@RaZer
Copy link

RaZer commented Dec 6, 2011

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

@rainerborene
Copy link

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

@e1senh0rn
Copy link

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

@hinke
Copy link

hinke commented Dec 6, 2011

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

@bryckbost
Copy link

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

@RaZer
Copy link

RaZer commented Dec 6, 2011

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

@tiegz
Copy link

tiegz commented Dec 6, 2011

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

@itspriddle
Copy link

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

@lunks
Copy link

lunks commented Dec 6, 2011

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.

@mbulat
Copy link

mbulat commented Dec 6, 2011

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...

@rtdp
Copy link

rtdp commented Dec 6, 2011

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


@anildigital
Copy link

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.

@mbulat
Copy link

mbulat commented Dec 6, 2011

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

@luizcosta
Copy link

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...

@johnmcaliley
Copy link

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

@josevalim
Copy link
Author

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

rails/rails@885a599

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment