-
tested with Ruby (1.8.7, 1.9.3, and 2.0.0), JRuby(1.8 and 1.9 mode), and Rubinius(1.8 and 1.9 mode) - see .travis.yml
##TL;DR
Precompute asynchronously a value when the file is loaded, not on the 1st call.
cattr_reader_preloaded :gem_names do
`gem list --no-version`
end
##Problem :
Some operations are very slow the 1st time you perform them, even when this takes place long after the program was started.
Memoization will only speed up the 2nd and later calls.
Example : this operation
def gems_names
@@_gems_names ||= `gem list --no-version`
end
is slow :
sleep 10
3.times do
start = Time.now
gems_names
puts "%.4f" % (Time.now - start)
end
1.6710 : 1st call = slow
0.0000 : 2nd call = fast (memoized)
0.0000 : 3rd call = fast (memoized)
##Solution :
This gem adds a cattr_reader_preloaded
method to Kernel
that starts precomputing - asynchronously the value as soon as the file (class or module) is loaded by Ruby
cattr_reader_preloaded :gem_names do
`gem list --no-version`
end
and the result is now
0.0000
0.0000
0.0000
Installation
Add this line to your application's Gemfile:
gem 'cattr_reader_preloaded'
And then execute:
$ bundle
Contributing
- Respect the code style and formatting.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request