ActiveSupport::Autoload 学习

最近遇到一个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.

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中有很多类似这样的代码:

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,446评论 0 13
  • Awesome Ruby Toolbox Awesome A collection of awesome Ruby...
    debbbbie阅读 2,778评论 0 3
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,806评论 0 0
  • 父子关系的改善的感想 2018年10月份起,我们做了一个重大的决定,孩子他爸到...
    Faith_6eaa阅读 98评论 0 0
  • 2019年,想做一些自己喜欢的事情,实现一些自己的目标,也希望自己的人生能更加的多姿多彩。
    竹间5869阅读 176评论 0 2