ddmemoize 1.0.0a2 → 1.0.0a3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/NEWS.md +6 -0
- data/README.md +8 -14
- data/ddmemoize.gemspec +1 -1
- data/lib/ddmemoize/version.rb +1 -1
- data/lib/ddmemoize.rb +19 -29
- data/samples/fib.rb +3 -3
- data/spec/ddmemoize_spec.rb +11 -20
- data/spec/spec_helper.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 075c0a1dd9addf300b84957a864cdd5e24a7a7131e0eb69a9c175351d82eee09
|
4
|
+
data.tar.gz: a398a8b1b884a404b413d837dd81f43057d5c1771cdb893e0838cafa4ff16b4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c74d84354fc652fcaec00c1bfeb60ae3e0a718cc8a7431879aefbb4e59e051015ca36e75aec99b913a33646cd93a67558a10317e53d498ce2557bf3289f57bd2
|
7
|
+
data.tar.gz: 1492b6c2cebc1398af73e3d0108967459bacece5bad8f0ed4777a549097a83b33adf97296655640dcdf5ff7effda5590adfadc9c99476165fe7a246d375b32f9
|
data/NEWS.md
CHANGED
data/README.md
CHANGED
@@ -83,24 +83,18 @@ Do not memoize functions that depend on mutable state.
|
|
83
83
|
|
84
84
|
### Telemetry
|
85
85
|
|
86
|
-
To activate telemetry
|
86
|
+
To activate telemetry, call `DDMemoize.enable_telemetry` after requiring `ddmemoize`.
|
87
87
|
|
88
|
-
|
89
|
-
require 'ddmemoize'
|
90
|
-
|
91
|
-
TELEMETRY = DDTelemetry.new
|
88
|
+
To print the collected metrics, call `DDMemoize.print_telemetry`:
|
92
89
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
# …
|
97
|
-
end
|
90
|
+
```ruby
|
91
|
+
DDMemoize.print_telemetry
|
98
92
|
```
|
99
93
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
94
|
+
```
|
95
|
+
memoization │ hit miss %
|
96
|
+
────────────┼───────────────────
|
97
|
+
FibFast#fib │ 998 1001 49.9%
|
104
98
|
```
|
105
99
|
|
106
100
|
## Development
|
data/ddmemoize.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
|
|
14
14
|
|
15
15
|
spec.required_ruby_version = '>= 2.3.0'
|
16
16
|
|
17
|
-
spec.add_runtime_dependency('ddtelemetry', '= 1.0.
|
17
|
+
spec.add_runtime_dependency('ddtelemetry', '= 1.0.0a2') # TODO: upgrade
|
18
18
|
spec.add_runtime_dependency('ref', '~> 2.0')
|
19
19
|
|
20
20
|
spec.files = `git ls-files -z`.split("\x0")
|
data/lib/ddmemoize/version.rb
CHANGED
data/lib/ddmemoize.rb
CHANGED
@@ -15,39 +15,31 @@ module DDMemoize
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
|
19
|
-
class TelemetryMap
|
20
|
-
include Singleton
|
18
|
+
NONE = Object.new
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
def self.activate(mod)
|
21
|
+
mod.extend(Mixin)
|
22
|
+
end
|
25
23
|
|
26
|
-
|
27
|
-
|
24
|
+
class << self
|
25
|
+
def enable_telemetry
|
26
|
+
@telemetry_enabled = true
|
28
27
|
end
|
29
28
|
|
30
|
-
def
|
31
|
-
@
|
29
|
+
def telemetry_enabled?
|
30
|
+
@telemetry_enabled
|
32
31
|
end
|
33
|
-
end
|
34
|
-
|
35
|
-
NONE = Object.new
|
36
|
-
|
37
|
-
def self.activate(mod, telemetry: nil)
|
38
|
-
mod.extend(Mixin)
|
39
|
-
TelemetryMap.instance[mod] = telemetry
|
40
|
-
end
|
41
32
|
|
42
|
-
|
43
|
-
|
33
|
+
def telemetry_counter
|
34
|
+
@_telemetry_counter ||= DDTelemetry::Counter.new
|
35
|
+
end
|
44
36
|
end
|
45
37
|
|
46
|
-
def self.print_telemetry
|
38
|
+
def self.print_telemetry
|
47
39
|
headers = %w[memoization hit miss %]
|
48
40
|
|
49
|
-
rows_raw =
|
50
|
-
{ name: name, type: type, count:
|
41
|
+
rows_raw = DDMemoize.telemetry_counter.map do |(name, type), count|
|
42
|
+
{ name: name, type: type, count: count }
|
51
43
|
end
|
52
44
|
|
53
45
|
rows = rows_raw.group_by { |r| r[:name] }.map do |name, rows_for_name|
|
@@ -70,7 +62,7 @@ module DDMemoize
|
|
70
62
|
alias_method original_method_name, method_name
|
71
63
|
|
72
64
|
instance_cache = Hash.new { |hash, key| hash[key] = {} }
|
73
|
-
|
65
|
+
counter_label = "#{self}##{method_name}"
|
74
66
|
|
75
67
|
define_method(method_name) do |*args|
|
76
68
|
instance_method_cache = instance_cache[self]
|
@@ -81,13 +73,11 @@ module DDMemoize
|
|
81
73
|
value = object ? object.value : NONE
|
82
74
|
end
|
83
75
|
|
84
|
-
if
|
85
|
-
counter_label = is_a?(Class) ? "#{self}.#{method_name}" : "#{self.class}##{method_name}"
|
86
|
-
|
76
|
+
if DDMemoize.telemetry_enabled?
|
87
77
|
if NONE.equal?(value)
|
88
|
-
|
78
|
+
DDMemoize.telemetry_counter.increment([counter_label, :miss])
|
89
79
|
else
|
90
|
-
|
80
|
+
DDMemoize.telemetry_counter.increment([counter_label, :hit])
|
91
81
|
end
|
92
82
|
end
|
93
83
|
|
data/samples/fib.rb
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
require 'ddmemoize'
|
4
4
|
|
5
|
-
|
5
|
+
DDMemoize.enable_telemetry
|
6
6
|
|
7
7
|
class FibFast
|
8
|
-
DDMemoize.activate(self
|
8
|
+
DDMemoize.activate(self)
|
9
9
|
|
10
10
|
memoized def fib(n)
|
11
11
|
case n
|
@@ -21,4 +21,4 @@ end
|
|
21
21
|
|
22
22
|
p FibFast.new.fib(1000)
|
23
23
|
|
24
|
-
DDMemoize.print_telemetry
|
24
|
+
DDMemoize.print_telemetry
|
data/spec/ddmemoize_spec.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
DDMemoize.enable_telemetry
|
4
|
+
|
3
5
|
describe DDMemoize do
|
4
6
|
it 'has a version number' do
|
5
7
|
expect(DDMemoize::VERSION).not_to be nil
|
@@ -41,8 +43,7 @@ describe DDMemoize do
|
|
41
43
|
end
|
42
44
|
|
43
45
|
class MemoizationSpecEqual
|
44
|
-
|
45
|
-
DDMemoize.activate(self, telemetry: TELEMETRY)
|
46
|
+
DDMemoize.activate(self)
|
46
47
|
|
47
48
|
class EqualToEverythingValue
|
48
49
|
def equal?(*)
|
@@ -79,8 +80,7 @@ describe DDMemoize do
|
|
79
80
|
end
|
80
81
|
|
81
82
|
class MemoizationSpecWithTelemetry
|
82
|
-
|
83
|
-
DDMemoize.activate(self, telemetry: TELEMETRY)
|
83
|
+
DDMemoize.activate(self)
|
84
84
|
|
85
85
|
def run(value)
|
86
86
|
value.upcase
|
@@ -115,11 +115,10 @@ describe DDMemoize do
|
|
115
115
|
sample.run
|
116
116
|
sample.run
|
117
117
|
|
118
|
-
|
119
|
-
counter = telemetry.counter(:memoization)
|
118
|
+
counter = DDMemoize.telemetry_counter
|
120
119
|
|
121
|
-
expect(counter.
|
122
|
-
expect(counter.
|
120
|
+
expect(counter.get(['MemoizationSpecEqual#run', :miss])).to eq(1)
|
121
|
+
expect(counter.get(['MemoizationSpecEqual#run', :hit])).to eq(2)
|
123
122
|
end
|
124
123
|
|
125
124
|
it 'supports memoized def … syntax' do
|
@@ -150,11 +149,10 @@ describe DDMemoize do
|
|
150
149
|
sample.run('denis')
|
151
150
|
sample.run('defreyne')
|
152
151
|
|
153
|
-
|
154
|
-
counter = telemetry.counter(:memoization)
|
152
|
+
counter = DDMemoize.telemetry_counter
|
155
153
|
|
156
|
-
expect(counter.
|
157
|
-
expect(counter.
|
154
|
+
expect(counter.get(['MemoizationSpecWithTelemetry#run', :miss])).to eq(2)
|
155
|
+
expect(counter.get(['MemoizationSpecWithTelemetry#run', :hit])).to eq(1)
|
158
156
|
end
|
159
157
|
|
160
158
|
it 'prints recorded telemetry' do
|
@@ -164,13 +162,6 @@ describe DDMemoize do
|
|
164
162
|
sample.run('denis')
|
165
163
|
sample.run('defreyne')
|
166
164
|
|
167
|
-
|
168
|
-
memoization │ hit miss %
|
169
|
-
─────────────────────────────────┼───────────────────
|
170
|
-
MemoizationSpecWithTelemetry#run │ 2 4 33.3%
|
171
|
-
OUTPUT
|
172
|
-
|
173
|
-
telemetry = MemoizationSpecWithTelemetry::TELEMETRY
|
174
|
-
expect { DDMemoize.print_telemetry(telemetry) }.to output(expected).to_stdout
|
165
|
+
expect { DDMemoize.print_telemetry }.to output(/memoization │ hit\s+miss\s+%/).to_stdout
|
175
166
|
end
|
176
167
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ddmemoize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.0a3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Defreyne
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ddtelemetry
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.0.
|
19
|
+
version: 1.0.0a2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.0.
|
26
|
+
version: 1.0.0a2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: ref
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|