Upgrade to Pro — share decks privately, control downloads, hide ads and more …

dive to Ruby on Rails module autoloading

teitei_tk
October 29, 2018

dive to Ruby on Rails module autoloading

teitei_tk

October 29, 2018
Tweet

More Decks by teitei_tk

Other Decks in Programming

Transcript

  1. Dive to Ruby on Rails
    Autoloading
    @teitei_tk

    Meguro.rb#20

    View full-size slide

  2. ͓લ͸୭ͳΜͩɻ
    • @teitei_tkͱ໊৐͍ͬͯ·͢ɻ

    • ΞΠίϯ͸ӈͰ΍ͬͯ·͢ɻ

    • झຯ͸ਓੜͱRailsͱ
    TypeScriptͰ͢ɻ

    • freeeͱ͍͏ձࣾͰಇ͍͍ͯ·
    ͢ɻ

    View full-size slide

  3. ࠓ೔࿩͢͜ͱ

    View full-size slide

  4. Railsͷautoload_pathsͱ
    eager_load_pathsͷҧ͍ʹ͍ͭ
    ͯ

    View full-size slide

  5. autoload_pathsͱ͸
    • ޠฐΛڪΕͣʹ͍͏ͱɺRails্Ͱ
    module΍ఆ਺Λ͍͍ײ͡ʹࣗಈͰಡ
    ΈࠐΜͰ͘ΕΔ࢓૊ΈͰ͢ɻ

    View full-size slide

  6. eg. Ruby on Rails
    class Hoge < ApplicationRecord
    end

    View full-size slide

  7. eg. PureRubyͷ৔߹
    require 'application_record'
    class Hoge < ApplicationRecord
    end

    View full-size slide

  8. • Pure RubyͰ͸ application_record.rb ΛrequireʹͯಡΈࠐ
    Ήඞཁ͕͋Δɻ

    • RailsͰ͸ಛఆͷσΟϨΫτϦ഑ԼͷϑΝΠϧ͸ࣗલͰಡ
    Έࠐ·ͣͱ΋͍͍ײ͡ʹࣗಈͰಡΈࠐΜͰ͘ΕΔ࢓૊Έ͕
    ͋Δ

    • ͦΕ͕autoload_pathsͰ͢ɻ

    View full-size slide

  9. ಛఆͷσΟϨΫτϦ is
    teitei.tk >> !(master) ~/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading
    $ bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'
    Running via Spring preloader in process 39294
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/assets
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/channels
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/controllers
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/controllers/concerns
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/helpers
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/jobs
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/mailers
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/models
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/models/concerns
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/
    activestorage-5.2.1/app/assets
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/
    activestorage-5.2.1/app/controllers
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/
    activestorage-5.2.1/app/controllers/concerns
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/
    activestorage-5.2.1/app/javascript
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/
    activestorage-5.2.1/app/jobs
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/
    activestorage-5.2.1/app/models
    /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/test/mailers/previews

    View full-size slide

  10. • puts ActiveSupport::Dependencies.autoload_paths Ͱ࣮
    ࡍʹautoload_paths͕దԠ͞ΕΔ஋ΛݟΔ͜ͱ͕Ͱ͖·
    ͢ɻ

    • config/application.rbʹͯconfig.autoload_pathsʹͯ௥Ճ
    Λ͢Δͱɺ൓ө͞Ε·͢ɻ

    View full-size slide

  11. • ৄ͘͠͸ https://railsguides.jp/
    autoloading_and_reloading_constants.html#autoload-
    paths

    View full-size slide

  12. Rails5͔Βͷڍಈͷมߋ
    • Rails5͔Βautoload_pathsʹ௥Ճͨ͠ϑΝΠϧ͸
    RAILS_ENV=production Ͱ͸ಡΈࠐ·ͳ͍Α͏ʹͳͬͯ
    ·͢ɻ
    • https://edgeguides.rubyonrails.org/
    upgrading_ruby_on_rails.html#autoloading-is-disabled-
    after-booting-in-the-production-environment

    • ୅ΘΓʹ࢖͓͏ͱݴΘΕ͍ͯΔͷ͕eager_load_pathsͰ
    ͢ɻ

    View full-size slide

  13. eager_load_pathsͱ͸
    • config.eager_load_pathsʹ௥Ճ͞Ε͍ͯΔσΟϨΫτϦͷ
    ϑΝΠϧΛࣄલʹҰׅͰಡΈࠐΉ࢓૊ΈͰ͢ɻ

    View full-size slide

  14. Rails::Engine.eager_load!
    # Eager load the application by loading all ruby
    # files inside eager_load paths.
    def eager_load!
    config.eager_load_paths.each do |load_path|
    matcher = /\A#{Regexp.escape(load_path.to_s)}\/(.*)\.rb\Z/
    Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
    require_dependency file.sub(matcher, '\1')
    end
    end
    end

    View full-size slide

  15. • config/environment.rbʹͯRails.application.initialize!Λಡ
    ΜͰ͍Δͱࢥ͍·͢ɻ͔ͦ͜ΒॳظԽͷྲྀΕΛಡΈऔΔ͜
    ͱ͕Ͱ͖·͢ɻ

    • Rails::Application.initialize!->
    Rails::Initializable.run_initializers ->
    Rails::Application.initializers ->
    Rails::Application::Finisher.initializers_for ->

    Rails::Engine.eager_load!

    View full-size slide

  16. ᷿ͰΑ͘ݴΘΕΔࣄ
    • Rails5Ͱ͸autoload_pathsͰ͸ͳ͘ɺeager_load_pathsʹ
    ม͑Ε͹ok

    View full-size slide

  17. ͳ͔ͥ
    • ౴͑͸Rails::Engine.initializer set_load_path ʹ͋Γ·͢ɻ
    # Add configured load paths to Ruby's load path, and remove
    duplicate entries.
    initializer :set_load_path, before: :bootstrap_hook do
    _all_load_paths.reverse_each do |path|
    $LOAD_PATH.unshift(path) if File.directory?(path)
    end
    $LOAD_PATH.uniq!
    end

    View full-size slide

  18. • Rails::Initializable Moduleʹఆٛ͞Ε͍ͯΔ
    $LOAD_PATHSʹunshiftͰ௥Ճ͍ͯ͠·͢ɻ

    View full-size slide

  19. _all_load_pathsͱ͸
    • Rails::Initializable Moduleʹఆٛ͞Ε͍ͯΔ
    $LOAD_PATHSʹunshiftͰ௥Ճ͍ͯ͠·͢ɻ

    • _all_load_pathsͷ࣮ଶ͸config.autoload_paths +
    config.eager_load_paths + config.autoload_once_pathsΛ
    ߹Θͤͨ෺Ͱͨ͠ɻ

    View full-size slide

  20. • RAILS_ENV=production Ͱ͸ eager_load_pathsͷ࢓૊Έ
    Λར༻͠ɺRAILS_ENV != production Ҏ֎Ͱ͸
    autoload_pathsͷ࢓૊ΈΛར༻͍ͯ͠Δɻ

    View full-size slide

  21. • ৄ͘͠͸Blogʹ·ͱΊ·ͨ͠ɻ

    • https://note.mu/teitei_tk/n/ne5ae711719b1

    View full-size slide