ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: fxn@hashref.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:91477] [Ruby trunk Feature#15592] mode that "autoload" behaves "require" immediately
Date: Thu, 07 Feb 2019 20:21:43 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-76736.20190207202141.7f6375d45bf7e55f@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15592.20190207065816@ruby-lang.org

Issue #15592 has been updated by fxn (Xavier Noria).


Many people do not realize that in order to eager load a project tree you need to autoload. Talking about the general case.

When Zeitwerk eager loads, it issues require calls, but the autoloads are set orderly still because otherwise it would not work generally.

In a Rails application, for example, you may have something like


``` ruby
module CountriesHelper
  ALL = Contry.all
end
```

or explicit namespaces as in

```ruby
class Hotel < ActiveRecord::Base
  include Hotel::Pricing
end
```

In the case of the proposal here, you could have (schematic):

```ruby
module M
  autoload :X
  autoload :Y
end
```

and then

```ruby
module M
  module X
    include Y
  end
end
```

You need to resolve a top-level M::Y before its autoload has been set.

The key observation is that _any_ constant can be referenced at the top-level of execution.

I think this proposal won't work in the general case.

----------------------------------------
Feature #15592: mode that "autoload" behaves "require" immediately
https://bugs.ruby-lang.org/issues/15592#change-76736

* Author: akr (Akira Tanaka)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
How about a feature to switch "autoload" behavior to "require" immediately.

autoload is a feature for lazy loading.

matz dislikes autoload as [Feature #5653].
I heard that he dislikes class (and other) definitions at arbitrary timing.
I agree that eager loading is safer than lazy loading.

However, lazy loading realize shorter loading time and
it makes development cycle shorter.
It is more important for larger applications as Eregon said in
https://bugs.ruby-lang.org/issues/5653#note-39 .
It is especially important when library loading causes I/O (code generation from DB schema).

These two, safety of eager loading and easier development of lazy loading, conflicts.
But if we can distinguish production mode and development mode,
we can enjoy both benefits.

So, I propose a feature to select autoload behavior from two modes:
- autoload behaves as lazy loading as now in development mode
- autoload behaves as eager loading (immediately invokes "require") in production mode.

There are several idea to switch the mode:

- $AUTOLOAD_MODE = :eager or :lazy
- RubyVM.autoload_mode = :eager or :lazy
- ObjectSpace.autoload_mode = :eager or lazy

I'm not sure there is a good enough one in above list, though.



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2019-02-07 20:21 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15592.20190207065816@ruby-lang.org>
2019-02-07  6:58 ` [ruby-core:91454] [Ruby trunk Feature#15592] mode that "autoload" behaves "require" immediately akr
2019-02-07 14:35 ` [ruby-core:91472] " merch-redmine
2019-02-07 18:13 ` [ruby-core:91475] " eregontp
2019-02-07 20:21 ` fxn [this message]
2019-02-07 20:45 ` [ruby-core:91478] " eregontp
2019-02-07 20:47 ` [ruby-core:91479] " eregontp
2019-02-07 20:49 ` [ruby-core:91480] " eregontp
2019-02-07 20:58 ` [ruby-core:91481] " merch-redmine
2019-02-08  0:38 ` [ruby-core:91485] " akr
2019-02-08  6:54 ` [ruby-core:91489] [Ruby trunk Feature#15592] mode where "autoload" behaves like an immediate "require" fxn
2019-02-09 13:36 ` [ruby-core:91501] " akr
2019-02-09 17:47 ` [ruby-core:91502] " fxn
2019-02-10 15:20 ` [ruby-core:91504] " Greg.mpls

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-76736.20190207202141.7f6375d45bf7e55f@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).