Rails ActiveSupport::Autoload 学习

rubyu2 · 2015年04月06日 · 最后由 adamshen 回复于 2016年04月19日 · 8514 次阅读
本帖已被管理员设置为精华贴

最近遇到一个 eager_load 的问题,就搜索了相关的文章,又看了一些 Autoload 的源代码,感觉不错,分享下。以下内容多数引自原文,并加上部分补充,本人不准备在这里做翻译工作。

或许 eager_load_paths 是比 autoload_paths 更好的选择

在 Rails 的官方文档中提到,可以使用config.autoload_paths += %W(#{config.root}/extras)的方式加载目录。这种加载方式看上去十分美好,不过有一些小小的瑕疵。

Let’s say we have two files

# root/extras/foo.rb
class Foo
end

and

# root/app/models/blog.rb
class Blog < ActiveRecord::Base
end

Our configuration looks like this:

# root/config/application.rb
config.autoload_paths += %W( #{config.root}/extras )

Things are ok in development.Now, let’s check how it behaves in development.

defined?(Blog)
# => nil 
defined?(Foo)
# => nil 
Blog
# => Blog (call 'Blog.connection' to establish a connection) 
Foo
# => Foo 
defined?(Blog)
# => "constant" 
defined?(Foo)
# => "constant"

As you can see from this trivial example, at first nothing is loaded. Neither Blog, nor Foo is known. When we try to use them, rails autoloading jumps in. const_missing is handled, it looks for the classes in proper directories based on the convention and bang. app/models/blog.rb is loaded, Blog class is now known under Blog constant. Same goes for extras/foo.rb and Foo class.

即在开发环境下,Rails 会延迟加载。首先 Rails 在启动时会记下加载路径,当有找到未定义的 constant 时,会触发 ActiveSupport 的 const_missing,然后在 const_missing 中加载 constant。

But on the production, the situation is a little different…

defined?(Blog)
# => "constant"

defined?(Foo)

=> nil

Blog

=> Blog (call 'Blog.connection' to establish a connection)

Foo

=> Foo

defined?(Blog)

=> "constant"

defined?(Foo)

=> "constant"

> Why is that a problem? For the opposite reasons why eager loading is a good thing. When Foo is not eager loaded it means that:
> * when there is HTTP request hitting your app which needs to know about Foo to get finished, it will be served a bit slower. Not much for a one class, but still. Slower. It needs to find foo.rb in the directoriess and load this class.
> * All workers can’t share in memory the code where Foo is defined. The copy-on-write optimization won’t be used here.
>  
>If all that was for one class, that wouldn’t be much problem. But with some legacy rails applications I’ve seen them adding lot more directories to config.autoload_paths. And not a single class from those directories is eager loaded on production. That can hurt the performance of few initial requests after deploy that will need to dynamicaly load some of these classes. This can be especially painful when you practice continuous deployment. We don’t want our customers to be affected by our deploys.

> How can we fix it?
There is another, less known rails configuration called config.eager_load_paths that we can use to achieve our goals.
```ruby
config.eager_load_paths += %W( #{config.root}/extras )

How will that work on production? Let’s see.

defined?(Blog)
# => "constant" 
defined?(Foo)
# => "constant"

Not only is our class/constant Foo from extras/foo.rb autoloaded now, but it is also eager loaded in production mode. That fixed the problem.

简单的做法就是用eager_load_paths代替autoload_paths。Rails 是如何控制 Development 和 Production 环境下用不同的加载方式的呢?在配置环境里config.eager_load = false可以修改eager_load,Development 是 false,Production 是 true。

Autoloading is using eager loading paths as well

def _all_autoload_paths
  @_all_autoload_paths ||= (
    config.autoload_paths   + 
    config.eager_load_paths + 
    config.autoload_once_paths
  ).uniq
end

Unfortunately I’ve seen many people doing things like

config.autoload_paths += %W( #{config.root}/app/services )
config.autoload_paths += %W( #{config.root}/app/presenters )

It is completely unnecessary because app/* is already added there. You can see the default rails 4.1.7 paths configuration

def paths
  @paths ||= begin
    paths = Rails::Paths::Root.new(@root)

paths.add "app", eager_load: true, glob: "" paths.add "app/assets", glob: "" paths.add "app/controllers", eager_load: true paths.add "app/helpers", eager_load: true paths.add "app/models", eager_load: true paths.add "app/mailers", eager_load: true paths.add "app/views"

paths.add "app/controllers/concerns", eager_load: true paths.add "app/models/concerns", eager_load: true

paths.add "lib", load_path: true paths.add "lib/assets", glob: "" paths.add "lib/tasks", glob: "/.rake"

paths.add "config" paths.add "config/environments", glob: "#{Rails.env}.rb" paths.add "config/initializers", glob: "*/.rb" paths.add "config/locales", glob: "*.{rb,yml}" paths.add "config/routes.rb"

paths.add "db" paths.add "db/migrate" paths.add "db/seeds.rb"

paths.add "vendor", load_path: true paths.add "vendor/assets", glob: "*"

paths end end


#### Autoload
在Rails中有很多类似这样的代码:
```ruby
module ActiveSupport
  extend ActiveSupport::Autoload

  autoload :Concern
  autoload :Dependencies
  autoload :DescendantsTracker
  ...

在很多 gem 中也会用到 Autoload。比如simple_form

module SimpleForm
  extend ActiveSupport::Autoload

  autoload :Helpers
  autoload :Wrappers

  eager_autoload do
    autoload :Components
    autoload :ErrorNotification
    autoload :FormBuilder
    autoload :Inputs
  end

  def self.eager_load!
    super
    SimpleForm::Inputs.eager_load!
    SimpleForm::Components.eager_load!
  end
end

这些 autoload 和原生的 ruby 的 autoload 是一样的么?这个 eager_autoload 和 eager_load! 到底做了什么? 下面分析啊下Autoload的代码:

require "active_support/inflector/methods"

module ActiveSupport
  # Autoload and eager load conveniences for your library.
  #
  # This module allows you to define autoloads based on
  # Rails conventions (i.e. no need to define the path
  # it is automatically guessed based on the filename)
  # and also define a set of constants that needs to be
  # eager loaded:
  #
  #   module MyLib
  #     extend ActiveSupport::Autoload
  #
  #     autoload :Model
  #
  #     eager_autoload do
  #       autoload :Cache
  #     end
  #   end
  #
  # Then your library can be eager loaded by simply calling:
  #
  #   MyLib.eager_load!
  module Autoload
    def self.extended(base) # :nodoc:
      base.class_eval do
        @_autoloads = {}
        @_under_path = nil
        @_at_path = nil
        # 默认情况下_eager_autoload是false的
        @_eager_autoload = false
      end
    end

    def autoload(const_name, path = @_at_path)
      # 这里很显然Rails惯例大于配置的就在这里实现的,在调用ruby原生的autoload时Rails会帮忙配置path。
      unless path
        full = [name, @_under_path, const_name.to_s].compact.join("::")
        path = Inflector.underscore(full)
      end
      # 判断@_eager_autoload,true则保持到@_autoloads
      if @_eager_autoload
        @_autoloads[const_name] = path
      end

      super const_name, path
    end

    def autoload_under(path)
      @_under_path, old_path = path, @_under_path
      yield
    ensure
      @_under_path = old_path
    end

    def autoload_at(path)
      @_at_path, old_path = path, @_at_path
      yield
    ensure
      @_at_path = old_path
    end
    # 将@_eager_autoload置为true,然后yield,最后再将@_eager_autoload恢复
    def eager_autoload
      old_eager, @_eager_autoload = @_eager_autoload, true
      yield
    ensure
      @_eager_autoload = old_eager
    end
    # 通过这里eager_load!方法将@_autoloads保存的path全部require。autoload在不同情况下就是调用了require或者autoload。
    def eager_load!
      @_autoloads.each_value { |file| require file }
    end

    def autoloads
      @_autoloads
    end
  end
end
def autoload(const_name, path = @_at_path)
  # 这里很显然Rails惯例大于配置的就在这里实现的,在调用ruby原生的autoload时Rails会帮忙配置path。
  unless path
    full = [name, @_under_path, const_name.to_s].compact.join("::")
    path = Inflector.underscore(full)
  end
  # 判断@_eager_autoload,true则保持到@_autoloads
  if @_eager_autoload
    @_autoloads[const_name] = path
  end

  super const_name, path
end

  # 将@_eager_autoload置为true,然后yield,最后再将@_eager_autoload恢复
def eager_autoload
  old_eager, @_eager_autoload = @_eager_autoload, true
  yield
ensure
  @_eager_autoload = old_eager
end
# 通过这里eager_load!方法将@_autoloads保存的path全部require。ActiveSupport的autoload在不同情况下就是调用了require或者原生ruby的autoload。
def eager_load!
  @_autoloads.each_value { |file| require file }
end

对于一些 lib 这样做的好处就是可以利用 rails 的 eager_load 在 production 环境下提前加载,而不是在 request 请求时加载。

待续。

参考链接: http://blog.arkency.com/2014/11/dont-forget-about-eager-load-when-extending-autoload/ http://blog.plataformatec.com.br/2012/08/eager-loading-for-greater-good/ http://www.dbose.in/blog/2013/06/09/ruby-notes-autoload/ http://stackoverflow.com/questions/1457241/how-are-require-require-dependency-and-constants-reloading-related-in-rails

哈哈 今天也在看这个

嗯,以前对这个很迷糊,学习一下

:plus1: , 有点困惑于这句话:‘autoload 在不同情况下就是调用了 require 或者 autoload。’,并未在代码中看到不同情况的判断条件,为何这句注释写在eager_load!方法上? 我想可能这句话中后一个 autoload 指的可能是 Ruby 原生态的方法:

#autoload(module, filename) → nil 
#Registers filename to be loaded (using Kernel::require) the first time that module (which may be a String or a symbol) is accessed.

autoload(:MyModule, "/usr/local/lib/modules/my_module.rb")

#3 楼 @huopo125 是的。

autoload 在不同情况下就是调用了 require 或者 autoload。

修改为:

ActiveSupport 的 autoload 在不同情况下就是调用了 require 或者原生 ruby 的 autoload。

其实应该也不是require,而是require_dependency或者require。在不同情况下的处理也是不同的。

Rails 实现自动加载,用到了 const_missing机制。和 Ruby 的load没有关系。

Ruby 中的autoload不是线程安全的。

eager load 路径里的文件,在加载框架初始化项目后就会加载。而不是处理请求的时候,才加载类,这样避免了线程安全问题。

#5 楼 @zhangyuan Ruby 中的autoload在 2.0 以后是线程安全的。

#7 楼 @gelihai1991 这个帖子是 11 年的,autoload 多线程的问题已经在 2.0 解决了。 https://bugs.ruby-lang.org/issues/921

据我初步观察,现在大多数复杂一点的 gem 依然使用 autoload 来延迟加载代码的,似乎大家觉得不加载一些可能不会用到的代码要比提前加载整个 gem 的代码开销少。

huacnlee 请教一下关于 Rails 加载的问题 提及了此话题。 07月11日 15:39
chenyunli [该话题已被删除] 提及了此话题。 10月18日 16:36
需要 登录 后方可回复, 如果你还没有账号请 注册新账号