memonic 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00f453f84636c12710e40937eb247d502fa5fc29
4
- data.tar.gz: d5b6864989b1496753b02832360238144cb3a014
3
+ metadata.gz: 0fc00b69e78cf989af051b858a3c4089ecf7cc74
4
+ data.tar.gz: 1a485d6ea4594d13607dc2fec5782845faf09bb0
5
5
  SHA512:
6
- metadata.gz: ae5580d59462a61d65e2143fe7b7e1c38c2289c93febf54eb906b0eb7ae45c5bfc7ce16c3873be64ee8b42aa4c4545edb99d09167cba93500682603e48519d5e
7
- data.tar.gz: 05e101e9d9c3d31722ab298bc59c86f5d6187ca263374081992cd690d9882eeff3efdbbe75277ca927cebdd130bd685f3828e4495fa91a1ad206677d053f2076
6
+ metadata.gz: d18ad8bb33991680d7d7a69975387c321708073994eed5dd8a774a09e9c2e083f61c09287e843048204924f4d80ce8a5694d934357aeb4fb787bb64a100fabdd
7
+ data.tar.gz: bb4d536f13bfa0b0e9d287693ccd1e74675b0858db3e27fb698db4f0be266a3e255a55826cec9b42e071a2f499aecdc2df411e8a6b490bfc5af267bf4562e528
@@ -1,3 +1,20 @@
1
+ ## 1.0.3
2
+
3
+ - More performance improvements (details below).
4
+ - Calling `memoize(:value) { ... }` now defines a `__value___` method as well
5
+ as a `value` method.
6
+
7
+ ### Performance improvements
8
+
9
+ 1. The instance version of `memoize` now only checks with the variable is
10
+ defined if `instance_variable_get` returns a false value. This yields a
11
+ modest improvement in performance.
12
+ 2. The class version of `memoize` no longer piggy-backs on the instance
13
+ version. Instead it defines a `__value__` method using the provided block,
14
+ then defines a `value` method using `class_eval`. This yields a dramatic
15
+ improvement in performance, making the class version almost as fast as
16
+ doing it yourself.
17
+
1
18
  ## 1.0.2
2
19
 
3
20
  Performance improvement.
data/README.md CHANGED
@@ -44,6 +44,11 @@ you would want to use it, but if you do, here's how:
44
44
 
45
45
  Note that the '@' prefix **is** necessary.
46
46
 
47
+ ## Notes
48
+
49
+ Calling `memoize(:value) { ... }` on the class defines a `__value__` method
50
+ as well as `value`.
51
+
47
52
  ## Background
48
53
 
49
54
  Memoization is a common optimization technique in which the result of a
@@ -8,17 +8,29 @@ module Memonic
8
8
  private
9
9
 
10
10
  def memoize(variable, &block)
11
- if instance_variable_defined?(variable)
12
- instance_variable_get(variable)
13
- else
14
- instance_variable_set(variable, instance_exec(&block))
11
+ instance_variable_get(variable) || begin
12
+ if instance_variable_defined?(variable)
13
+ instance_variable_get(variable)
14
+ else
15
+ instance_variable_set(variable, instance_exec(&block))
16
+ end
15
17
  end
16
18
  end
17
19
 
18
20
  module ClassMethods
19
21
  def memoize(name, &block)
20
- variable = "@#{name}".to_sym
21
- define_method(name) { memoize(variable, &block) }
22
+ define_method("__#{name}__", &block)
23
+ class_eval <<-RUBY
24
+ def #{name}
25
+ @#{name} || begin
26
+ if defined?(@#{name})
27
+ @#{name}
28
+ else
29
+ @#{name} = __#{name}__
30
+ end
31
+ end
32
+ end
33
+ RUBY
22
34
  end
23
35
  end
24
36
  end
@@ -1,3 +1,3 @@
1
1
  module Memonic
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -7,7 +7,12 @@ describe Memonic do
7
7
  allow(memoizer).to receive(:computation).and_call_original
8
8
  end
9
9
 
10
- it "returns the computation result" do
10
+ it "returns the computation result on the first call" do
11
+ expect(memoizer.value).to be result
12
+ end
13
+
14
+ it "returns the same result on subsequent calls" do
15
+ memoizer.value
11
16
  expect(memoizer.value).to be result
12
17
  end
13
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memonic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Carney
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-16 00:00:00.000000000 Z
11
+ date: 2015-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler