From: Yehuda Katz Date: 2011-11-14T07:52:38+09:00 Subject: [ruby-core:40998] Re: [Backport93 - Backport #5621] Please backport thread-safe autoloading patch --20cf300e4bf3338ac604b1a59c9c Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable We don't use autoload for one simple reason: we support nested autoloads and don't know ahead of time whether an intermediate module is a module or class. Consider the following file structure: |-app | |-models | | |-admin | | | |-user.rb At various times, I have tried to have Rails' internal structure operate by setting up autoloads ahead of time and letting Ruby do the rest. Unfortunately, in this case, we would have to explicitly define Admin, but we have no way to know whether Admin is a module or class. If we define it as a module, for instance, and user.rb looks like this: class Admin class User < ActiveRecord::Base =85 end end we get: "Admin is not a class". As a result, cannot set everything up ahead of time and have to let the first file that is autoloaded define the intermediate namespaces. --- ASIDE --- While we're on the topic, http://redmine.ruby-lang.org/issues/2740 is probably the major remaining issue with Rails' autoload solution. To summarize: module Foo def self.const_missing(id) return lookup(id) if can_lookup?(id) raise NoConstantError end module Bar def self.const_missing(id) return lookup(id) if can_lookup?(id) raise NoConstantError end end end The reason this is needed is that this: module Foo module Bar Baz end end has different semantics than: module Foo::Bar Baz end Rails can't tell the difference between these two cases, and therefore guesses that it's *probably* the first case. But consider this situation: # foo/array.rb module Foo class Array end end module Foo::Bar Array end Because we can't tell from the const_missing call that the nesting is [Foo::Bar], we assume it's [Foo::Bar, Foo] and load in foo/array.rb even though that is not the semantically correct behavior. In 99% of cases, this does not cause any problems, but when it fails (for this and other reasons), it causes pretty extreme confusion. Yehuda Katz (ph) 718.877.1325 On Sun, Nov 13, 2011 at 8:00 AM, Aaron Patterson wrote: > On Sun, Nov 13, 2011 at 10:35:42AM +0900, Hiroshi Nakamura wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > > Hash: SHA1 > > > > (2011/11/13 1:58), Mike Perham wrote: > > > My use case: I want to use Rails in thread-safe mode in production > > > so I don't have to fork 20 Ruby processes to handle 20 concurrent > > > requests. When developing, Rails auto loads classes on every > > > request. This auto loading is critical to Rails' rapid development > > > cycle and there's no way I'm going to disable it. Unfortunately > > > the current autoload behavior makes the development environment > > > incompatible with thread-safe mode. > > > > I should read ActiveSupport source code first but please allow me to > > post lazy question. Does Rails development mode use autoload? It is > > doing constant lookup by itself I guess, but I could be wrong, of cours= e. > > ActiveSupport doesn't use autoload for loading missing constants, but > supporting libraries definitely use autoloading things. It's quite > possible that files loaded using the missing constant hooks contain > autoload directives. :( > > -- > Aaron Patterson > http://tenderlovemaking.com/ > --20cf300e4bf3338ac604b1a59c9c Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable
We don't use autoload for one simple reason: we support nested aut= oloads and don't know ahead of time whether an intermediate module is a= module or class. Consider the following file structure:

|-app
| |-models
| | |-admin
| | | |-use= r.rb

At various times, I have tried to have Rails&= #39; internal structure operate by setting up autoloads ahead of time and l= etting Ruby do the rest. Unfortunately, in this case, we would have to expl= icitly define Admin, but we have no way to know whether Admin is a module o= r class. If we define it as a module, for instance, and user.rb looks like = this:

class Admin
=A0 class User < ActiveRecord:= :Base
=A0 =A0 =85
=A0 end
end

<= /div>
we get: "Admin is not a class". As a result, cannot set= everything up ahead of time and have to let the first file that is autoloa= ded define the intermediate namespaces.

--- ASIDE ---

While we're = on the topic,=A0http:/= /redmine.ruby-lang.org/issues/2740=A0is probably the major remaining is= sue with Rails' autoload solution. To summarize:

module Foo
=A0 def self.const_missing(id)
=A0 =A0 return lookup(id) if can_lookup?(id)
=A0 =A0 raise= NoConstantError
=A0 end

=A0 module Bar<= /div>
=A0 =A0 def self.const_missing(id)
=A0 =A0 =A0 return lookup= (id) if can_lookup?(id)
=A0 =A0 =A0 raise NoConstantError
=A0 =A0 end
=A0 end
end

The reason this is needed is that this:

module Foo
=A0 module Bar
=A0 =A0 B= az
=A0 end
end

has different s= emantics than:

module Foo::Bar
=A0 Baz
end

Rails can't tell the difference between th= ese two cases, and therefore guesses that it's *probably* the first cas= e. But consider this situation:

# foo/array.rb
module Foo
=A0 class Array
=A0 end
end

module Foo::Bar
=A0 Array
end

Because we can't tell from the const_missing ca= ll that the nesting is [Foo::Bar], we assume it's [Foo::Bar, Foo] and l= oad in foo/array.rb even though that is not the semantically correct behavi= or. In 99% of cases, this does not cause any problems, but when it fails (f= or this and other reasons), it causes pretty extreme confusion.

Yehuda Katz
(ph) 718.877.1325


On Sun, Nov 13, 2011 at 8:00 AM, Aaron P= atterson <= tenderlove@ruby-lang.org> wrote:
On Sun, Nov 13, 2011 at 10:35:42AM +0900, Hiroshi Nakamur= a wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> (2011/11/13 1:58), Mike Perham wrote:
> > My use case: I want to use Rails in thread-safe mode in productio= n
> > so I don't have to fork 20 Ruby processes to handle 20 concur= rent
> > requests. =A0When developing, Rails auto loads classes on every > > request. =A0This auto loading is critical to Rails' rapid dev= elopment
> > cycle and there's no way I'm going to disable it. =A0Unfo= rtunately
> > the current autoload behavior makes the development environment > > incompatible with thread-safe mode.
>
> I should read ActiveSupport source code first but please allow me to > post lazy question. =A0Does Rails development mode use autoload? =A0It= is
> doing constant lookup by itself I guess, but I could be wrong, of cour= se.

ActiveSupport doesn't use autoload for loading missing constants,= but
supporting libraries definitely use autoloading things. =A0It's quite possible that files loaded using the missing constant hooks contain
autoload directives. =A0:(

--
Aaron Patterson
http://tenderlov= emaking.com/

--20cf300e4bf3338ac604b1a59c9c--