Skip to content

Triggering Sprockets on Rails init #249

Closed
@toolmantim

Description

@toolmantim

Whilst trying to reduce our app boot time and memory I noticed Sprockets extension name parsing was being triggered on Rails app load.

$ ruby -rmemory_profiler -I. -e 'MemoryProfiler.report { require "config/environment" }.pretty_print'
Total allocated 2057572
Total retained 390073

allocated memory by gem
-----------------------------------
activesupport-4.2.1 x 60149050 (note: this is just because dependency require'ing gets reported under here)
sprockets-3.0.1 x 56626099
2.2.0/lib x 28229776
rubygems x 13925583

retained memory by gem
-----------------------------------
sprockets-3.0.1 x 14024689
2.2.0/lib x 7370079
activesupport-4.2.1 x 5285833

I added a raise inside Sprockets#extname_map that is causing all this, and the only place it's called during Rails init is from this part of railtie.rb:

      config.react.react_js = lambda {File.read(::Rails.application.assets.resolve('react.js'))}

Removing react-rails from the Gemfile brings things back to normal (extname is never called):

$ ruby -rmemory_profiler -I. -e 'MemoryProfiler.report { require "config/environment" }.pretty_print'
Total allocated 1614438
Total retained 257955

allocated memory by gem
-----------------------------------
activesupport-4.2.1 x 60005108
2.2.0/lib x 28192938
rubygems x 13108443
json-1.8.2 x 9075397
mime-types-2.4.3 x 7098487
erubis-2.7.0 x 5427205

So config.react.react_js is called from in the config.after_initialize like so:

          React::Renderer.setup!( cfg.react_js, cfg.components_js, cfg.replay_console,
                                {:size => cfg.max_renderers, :timeout => cfg.timeout})

Is there a way to avoid this call to ::Rails.application.assets.resolve('react.js') in cfg.react_js?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions