sstephenson/sprockets — pull request #460 by tiegz (2013-08-02 17:27:34+00:00)
Title: Possible optimization: caching AssetAttributes per Index instance

This caches the result of `Index#attributes_for(path)` by keeping a cache per asset (per index).

### why?

I recently realized that `AssetAttributes#content_type`, although memoized, gets recalculated multiple times for the same path because by default `Index#attributes_for(path)` creates a new `AssetAttributes` object every time you call it.

If we can keep a hash that maps a pathname to its `AssetAttributes`, we could avoid this lookup.

It also seemed logical that we should keep this cache in `Index` since it's supposed be a cached `Environment`.

### will it work?

I'm new to the sprockets codebase & unaware of any pitfalls with this patch. Could there be a change to an asset from the first time `attributes_for` is called with it until the next time it's called?

### boost?

In a Rails 3.2 app with a lot of sass/js/images/fonts files, this speeds up `assets:precompile` by 32%. I'm not sure how conventional my environment is though -- does anyone else want to benchmark `asset:precompile` with this patch too?