The wise choice for Ruby memoization
MemoWiseMemoWise?MemoWise is the wise choice for Ruby memoization, featuring:
Add this line to your application's Gemfile:
gem 'memo_wise'And then execute:
$ bundle installOr install it yourself as:
$ gem install memo_wiseWhen you prepend MemoWise within a class or module, MemoWise exposes three
methods:
…The same three methods are exposed for class methods as well:
…NOTE: Methods which take implicit or explicit block arguments cannot be memoized.
For more usage details, see our detailed documentation.
Benchmarks are run in GitHub Actions, and the tables below are updated with every code change. Values >1.00x represent how much slower each gem’s memoized value retrieval is than the latest commit of MemoWise, according to benchmark-ips (2.14.0).
Results using Ruby 3.4.3:
| Method arguments | alt_memery (2.1.0) |
dry-core* (1.1.0) |
memery (1.7.0) |
memoist3 (1.0.0) |
short_circu_it (0.29.3) |
|---|---|---|---|---|---|
() (none) |
12.20x | 0.57x | 3.31x | 2.76x | 18.45x |
(a) |
9.75x | 0.98x | 3.76x | 14.54x | 13.96x |
(a, b) |
7.59x | 0.82x | 2.92x | 11.39x | 10.87x |
(a:) |
14.89x | 0.97x | 6.39x | 19.76x | 12.60x |
(a:, b:) |
12.64x | 0.86x | 5.43x | 21.05x | 10.70x |
(a, b:) |
12.25x | 0.84x | 5.22x | 16.12x | 10.31x |
(a, *args) |
1.89x | 0.65x | 0.73x | 2.84x | 2.70x |
(a:, **kwargs) |
2.86x | 0.71x | 1.21x | 4.79x | 2.42x |
(a, *args, b:, **kwargs) |
1.81x | 0.62x | 0.83x | 3.03x | 1.52x |
* dry-core
may cause incorrect behavior caused by hash collisions.
You can run benchmarks yourself with:
$ cd benchmarks
$ bundle install
$ bundle exec ruby benchmarks.rbIf your results differ from what's posted here, or if there's another gem we should be benchmarking, let us know!
MemoWise makes the following thread safety guarantees on all supported Ruby versions:
Before a value has been memoized
rand)After a value has been memoized
We maintain API documentation using YARD, which is published automatically at RubyDoc.info.
To generate documentation locally or run documentation tests,
first install the docs dependencies (e.g. yard) as follows:
BUNDLE_WITH=docs bundle installTo edit documentation locally and see it rendered in your browser using hot reloading, run:
BUNDLE_WITH=docs bundle exec yard server --reloadYou can then open your web browser to http://127.0.0.1:8808/. As you
edit documentation locally, reload your browser to see it generated.
To statically generate documentation locally, run:
bundle exec yardYou can then open the generated documentation at docs/index.html.
We use yard-doctest to test all
code examples in our YARD documentation. To run doctest locally:
bundle exec yard doctestWe use dokaz to test all code examples in
this README.md file, and all other non-code documentation. To run dokaz
locally:
bundle exec dokazWhen testing memoized module methods, note that some testing setups will
reuse the same instance (which includes/extends/prepends the module)
across tests, which can result in confusing test failures when this differs from
how you use the code in production.
For example, Rails view helpers are modules that are commonly tested with a
shared view instance. Rails initializes a new view instance for each web request so any view helper
methods would only be memoized for the duration of that web request, but in
tests (such as when using
rspec-rails's helper),
the memoization may persist across tests. In this case, simply reset the
memoization between your tests with something like:
after(:each) { helper.reset_memo_wise }We presented at RubyConf 2021:
MemoWise
(slides /
benchmarks)And we've written more about MemoWise in a series of blog posts:
MemoWise's logo was created by Luci Cooke. The
logo is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.
Bug reports and pull requests are welcome on GitHub at https://github.com/panorama-ed/memo_wise. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
To make a new release of MemoWise to
RubyGems:
Update CHANGELOG.md:
CHANGELOG.md**Gem enhancements:** none
_No breaking changes!_
**Project enhancements:** noneUpdate lib/memo_wise/version.rb
bundle install to update Gemfile.lockBump version to x.y.zBUNDLE_WITH=release bundle install && BUNDLE_WITH=release bundle exec rake release
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the MemoWise project's codebases, issue trackers, chat
rooms and mailing lists is expected to follow the
code of conduct.
No open issues yet, or sync has not completed.