rstacruz / sinatra-assetpack

Package your assets transparently in Sinatra.
http://ricostacruz.com/sinatra-assetpack/
MIT License
542 stars 96 forks source link

Cached files so they are only loaded once #109

Closed lukesmith closed 11 years ago

lukesmith commented 11 years ago

files is an expensive method and is called a number of times. Added caching of the files result and reloads them in certain conditions

Before:

MethodProfiler results for: Sinatra::AssetPack::Package
+---------------------+------------+-------------+--------------+-------------+-------------+
| Method              | Min Time   | Max Time    | Average Time | Total Time  | Total Calls |
+---------------------+------------+-------------+--------------+-------------+-------------+
| #to_production_html | 643.064 ms | 2077.208 ms | 1247.375 ms  | 4989.499 ms | 4           |
| #production_path    | 641.064 ms | 2076.208 ms | 1246.374 ms  | 4985.498 ms | 4           |
| #files              | 640.064 ms | 2071.207 ms | 1243.624 ms  | 4974.497 ms | 4           |
| #paths_and_files    | 640.064 ms | 2071.207 ms | 1243.624 ms  | 4974.497 ms | 4           |
| #link_tag           | 0.000 ms   | 1.000 ms    | 0.500 ms     | 2.000 ms    | 4           |
| #add_path_prefix    | 0.000 ms   | 0.000 ms    | 0.000 ms     | 0.000 ms    | 4           |
| #css?               | 0.000 ms   | 0.000 ms    | 0.000 ms     | 0.000 ms    | 2           |
| #js?                | 0.000 ms   | 0.000 ms    | 0.000 ms     | 0.000 ms    | 4           |
+---------------------+------------+-------------+--------------+-------------+-------------+
MethodProfiler results for: Sinatra::AssetPack::Options
+-----------------+------------+-------------+--------------+-------------+-------------+
| Method          | Min Time   | Max Time    | Average Time | Total Time  | Total Calls |
+-----------------+------------+-------------+--------------+-------------+-------------+
| #glob           | 640.064 ms | 2070.207 ms | 1243.374 ms  | 4973.497 ms | 4           |
| #files          | 303.030 ms | 396.039 ms  | 331.433 ms   | 4971.497 ms | 15          |
| #ignored?       | 0.000 ms   | 1.000 ms    | 0.067 ms     | 1.000 ms    | 15          |
| #to_uri         | 0.000 ms   | 20.002 ms   | 0.018 ms     | 719.071 ms  | 39780       |
| #packages       | 0.000 ms   | 0.000 ms    | 0.000 ms     | 0.000 ms    | 4           |
| #served         | 0.000 ms   | 0.000 ms    | 0.000 ms     | 0.000 ms    | 4           |
| #app            | 0.000 ms   | 0.000 ms    | 0.000 ms     | 0.000 ms    | 8           |
| #local_file_for | 0.000 ms   | 0.000 ms    | 0.000 ms     | 0.000 ms    | 4           |
| #asset_hosts    | 0.000 ms   | 0.000 ms    | 0.000 ms     | 0.000 ms    | 8           |
+-----------------+------------+-------------+--------------+-------------+-------------+
MethodProfiler results for: Sinatra::AssetPack::BusterHelpers
+--------------------+----------+----------+--------------+------------+-------------+
| Method             | Min Time | Max Time | Average Time | Total Time | Total Calls |
+--------------------+----------+----------+--------------+------------+-------------+
| #cache_buster_hash | 0.000 ms | 5.001 ms | 1.250 ms     | 10.001 ms  | 8           |
| #add_cache_buster  | 0.000 ms | 5.001 ms | 1.250 ms     | 10.001 ms  | 8           |
+--------------------+----------+----------+--------------+------------+-------------+

After:

MethodProfiler results for: Sinatra::AssetPack::Package
+---------------------+----------+------------+--------------+------------+-------------+
| Method              | Min Time | Max Time   | Average Time | Total Time | Total Calls |
+---------------------+----------+------------+--------------+------------+-------------+
| #to_production_html | 4.000 ms | 369.037 ms | 102.010 ms   | 408.041 ms | 4           |
| #production_path    | 3.000 ms | 368.037 ms | 101.510 ms   | 406.041 ms | 4           |
| #files              | 0.000 ms | 362.037 ms | 91.009 ms    | 364.038 ms | 4           |
| #paths_and_files    | 0.000 ms | 362.037 ms | 91.009 ms    | 364.038 ms | 4           |
| #link_tag           | 0.000 ms | 1.000 ms   | 0.250 ms     | 1.000 ms   | 4           |
| #add_path_prefix    | 0.000 ms | 0.000 ms   | 0.000 ms     | 0.000 ms   | 4           |
| #css?               | 0.000 ms | 0.000 ms   | 0.000 ms     | 0.000 ms   | 2           |
| #js?                | 0.000 ms | 0.000 ms   | 0.000 ms     | 0.000 ms   | 4           |
+---------------------+----------+------------+--------------+------------+-------------+
MethodProfiler results for: Sinatra::AssetPack::Options
+-----------------+----------+------------+--------------+------------+-------------+
| Method          | Min Time | Max Time   | Average Time | Total Time | Total Calls |
+-----------------+----------+------------+--------------+------------+-------------+
| #glob           | 0.000 ms | 362.037 ms | 90.509 ms    | 362.037 ms | 4           |
| #files          | 0.000 ms | 361.037 ms | 24.069 ms    | 361.037 ms | 15          |
| #local_file_for | 0.000 ms | 1.000 ms   | 0.250 ms     | 1.000 ms   | 4           |
| #to_uri         | 0.000 ms | 1.001 ms   | 0.018 ms     | 47.003 ms  | 2652        |
| #ignored?       | 0.000 ms | 0.000 ms   | 0.000 ms     | 0.000 ms   | 15          |
| #served         | 0.000 ms | 0.000 ms   | 0.000 ms     | 0.000 ms   | 4           |
| #app            | 0.000 ms | 0.000 ms   | 0.000 ms     | 0.000 ms   | 8           |
| #packages       | 0.000 ms | 0.000 ms   | 0.000 ms     | 0.000 ms   | 4           |
| #asset_hosts    | 0.000 ms | 0.000 ms   | 0.000 ms     | 0.000 ms   | 8           |
+-----------------+----------+------------+--------------+------------+-------------+
MethodProfiler results for: Sinatra::AssetPack::BusterHelpers
+--------------------+----------+-----------+--------------+------------+-------------+
| Method             | Min Time | Max Time  | Average Time | Total Time | Total Calls |
+--------------------+----------+-----------+--------------+------------+-------------+
| #add_cache_buster  | 0.000 ms | 30.003 ms | 5.125 ms     | 41.003 ms  | 8           |
| #cache_buster_hash | 0.000 ms | 29.003 ms | 4.875 ms     | 39.003 ms  | 8           |
+--------------------+----------+-----------+--------------+------------+-------------+

Helps towards solving #85

j15e commented 11 years ago

Awesome! Thanks, will merge into master but before releasing will have to add doc & verify this behaviour as I think if you add new files during development they won't be detected? Or is this change for production only?

j15e commented 11 years ago

Ah the default is to reload it, good. We could set is to false when in production mode.

j15e commented 11 years ago

Released in 0.2.4