cattr_reader_preloaded 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +81 -0
- data/Rakefile +10 -0
- data/cattr_reader_preloaded.gemspec +24 -0
- data/lib/cattr_reader_preloaded.rb +27 -0
- data/lib/cattr_reader_preloaded/version.rb +3 -0
- data/test/acceptance_test.rb +42 -0
- data/test/fixtures/class_with_two_values_preloading.rb +12 -0
- data/test/fixtures/class_with_value_preloading.rb +8 -0
- data/test/fixtures/class_without_value_preloading.rb +7 -0
- data/test/test_helper.rb +20 -0
- metadata +127 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- "1.8.7"
|
4
|
+
- "1.9.3"
|
5
|
+
- "2.0.0"
|
6
|
+
- jruby-18mode # JRuby in 1.8 mode
|
7
|
+
- jruby-19mode # JRuby in 1.9 mode
|
8
|
+
- jruby-head
|
9
|
+
- rbx-18mode
|
10
|
+
- rbx-19mode
|
11
|
+
|
12
|
+
# uncomment this line if your project needs to run something other than `rake`:
|
13
|
+
script: bundle exec rake
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alain Ravet
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/alainravet/cattr_reader_preloaded.png?branch=master)](https://travis-ci.org/alainravet/cattr_reader_preloaded) -
|
2
|
+
[![Code Climate](https://codeclimate.com/github/alainravet/cattr_reader_preloaded.png)](https://codeclimate.com/github/alainravet/cattr_reader_preloaded)
|
3
|
+
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](.travis.yml)
|
4
|
+
|
5
|
+
##TL;DR
|
6
|
+
Precompute asynchronously a value when the file is loaded, not on the 1st call.
|
7
|
+
```ruby
|
8
|
+
cattr_reader_preloaded :gem_names do
|
9
|
+
`gem list --no-version` # <<< SLOW
|
10
|
+
end
|
11
|
+
```
|
12
|
+
---------------
|
13
|
+
|
14
|
+
##Problem :
|
15
|
+
Some operations are very slow the 1st time you perform them, even when this takes place long after the program was started.
|
16
|
+
Memoization will only speed up the 2nd and later calls.
|
17
|
+
|
18
|
+
Example : this operation
|
19
|
+
```ruby
|
20
|
+
def gems_names
|
21
|
+
@@_gems_names ||= `gem list --no-version` # <<<
|
22
|
+
end
|
23
|
+
```
|
24
|
+
is slow :
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
sleep 10 # simulates the program running for a while.
|
28
|
+
|
29
|
+
3.times do
|
30
|
+
start = Time.now
|
31
|
+
gems_names # <<< the slow operation
|
32
|
+
puts "%.4f" % (Time.now - start)
|
33
|
+
end
|
34
|
+
```
|
35
|
+
```
|
36
|
+
1.6710 : 1st call = slow
|
37
|
+
0.0000 : 2nd call = fast (memoized)
|
38
|
+
0.0000 : 3rd call = fast (memoized)
|
39
|
+
```
|
40
|
+
|
41
|
+
##Solution :
|
42
|
+
|
43
|
+
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
|
44
|
+
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# replace
|
48
|
+
# def gems_names
|
49
|
+
# @@_gems_names ||= `gem list --no-version`
|
50
|
+
# end
|
51
|
+
# by
|
52
|
+
cattr_reader_preloaded :gem_names do
|
53
|
+
`gem list --no-version` # <<< SLOW
|
54
|
+
end
|
55
|
+
```
|
56
|
+
and the result is now
|
57
|
+
```
|
58
|
+
0.0000
|
59
|
+
0.0000
|
60
|
+
0.0000
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
## Installation
|
65
|
+
|
66
|
+
Add this line to your application's Gemfile:
|
67
|
+
|
68
|
+
gem 'cattr_reader_preloaded'
|
69
|
+
|
70
|
+
And then execute:
|
71
|
+
|
72
|
+
$ bundle
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
0. Respect the code style and formatting.
|
77
|
+
1. Fork it
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cattr_reader_preloaded/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cattr_reader_preloaded"
|
8
|
+
spec.version = CattrReaderPreloaded::VERSION
|
9
|
+
spec.authors = ["Alain Ravet"]
|
10
|
+
spec.email = ["alainravet@gmail.com"]
|
11
|
+
spec.description = 'asynchronously precompute and cache values when a class/module is loaded, not on the 1st call.'
|
12
|
+
spec.summary = 'asynchronously precompute and cache values when a class/module is loaded, not on the 1st call.'
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "minitest"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "cattr_reader_preloaded/version"
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
|
5
|
+
# Usage :
|
6
|
+
# cattr_reader_preloaded :gems_names do
|
7
|
+
# `gem list --no-version`
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
def cattr_reader_preloaded(name, &block)
|
11
|
+
raise "initialization block is missing" unless block_given?
|
12
|
+
cache = "@_cached_#{name}" .to_sym
|
13
|
+
preloader = "@_#{name}_preloader".to_sym
|
14
|
+
|
15
|
+
self.class.send :define_method, name, lambda {
|
16
|
+
instance_variable_get(preloader).join.value
|
17
|
+
}
|
18
|
+
|
19
|
+
instance_variable_set(preloader, Thread.new do
|
20
|
+
instance_variable_set cache, block.call
|
21
|
+
self.class.send :define_method, name, lambda {
|
22
|
+
instance_variable_get cache
|
23
|
+
}
|
24
|
+
instance_variable_get cache
|
25
|
+
end)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class TestCAttrReaderPreloaded < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def test_call_twice_a_method_that_does_not_preload
|
6
|
+
load_file('class_without_value_preloading.rb')
|
7
|
+
|
8
|
+
# no preloading => each call is slow and takes about the same time
|
9
|
+
assert_slower_than(0.1 ) { ClassWithoutValuePreloading.slow_op }
|
10
|
+
assert_slower_than(0.1 ) { ClassWithoutValuePreloading.slow_op }
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
def test_call_twice_a_method_that_preloads
|
16
|
+
load_file('class_with_value_preloading.rb')
|
17
|
+
|
18
|
+
# at this point, the value preloading is still running => must wait till it's finished
|
19
|
+
assert_slower_than(0.4 ) { @value = ClassWithValuePreloading.slow_op }
|
20
|
+
assert @value == '<value-returned>'
|
21
|
+
|
22
|
+
# preloading is finished => the second call is much faster
|
23
|
+
assert_faster_than(0.001) { @value = ClassWithValuePreloading.slow_op }
|
24
|
+
assert @value == '<value-returned>'
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
def test_call_in_one_class_two_method_that_preload
|
30
|
+
load_file('class_with_two_values_preloading.rb')
|
31
|
+
# at this point, the values preloading is still running => must wait till it's finished
|
32
|
+
|
33
|
+
assert_slower_than(0.4 ) { @value = ClassWithTwoValuesPreloading.slow_op }
|
34
|
+
assert @value == '<value-returned>'
|
35
|
+
|
36
|
+
# preloading of ALL values is finished
|
37
|
+
# => 1st call on the 2nd method is almost instantaneous (value is already computed)
|
38
|
+
assert_faster_than(0.001) { @value = ClassWithTwoValuesPreloading.slow_op_2 }
|
39
|
+
assert @value == '<value_2-returned>'
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
require File.dirname(__FILE__) + '/../lib/cattr_reader_preloaded'
|
3
|
+
|
4
|
+
def load_file(fixture)
|
5
|
+
load File.dirname(__FILE__) + "/../test/fixtures/#{fixture}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def assert_slower_than(minimum_time)
|
9
|
+
start = Time.now
|
10
|
+
yield
|
11
|
+
time_taken = (Time.now - start)
|
12
|
+
assert minimum_time < time_taken, "test was too fast (#{time_taken}); it should have take more than #{minimum_time}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def assert_faster_than(maximum_time)
|
16
|
+
start = Time.now
|
17
|
+
yield
|
18
|
+
time_taken = (Time.now - start)
|
19
|
+
assert time_taken < maximum_time, "test was too slow (#{time_taken}); it should have take less than #{maximum_time}"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cattr_reader_preloaded
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Alain Ravet
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2013-05-16 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 9
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 3
|
31
|
+
version: "1.3"
|
32
|
+
type: :development
|
33
|
+
name: bundler
|
34
|
+
version_requirements: *id001
|
35
|
+
prerelease: false
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
name: minitest
|
48
|
+
version_requirements: *id002
|
49
|
+
prerelease: false
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
type: :development
|
61
|
+
name: rake
|
62
|
+
version_requirements: *id003
|
63
|
+
prerelease: false
|
64
|
+
description: asynchronously precompute and cache values when a class/module is loaded, not on the 1st call.
|
65
|
+
email:
|
66
|
+
- alainravet@gmail.com
|
67
|
+
executables: []
|
68
|
+
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files: []
|
72
|
+
|
73
|
+
files:
|
74
|
+
- .gitignore
|
75
|
+
- .travis.yml
|
76
|
+
- Gemfile
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- cattr_reader_preloaded.gemspec
|
81
|
+
- lib/cattr_reader_preloaded.rb
|
82
|
+
- lib/cattr_reader_preloaded/version.rb
|
83
|
+
- test/acceptance_test.rb
|
84
|
+
- test/fixtures/class_with_two_values_preloading.rb
|
85
|
+
- test/fixtures/class_with_value_preloading.rb
|
86
|
+
- test/fixtures/class_without_value_preloading.rb
|
87
|
+
- test/test_helper.rb
|
88
|
+
has_rdoc: true
|
89
|
+
homepage: ""
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
requirements: []
|
116
|
+
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.6.2
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: asynchronously precompute and cache values when a class/module is loaded, not on the 1st call.
|
122
|
+
test_files:
|
123
|
+
- test/acceptance_test.rb
|
124
|
+
- test/fixtures/class_with_two_values_preloading.rb
|
125
|
+
- test/fixtures/class_with_value_preloading.rb
|
126
|
+
- test/fixtures/class_without_value_preloading.rb
|
127
|
+
- test/test_helper.rb
|