pakiderm 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +25 -16
- data/lib/pakiderm/recompiler.rb +6 -1
- data/lib/pakiderm/version.rb +1 -1
- data/lib/pakiderm.rb +13 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6ecbb403a6459578fba812d5ebfbe298cc42bab0d8c442f1d26c7f48add8ed17
|
|
4
|
+
data.tar.gz: 45d158f5eaff0afabc579777215394ae84cd61f54b9f440b82c44a847de479e4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d7f34cca2e806aa6b3975e9ee28b3047d43ef5f388fae875edd39406bc272678e792ff014ff098e97668b4c32e13a3ae76cb84ccf0496a9aa265b8be77931573
|
|
7
|
+
data.tar.gz: fd496d6aacf53d1b6dedb7ad620a9dde54a47f6f69aca59b6ff9672f068a3fdda74788d3f01e13de545b2bf197efceabf23f35c555b5631246a9b57a4b1737db
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Pakiderm [](https://rubygems.org/gems/pakiderm) [](https://github.com/ElMassimo/pakiderm/actions/workflows/ruby.yml) [](https://qlty.sh/gh/ElMassimo/projects/pakiderm) [](https://qlty.sh/gh/ElMassimo/projects/pakiderm) [](LICENSE.txt)
|
|
2
2
|
=====================
|
|
3
3
|
|
|
4
|
-
Allows you to
|
|
4
|
+
Allows you to easily memoize methods, with the best performance.
|
|
5
5
|
|
|
6
6
|
```ruby
|
|
7
7
|
class Stopwatch
|
|
@@ -37,19 +37,33 @@ You can also pass a list of methods after defining them on the same class or mod
|
|
|
37
37
|
memoize :complex_calculation, :api_response, :db_query
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
### Configuration ⚙️
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
You can configure Pakiderm at boot, before loading your memoized methods:
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
```ruby
|
|
45
|
+
# config/initializers/pakiderm.rb
|
|
46
|
+
Pakiderm.cache_prefix = ""
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### `cache_prefix`
|
|
50
|
+
|
|
51
|
+
_Default:_ `"pakiderm_"`
|
|
52
|
+
|
|
53
|
+
The prefix for memoized instance variables. Set it to `""` to use `@songs` for `songs` if your app reserves that name for memoization. With an empty prefix, methods that use the same instance variable share a cache: don’t memoize both an override and its `super` method.
|
|
45
54
|
|
|
46
55
|
## Background
|
|
47
56
|
|
|
48
|
-
|
|
57
|
+
`memoize` uses Prism to recompile the method in place from:
|
|
49
58
|
|
|
50
|
-
|
|
59
|
+
```ruby
|
|
60
|
+
memoize
|
|
61
|
+
def elapsed_time
|
|
62
|
+
Time.now - @started_at
|
|
63
|
+
end
|
|
64
|
+
```
|
|
51
65
|
|
|
52
|
-
|
|
66
|
+
to
|
|
53
67
|
|
|
54
68
|
```ruby
|
|
55
69
|
def elapsed_time
|
|
@@ -58,15 +72,10 @@ def elapsed_time
|
|
|
58
72
|
end
|
|
59
73
|
```
|
|
60
74
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
The method keeps its visibility and its place in Ruby's lookup order. A module prepended before the definition can still call `super`. A subclass can memoize an override that calls `super`, too: the parent and child store their results separately. No hidden alias is needed. An alias made after memoizing uses the cache; an earlier alias still calls the original method.
|
|
75
|
+
It performs just like writing it by hand, run the benchmarks with `bundle exec ruby --yjit benchmark/readers.rb`.
|
|
64
76
|
|
|
65
|
-
|
|
77
|
+
Unlike other techniques, this works well with alias and module prepend.
|
|
66
78
|
|
|
67
|
-
|
|
79
|
+
## License
|
|
68
80
|
|
|
69
|
-
|
|
70
|
-
- Pakiderm preserves source lines, lexical constants, and frozen-string settings. Files that activate refinements with `using`, dynamic class or module paths around the reader, and `class << self` inside a block with unknown `self` aren't supported.
|
|
71
|
-
- Custom method-added hooks must call `super` and can see both definitions. To export a module function, use `module_function :name` **after** memoizing; bare `module_function` mode leaves the module-level copy uncached.
|
|
72
|
-
- As with handwritten memoization, an exception is not cached, a cache miss cannot write to a frozen receiver, and concurrent first calls are not synchronized.
|
|
81
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/pakiderm/recompiler.rb
CHANGED
|
@@ -58,7 +58,12 @@ module Pakiderm
|
|
|
58
58
|
raise Error, "unsupported method name: `#{name}`" unless match
|
|
59
59
|
|
|
60
60
|
kind = { "?" => "question", "!" => "bang", "" => "reader" }.fetch(match[2])
|
|
61
|
-
|
|
61
|
+
prefix = Pakiderm.cache_prefix
|
|
62
|
+
ivar = if prefix.empty?
|
|
63
|
+
kind == "reader" ? "@#{match[1]}" : "@#{match[1]}_#{kind}"
|
|
64
|
+
else
|
|
65
|
+
"@#{prefix}#{owner.object_id}_#{kind}_#{match[1]}"
|
|
66
|
+
end
|
|
62
67
|
guard = "; return #{ivar} if defined?(#{ivar}); #{ivar} = "
|
|
63
68
|
source = node.slice
|
|
64
69
|
start = node.location.start_offset
|
data/lib/pakiderm/version.rb
CHANGED
data/lib/pakiderm.rb
CHANGED
|
@@ -6,6 +6,19 @@ require_relative "pakiderm/version"
|
|
|
6
6
|
module Pakiderm
|
|
7
7
|
Error = Class.new(StandardError)
|
|
8
8
|
|
|
9
|
+
@cache_prefix = "pakiderm_"
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
attr_reader :cache_prefix
|
|
13
|
+
|
|
14
|
+
def cache_prefix=(prefix)
|
|
15
|
+
unless prefix.is_a?(String) && /\A(?:[a-zA-Z_]\w*)?\z/.match?(prefix)
|
|
16
|
+
raise ArgumentError, "cache_prefix must be an identifier prefix or an empty string"
|
|
17
|
+
end
|
|
18
|
+
@cache_prefix = prefix.dup.freeze
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
9
22
|
def memoize(*names)
|
|
10
23
|
if names.empty?
|
|
11
24
|
@_pakiderm_pending = true
|