ractor-cache 0.1.0
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 +7 -0
- data/.github/workflows/ractor-cache.yml +45 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +54 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +89 -0
- data/Rakefile +8 -0
- data/bin/console +17 -0
- data/bin/setup +8 -0
- data/hacker_guide.md +143 -0
- data/lib/ractor/cache.rb +18 -0
- data/lib/ractor/cache/caching_layer.rb +66 -0
- data/lib/ractor/cache/store.rb +37 -0
- data/lib/ractor/cache/strategy.rb +114 -0
- data/lib/ractor/cache/version.rb +7 -0
- data/ractor-cache.gemspec +31 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 377a50819a1cba2fe238c388c7d493cddf0260f97edaa88952bdfde0bb46f8f9
|
4
|
+
data.tar.gz: 4723d13d9e5b3a41cb84be4aff50061d67e2bf9850275c098cc05197b6c549c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7e7ed096e89d921bbd30bfb3884a29c4ec4d83fe56d38865394f85b8eb5e88806a41f06dcf061a6370450f06e9249b7ef4c70dcddf64c4015ba7d1ba2e379a2d
|
7
|
+
data.tar.gz: 3bf00fd952e7f8dbe610160c1d34320181821d6bb5aaecbe89866f64e2239e909a5e499f81ed12bdb8746e887013b23411dbc4089f437a16c4c22554bc61bb73
|
@@ -0,0 +1,45 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
tests:
|
7
|
+
name: >-
|
8
|
+
Specs | ${{ matrix.ruby }}
|
9
|
+
runs-on: ${{ matrix.os }}-latest
|
10
|
+
strategy:
|
11
|
+
fail-fast: false
|
12
|
+
matrix:
|
13
|
+
os: [ ubuntu ]
|
14
|
+
ruby: [ 2.4, 2.5, 2.6, 2.7, head ]
|
15
|
+
steps:
|
16
|
+
- name: checkout
|
17
|
+
uses: actions/checkout@v2
|
18
|
+
- name: set up Ruby
|
19
|
+
uses: ruby/setup-ruby@v1
|
20
|
+
with:
|
21
|
+
ruby-version: ${{ matrix.ruby }}
|
22
|
+
- name: install dependencies
|
23
|
+
run: bundle install --jobs 3 --retry 3
|
24
|
+
- name: spec
|
25
|
+
run: bundle exec rake
|
26
|
+
internal_investigation:
|
27
|
+
name: >-
|
28
|
+
Coding Style
|
29
|
+
runs-on: ${{ matrix.os }}-latest
|
30
|
+
strategy:
|
31
|
+
fail-fast: false
|
32
|
+
matrix:
|
33
|
+
os: [ ubuntu ]
|
34
|
+
ruby: [ 2.7 ]
|
35
|
+
steps:
|
36
|
+
- name: checkout
|
37
|
+
uses: actions/checkout@v2
|
38
|
+
- name: set up Ruby
|
39
|
+
uses: ruby/setup-ruby@v1
|
40
|
+
with:
|
41
|
+
ruby-version: ${{ matrix.ruby }}
|
42
|
+
- name: install dependencies
|
43
|
+
run: bundle install --jobs 3 --retry 3
|
44
|
+
- name: internal investigation
|
45
|
+
run: bundle exec rubocop
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
TargetRubyVersion: 2.4
|
4
|
+
NewCops: enable
|
5
|
+
|
6
|
+
# For sure
|
7
|
+
|
8
|
+
Layout/LineLength:
|
9
|
+
Max: 150
|
10
|
+
|
11
|
+
Style/ModuleFunction:
|
12
|
+
EnforcedStyle: extend_self
|
13
|
+
|
14
|
+
Style/TrailingCommaInArrayLiteral:
|
15
|
+
EnforcedStyleForMultiline: consistent_comma
|
16
|
+
Style/TrailingCommaInHashLiteral:
|
17
|
+
EnforcedStyleForMultiline: consistent_comma
|
18
|
+
|
19
|
+
Layout/SpaceInsideHashLiteralBraces:
|
20
|
+
EnforcedStyle: no_space
|
21
|
+
|
22
|
+
Metrics/BlockLength:
|
23
|
+
Exclude:
|
24
|
+
- 'spec/**/*.rb'
|
25
|
+
|
26
|
+
Style/Documentation:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Gemspec/RequiredRubyVersion:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Layout/MultilineBlockLayout:
|
33
|
+
Enabled: false
|
34
|
+
Layout/BlockEndNewline:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Style/CommentedKeyword:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Style/NilComparison:
|
41
|
+
EnforcedStyle: comparison
|
42
|
+
|
43
|
+
# Experiment
|
44
|
+
|
45
|
+
Style/AccessModifierDeclarations:
|
46
|
+
EnforcedStyle: inline
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
# Layout/SpaceBeforeBlockBraces:
|
51
|
+
# EnforcedStyleForEmptyBraces: no_space
|
52
|
+
|
53
|
+
Style/DocumentDynamicEvalDefinition:
|
54
|
+
Enabled: false
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at github@marc-andre.ca. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in ractor-cache.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'backports', git: 'https://github.com/marcandre/backports.git', branch: 'ractor'
|
9
|
+
gem 'pry-byebug', require: false
|
10
|
+
gem 'rake', require: false
|
11
|
+
gem 'rspec', require: false
|
12
|
+
gem 'rubocop', require: false
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Marc-Andre Lafortune
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# Ractor::Cache
|
2
|
+
|
3
|
+
## Usage:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# Typical cached method:
|
7
|
+
class Foo
|
8
|
+
def long_calc
|
9
|
+
@long_calc ||= do_long_calculation
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# With Ractor::Cache:
|
14
|
+
using Ractor::Cache
|
15
|
+
|
16
|
+
class Foo
|
17
|
+
cache def long_calc
|
18
|
+
do_long_calculation
|
19
|
+
end
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
## Why?
|
24
|
+
|
25
|
+
0) It's pretty
|
26
|
+
1) It makes your class `Ractor`-compatible
|
27
|
+
|
28
|
+
## `Ractor`-compatible?
|
29
|
+
|
30
|
+
Ractor is new in Ruby 3.0 and is awesome.
|
31
|
+
|
32
|
+
Passing classes between Ractors can be done very efficiently if classes are deeply frozen.
|
33
|
+
|
34
|
+
For some classes, being frozen isn't useful or possible (e.g. `IO`), but for many it is possible.
|
35
|
+
|
36
|
+
One challenge of writing classes that can be deeply frozen is methods that cache the results:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
f = Foo.new
|
40
|
+
f.freeze
|
41
|
+
f.long_calc # => FrozenError, can't set `@long_calc`
|
42
|
+
```
|
43
|
+
|
44
|
+
Some techniques could include storing the cache in a mutable data structures:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class Foo
|
48
|
+
def initialize
|
49
|
+
@cache = {}
|
50
|
+
end
|
51
|
+
|
52
|
+
def long_calc
|
53
|
+
@cache[:long_calc] ||= # ...
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
f = Foo.new
|
58
|
+
f.freeze
|
59
|
+
f.long_calc # => ok
|
60
|
+
```
|
61
|
+
|
62
|
+
But `Ractor.make_shareable` freezes the instance variables too, so this can't work:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
f = Foo.new
|
66
|
+
Ractor.make_shareable(foo)
|
67
|
+
foo.long_calc # => `FrozenError`, @cache is frozen
|
68
|
+
```
|
69
|
+
|
70
|
+
## How to resolve this
|
71
|
+
|
72
|
+
This gem will:
|
73
|
+
1) Use a mutable data structure like above, which means no issue for shallow freezing an instance
|
74
|
+
2) If an instance is deeply frozen, the gem will either insure things will keep working by applying any of the following strategies:
|
75
|
+
- prebuild the cache,
|
76
|
+
- not write to the cache,
|
77
|
+
- (or maybe use a separate Ractor / `SharedHash`)
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marcandre/ractor-cache. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/marcandre/ractor-cache/blob/master/CODE_OF_CONDUCT.md).
|
82
|
+
|
83
|
+
## License
|
84
|
+
|
85
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
86
|
+
|
87
|
+
## Code of Conduct
|
88
|
+
|
89
|
+
Everyone interacting in the Ractor::Cache project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/marcandre/ractor-cache/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'ractor/cache'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
require_relative '../spec/ractor/fixtures'
|
11
|
+
|
12
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
13
|
+
require 'pry'
|
14
|
+
Pry.start
|
15
|
+
|
16
|
+
# require 'irb'
|
17
|
+
# IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/hacker_guide.md
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
### Structure
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
using Ractor::Cache
|
5
|
+
|
6
|
+
class Animal
|
7
|
+
cache def something
|
8
|
+
# ... calculation
|
9
|
+
end
|
10
|
+
|
11
|
+
cache def something_else
|
12
|
+
# ... other calculation
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Mammal < Animal
|
17
|
+
end
|
18
|
+
|
19
|
+
class Ape < Mammal
|
20
|
+
cache def something_else # Supported if same strategy and signature
|
21
|
+
return super unless a_predicate?
|
22
|
+
|
23
|
+
# specialized calculation
|
24
|
+
end
|
25
|
+
|
26
|
+
def complex(arg)
|
27
|
+
# ... calculation
|
28
|
+
def
|
29
|
+
cache :complex, strategy: :disable
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Equivalent code:
|
34
|
+
```
|
35
|
+
class Animal
|
36
|
+
module RactorCacheLayer
|
37
|
+
CACHED = ... # private information of how things are cached
|
38
|
+
|
39
|
+
# Where caching info is stored for `Animal`
|
40
|
+
class Store
|
41
|
+
def initialize(owner)
|
42
|
+
@owner = owner
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_accessor :something, :something_else
|
46
|
+
|
47
|
+
def freeze # only called in case of deep-freezing
|
48
|
+
@owner.class::RactorCacheLayer.deep_freeze_callback(@owner)
|
49
|
+
super
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def something
|
54
|
+
ractor_cache.something ||= super
|
55
|
+
end
|
56
|
+
|
57
|
+
def freeze
|
58
|
+
ractor_cache # make sure cache store is built
|
59
|
+
super
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.deep_freeze_callback(instance)
|
63
|
+
# strategy for `something` is prebuild:
|
64
|
+
instance.something
|
65
|
+
# same for `something_else`:
|
66
|
+
instance.something_else
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def ractor_cache
|
72
|
+
@ractor_cache ||= self.class::Store.new
|
73
|
+
end
|
74
|
+
end
|
75
|
+
prepend CacheLayer
|
76
|
+
|
77
|
+
def something
|
78
|
+
# ... calculation
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class Mammal < Animal
|
83
|
+
# Nothing special cache-wise here.
|
84
|
+
end
|
85
|
+
|
86
|
+
class Ape < Mammal
|
87
|
+
module RactorCacheLayer
|
88
|
+
CACHED = ... # private array of Strategy
|
89
|
+
|
90
|
+
# Where caching info is stored for `Ape`
|
91
|
+
class Store < Animal::CacheLayer::Store
|
92
|
+
attr_reader :complex
|
93
|
+
|
94
|
+
def initialize(owner)
|
95
|
+
@complex = {}
|
96
|
+
super
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def something_else
|
101
|
+
ractor_cache.something_else ||= super
|
102
|
+
end
|
103
|
+
|
104
|
+
def complex(arg)
|
105
|
+
hash = ractor_cache.complex
|
106
|
+
hash.fetch(arg) do
|
107
|
+
result = super
|
108
|
+
# strategy is disable:
|
109
|
+
hash[arg] = result unless frozen?
|
110
|
+
result
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.deep_freeze_callback(instance)
|
115
|
+
# strategy for `complex` is disable
|
116
|
+
# nothing to do
|
117
|
+
# process any other cached data
|
118
|
+
# and then
|
119
|
+
super
|
120
|
+
end
|
121
|
+
end
|
122
|
+
prepend CacheLayer
|
123
|
+
|
124
|
+
def complex(arg)
|
125
|
+
# ... calculation
|
126
|
+
def
|
127
|
+
end
|
128
|
+
|
129
|
+
Ape.ancestors # =>
|
130
|
+
[ Ape::CacheLayer, # Top cache layer
|
131
|
+
Ape,
|
132
|
+
Mammal,
|
133
|
+
Animal::CacheLayer, # Second cache layer
|
134
|
+
Animal,
|
135
|
+
Object, #...
|
136
|
+
]
|
137
|
+
```
|
138
|
+
|
139
|
+
### Class ancestors
|
140
|
+
|
141
|
+
In the "equivalent" code above, `Animal::CacheLayer` is actually an instance of `Ractor::Cache::CacheLayer`.
|
142
|
+
|
143
|
+
`Animal::CacheLayer::Store` is a subclass of `Ractor::Cache::Store`
|
data/lib/ractor/cache.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'require_relative_dir'
|
4
|
+
using RequireRelativeDir
|
5
|
+
|
6
|
+
class Ractor
|
7
|
+
module Cache
|
8
|
+
require_relative_dir
|
9
|
+
|
10
|
+
private def cache(method_name, strategy: nil)
|
11
|
+
CachingLayer[self].cache(method_name, strategy: strategy)
|
12
|
+
end
|
13
|
+
|
14
|
+
refine Module do
|
15
|
+
include Cache
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Ractor
|
4
|
+
module Cache
|
5
|
+
module CachingLayer
|
6
|
+
def freeze
|
7
|
+
ractor_cache # Make sure the instance variable is created beforehand
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
private def ractor_cache
|
12
|
+
@ractor_cache ||= self.class::Store.new(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
attr_reader :sublayer, :cached, :parent
|
17
|
+
|
18
|
+
def attach(mod, sublayer)
|
19
|
+
@sublayer = sublayer
|
20
|
+
@cached = []
|
21
|
+
@parent = mod
|
22
|
+
mod.prepend self
|
23
|
+
substore = sublayer&.const_get(:Store, false) || Cache::Store
|
24
|
+
const_set(:Store, Class.new(substore))
|
25
|
+
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def cache(method, strategy:)
|
30
|
+
strat = build_stragegy(method, strategy)
|
31
|
+
file, line = strat.method(:compile_accessor).source_location
|
32
|
+
module_eval(strat.compile_accessor, file, line + 2)
|
33
|
+
@cached << strat
|
34
|
+
self::Store.update(@cached)
|
35
|
+
end
|
36
|
+
|
37
|
+
def deep_freeze_callback(instance)
|
38
|
+
@cached.each do |strategy|
|
39
|
+
strategy.deep_freeze_callback(instance)
|
40
|
+
end
|
41
|
+
sublayer&.deep_freeze_callback(instance)
|
42
|
+
end
|
43
|
+
|
44
|
+
# @returns [Strategy]
|
45
|
+
private def build_stragegy(method, strategy)
|
46
|
+
im = begin
|
47
|
+
@parent.instance_method(method)
|
48
|
+
rescue ::NameError => e
|
49
|
+
raise e, "#{e.message}. Method must be defined before calling `cache`", e.backtrace
|
50
|
+
end
|
51
|
+
Strategy.new(strategy, to_cache: im)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Return `CachingLayer` in `mod`, creating it if need be.
|
55
|
+
def [](mod)
|
56
|
+
if mod.const_defined?(:RactorCacheLayer, false)
|
57
|
+
mod::RactorCacheLayer
|
58
|
+
else
|
59
|
+
sublayer = mod::RactorCacheLayer if mod.const_defined?(:RactorCacheLayer, true)
|
60
|
+
mod.const_set(:RactorCacheLayer, dup.attach(mod, sublayer))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Ractor
|
4
|
+
module Cache
|
5
|
+
class Store
|
6
|
+
def initialize(owner) # Possibly redefined by `update`
|
7
|
+
@owner = owner
|
8
|
+
end
|
9
|
+
|
10
|
+
def freeze
|
11
|
+
@owner.class::RactorCacheLayer.deep_freeze_callback(@owner)
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def update(cached)
|
17
|
+
update_accessors(cached)
|
18
|
+
update_init(cached)
|
19
|
+
end
|
20
|
+
|
21
|
+
private def update_accessors(cached)
|
22
|
+
attr_accessor(*cached.map(&:method_name))
|
23
|
+
end
|
24
|
+
|
25
|
+
private def update_init(cached)
|
26
|
+
body = cached.map(&:compile_store_init).join("\n")
|
27
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
28
|
+
def initialize(owner)
|
29
|
+
#{body}
|
30
|
+
super
|
31
|
+
end
|
32
|
+
RUBY
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Ractor
|
4
|
+
module Cache
|
5
|
+
module Strategy
|
6
|
+
class Base
|
7
|
+
attr_reader :parameters, :method_name
|
8
|
+
|
9
|
+
def initialize(instance_method)
|
10
|
+
@method_name = instance_method.name
|
11
|
+
analyse_parameters(instance_method.parameters)
|
12
|
+
end
|
13
|
+
|
14
|
+
def compile_store_init
|
15
|
+
"@#{method_name} = {}" if @has_arguments
|
16
|
+
end
|
17
|
+
|
18
|
+
private def analyse_parameters(parameters) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
|
19
|
+
@has_positional_arguments = @has_keywords_arguments = false
|
20
|
+
parameters.each do |type, name|
|
21
|
+
case type
|
22
|
+
when :req, :opt, :rest
|
23
|
+
@has_positional_arguments = true
|
24
|
+
@has_keywords_arguments = true if type == :rest && name == :*
|
25
|
+
when :key, :keyrest
|
26
|
+
@has_keywords_arguments = true
|
27
|
+
when :nokey, :block
|
28
|
+
# ignore
|
29
|
+
end
|
30
|
+
@has_arguments = @has_positional_arguments || @has_keywords_arguments
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private def compile_lookup
|
35
|
+
args = [
|
36
|
+
*('args' if @has_positional_arguments),
|
37
|
+
*('opts' if @has_keywords_arguments),
|
38
|
+
]
|
39
|
+
|
40
|
+
return if args.empty?
|
41
|
+
|
42
|
+
"[#{args.join(', ')}]"
|
43
|
+
end
|
44
|
+
|
45
|
+
private def signature
|
46
|
+
[
|
47
|
+
*('*args' if @has_positional_arguments),
|
48
|
+
*('**opts' if @has_keywords_arguments),
|
49
|
+
].join(', ')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Prebuild < Base
|
54
|
+
def initialize(*)
|
55
|
+
super
|
56
|
+
raise "Can not cache method #{method_name} by prebuilding because it accepts arguments" if @has_arguments
|
57
|
+
end
|
58
|
+
|
59
|
+
def deep_freeze_callback(instance)
|
60
|
+
instance.__send__ method_name
|
61
|
+
end
|
62
|
+
|
63
|
+
def compile_accessor
|
64
|
+
<<~RUBY
|
65
|
+
def #{method_name}(#{signature})
|
66
|
+
ractor_cache.#{method_name} ||= super
|
67
|
+
end
|
68
|
+
RUBY
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class Disable < Base
|
73
|
+
def compile_accessor
|
74
|
+
<<~RUBY
|
75
|
+
def #{method_name}(#{signature})
|
76
|
+
ractor_cache.#{method_name}#{compile_lookup} || begin
|
77
|
+
result = super
|
78
|
+
ractor_cache.#{method_name}#{compile_lookup} = result unless ractor_cache.frozen?
|
79
|
+
result
|
80
|
+
end
|
81
|
+
end
|
82
|
+
RUBY
|
83
|
+
end
|
84
|
+
|
85
|
+
def deep_freeze_callback(instance)
|
86
|
+
# nothing to do
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
MAP = {
|
91
|
+
prebuild: Prebuild,
|
92
|
+
disable: Disable,
|
93
|
+
}.freeze
|
94
|
+
private_constant :MAP
|
95
|
+
|
96
|
+
class << self
|
97
|
+
def [](kind)
|
98
|
+
MAP.fetch(kind)
|
99
|
+
end
|
100
|
+
|
101
|
+
def new(
|
102
|
+
strategy = nil, # => (:prebuild | :disable)?
|
103
|
+
to_cache: # => UnboundMethod
|
104
|
+
) # => Strategy
|
105
|
+
self[strategy || :prebuild].new(to_cache)
|
106
|
+
rescue ArgumentError
|
107
|
+
return new(:prebuild, to_cache: to_cache) if strategy == nil
|
108
|
+
|
109
|
+
raise
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/ractor/cache/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'ractor-cache'
|
7
|
+
spec.version = Ractor::Cache::VERSION
|
8
|
+
spec.authors = ['Marc-Andre Lafortune']
|
9
|
+
spec.email = ['github@marc-andre.ca']
|
10
|
+
|
11
|
+
spec.summary = 'Ractor-compatible cache.'
|
12
|
+
spec.description = 'Ractor-compatible cache.'
|
13
|
+
spec.homepage = 'https://github.com/marcandre/ractor-cache'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/marcandre/ractor-cache'
|
19
|
+
# spec.metadata["changelog_uri"] = ""
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'require_relative_dir'
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = 'exe'
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ractor-cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marc-Andre Lafortune
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: require_relative_dir
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Ractor-compatible cache.
|
28
|
+
email:
|
29
|
+
- github@marc-andre.ca
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".github/workflows/ractor-cache.yml"
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- ".rubocop.yml"
|
38
|
+
- CODE_OF_CONDUCT.md
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- bin/console
|
44
|
+
- bin/setup
|
45
|
+
- hacker_guide.md
|
46
|
+
- lib/ractor/cache.rb
|
47
|
+
- lib/ractor/cache/caching_layer.rb
|
48
|
+
- lib/ractor/cache/store.rb
|
49
|
+
- lib/ractor/cache/strategy.rb
|
50
|
+
- lib/ractor/cache/version.rb
|
51
|
+
- ractor-cache.gemspec
|
52
|
+
homepage: https://github.com/marcandre/ractor-cache
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata:
|
56
|
+
homepage_uri: https://github.com/marcandre/ractor-cache
|
57
|
+
source_code_uri: https://github.com/marcandre/ractor-cache
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 2.3.0
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.1.4
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Ractor-compatible cache.
|
77
|
+
test_files: []
|