From: "Ara.T.Howard" Date: 2005-09-17T00:09:23+09:00 Subject: Re: bug in require? On Fri, 16 Sep 2005, Han Holl wrote: > On 9/16/05, Yukihiro Matsumoto wrote: >> >> Hi, >> >> In message "Re: bug in require?" >> on Fri, 16 Sep 2005 20:56:38 +0900, Han Holl writes: >> >> |Yes, that's answering where it happens. >> |Remains the question why. >> >> It was the easiest way to check if a file is loadable (i.e. a file >> exists and has proper permissions). Maybe it's possible to replace it >> with access(3). >> >> matz. >> >> Ara's question was what the difference was between execution from the > commandline and from a script. Only now I see that the difference is that > from the commandline he requires "digest/md5.so", and from the file > 'digest/md5', without the extension. Which means that you get a > considerable perfomance penalty by omitting the extension. In a > gem-installed rails environment the seachpath can easily be 25 directories. > Multiple requires withou extension can be costly there. right. try this fix for your rails app: harp:~ > strace ruby -e'4.times{ require "digest/md5" }' 2>&1 |egrep 'open.*md5' open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 harp:~ > strace ruby -r ./require.rb -e'4.times{ require "digest/md5" }' 2>&1 |egrep 'open.*md5' open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 open("/usr/lib/ruby/1.6/i386-linux-gnu/digest/md5.so", O_RDONLY) = 3 harp:~ > cat ./require.rb require 'rbconfig.rb' module Kernel alias_method '__require__', 'require' def require lib if((ext = lib[ %r/\.[^\.]+$/ ])) __require__ lib else dlext, dlext2 = ::Config::CONFIG['DLEXT'], ::Config::CONFIG['DLEXT2'] __require__ "#{ lib }.#{ dlext }" rescue __require__ "#{ lib }.#{ dlext2 }" rescue __require__ "#{ lib }.rb" end end end cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna ===============================================================================