forgecrafted / ruby-stylus

(DEPRECATED) Ruby interface for Stylus
MIT License
175 stars 39 forks source link

assets_hash is slow to iterate over entire directory tree (ruby-stylus + sprockets + bower) #76

Open ajb opened 10 years ago

ajb commented 10 years ago

After starting to using rails-assets to manage dependencies, we noticed that asset compilation was taking 500% longer than normal -- up to 5 minutes, sometimes. After two days of digging, I found that the assets_hash method was calling Sprockets' each_logical_path method, which iterates over every directory, even the ones in our new vendored bower assets. So for stuff like jquery-ui, with tons of subdirectories and images, ruby-stylus was building up an assets_hash that included all of them.

I'm not sure there's a fix for this in this library's current implementation, but I would definitely hope it gets considered a bug. I'd imagine that when the assets_hash method was implemented, this edge case was simply not thought of.

ajb commented 10 years ago

And if anyone cares, here's how we've cut our compile time from 3+ minutes to 50 seconds:

require 'stylus'

module Stylus
  module Rails
    class StylusTemplate

      ASSETS_THAT_WE_ACTUALLY_GIVE_A_DAMN_ABOUT = {
        'logo.png' => '/assets/logo.png'
      }

      def assets_hash(scope)
        {
          url: ASSETS_THAT_WE_ACTUALLY_GIVE_A_DAMN_ABOUT.map do |k, v|
            "('#{k}' url(\"#{v}\"))"
          end.join(' '),

          path: ASSETS_THAT_WE_ACTUALLY_GIVE_A_DAMN_ABOUT.map do |k, v|
            "('#{k}' \"#{v}\")"
          end.join(' ')
        }
      end

    end
  end
end