Starting Ruby app terribly slow due to requires from GEM_HOME -
i working on ruby application, running (very!) slow.. until now, i've tried couple of things , narrow down main issue: ruby trying it's requires in every single directory in $load_path.
basically observing, ruby looking through lot of files, trying see if it's requires exist on there. in case wont find them, go next directory in line. nice thing can see happening strace. there lot of output this:
open("/boa_proj_build/nsteen/.gem/gems/i18n-0.7.0/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/thread_safe-0.3.5/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/tzinfo-1.2.2/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/minitest-5.8.2/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/activesupport-4.2.4/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/climate_control-0.0.3/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/cocaine-0.5.7/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/boa_loggable-0.2.2/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/ruby_expect-1.6.0/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/cctools-3.0.1/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/git-1.2.9.1/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/naught-1.1.0/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/symbolizer-0.0.1/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/settingslogic-2.0.9/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/memoist-0.12.0/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/highline-1.7.8/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = -1 enoent (no such file or directory) open("/boa_proj_build/nsteen/.gem/gems/commander-4.3.5/lib/commander/help_formatters/base.rb", o_rdonly|o_cloexec) = 8
as can see, looking through directories find it's requirements.
tracing test application, filtering enoent error , counting occurances, displays following concerning output:
vdi9442:/boa_proj_build/nsteen/$ strace packager --version 2>&1 | grep enoent | wc -l 3454261
of-course, 3.5 million lot. , result in load time of 5 minutes (and half without strace), before output it's version number (default functionality commander gem).
i've removed entire gem home directory, , ran test again, , faster, can see looking through few gems (dependencies commander) again, 'just' several thousand occurances instead of 3.5 mil.
my gem env looks this:
- gem paths: - /boa_proj_build/nsteen/.gem - /home/nsteen/.gem/ruby/2.1.0 - /cadappl/ruby/2.1.1/ruby/lib/ruby/gems/2.1.0
it looks ruby walking through entire load path, satisfy dependencies. it's fine, getting rediculous. have clue what's going on? can't wanted/default behaviour suspect?
does have clue what's going on? , how can speed things up?
there quite few ways deal including gemrc files won't into. mention few of options have:
solution 1:
the other answer correct, wanted expand bit on topic since 1 people seem face. rvm can help. 1 particular feature made this, gemsets. have moved rbenv , have not looked back. rbenv lot less intrusive on environment rvm, both great. can use gemsets in both, rvm , rbenv, limit gems available app. can make app specific gemset too. gemset, app in 1 location , load gems has used. won't mix other apps gemsets. best thing automatic, need set once , auto switch when in directory. integrated rubymine , other ides/editors support ruby.
solution 2:
set gem_path
environment variable 1 of locations. overrides else @ least stop looking in multiple places.
solution 3(application loading optimization):
something might if using bundler, may want require special use gems when needed. example, if using curb gem in 1 module of app, add top of file loading module:
require 'curb'
and change gemfile line loading curb looks this:
gem 'curb', require: false
the less gems load when app starts faster load. auto require feature great, forgotten , taken granted. amazing difference make.
Comments
Post a Comment