MemoWise

Tests Code Coverage Yard Docs Inline docs Gem Version Gem Downloads

Why MemoWise?

MemoWise is the wise choice for Ruby memoization, featuring:

Installation

Add this line to your application's Gemfile:

gem 'memo_wise'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install memo_wise

Usage

When you prepend MemoWise within a class or module, MemoWise exposes three methods:

class Example
  prepend MemoWise

  def slow_value(x)
    sleep x
    x
  end
  memo_wise :slow_value
end

ex = Example.new
ex.slow_value(2) # => 2 # Sleeps for 2 seconds before returning
ex.slow_value(2) # => 2 # Returns immediately because the result is memoized

ex.reset_memo_wise(:slow_value) # Resets all memoized results for slow_value
ex.slow_value(2) # => 2 # Sleeps for 2 seconds before returning
ex.slow_value(2) # => 2 # Returns immediately because the result is memoized
# NOTE: Memoization can also be reset for all methods, or for just one argument.

ex.preset_memo_wise(:slow_value, 3) { 4 } # Store 4 as the result for slow_value(3)
ex.slow_value(3) # => 4 # Returns immediately because the result is memoized
ex.reset_memo_wise # Resets all memoized results for all methods on ex

The same three methods are exposed for class methods as well:

class Example
  prepend MemoWise

  def self.class_slow_value(x)
    sleep x
    x
  end
  memo_wise self: :class_slow_value
end

Example.class_slow_value(2) # => 2 # Sleeps for 2 seconds before returning
Example.class_slow_value(2) # => 2 # Returns immediately because the result is memoized

Example.reset_memo_wise(:class_slow_value) # Resets all memoized results for class_slow_value

Example.preset_memo_wise(:class_slow_value, 3) { 4 } # Store 4 as the result for slow_value(3)
Example.class_slow_value(3) # => 4 # Returns immediately because the result is memoized
Example.reset_memo_wise # Resets all memoized results for all methods on class

NOTE: Methods which take implicit or explicit block arguments cannot be memoized.

For more usage details, see our detailed documentation.

Benchmarks

Benchmarks measure memoized value retrieval time using benchmark-ips. Benchmarks are run in GitHub Actions and updated in every PR that changes code.

Values >1.00x represent how much slower each gem’s memoized value retrieval is than the baseline.

Benchmarks using Ruby 3.0.2:

Method arguments memo_wise (latest) dry-core (0.7.1) memery (1.4.0)
() (none) baseline 0.98-1.44x 10.40-14.95x
(a) baseline 2.10x 8.39x
(a, b) baseline baseline* 4.45x
(a:) baseline 1.89x 17.37x
(a:, b:) baseline baseline* 9.66x
(a, b:) baseline baseline* 8.78x
(a, *args) baseline 1.88x 4.07x
(a:, **kwargs) baseline 1.41x 4.98x
(a, *args, b:, **kwargs) baseline baseline* 2.50x

*Indicates a run that was slower than the baseline but the difference was not significant.

The following benchmarks are run on Ruby 2.7.4 because these gems raise errors in Ruby 3.0.2 due to their incorrect handling of keyword arguments:

Method arguments memo_wise (latest) ddmemoize (1.0.0) memoist (0.16.2) memoized (1.0.2) memoizer (1.0.3)
() (none) baseline 20.96-25.44x 2.49-3.31x 1.06-1.48x 2.96-3.83x
(a) baseline 19.37x 13.28x 9.31x 10.78x
(a, b) baseline 3.65x 2.64x 2.00x 2.28x
(a:) baseline 25.88x 21.74x 17.68x 19.17x
(a:, b:) baseline 5.72x 4.57x 3.90x 4.42x
(a, b:) baseline 5.63x 5.06x 4.20x 4.18x
(a, *args) baseline 5.13x 3.89x 3.25x 3.31x
(a:, **kwargs) baseline 3.23x 2.55x 2.33x 2.47x
(a, *args, b:, **kwargs) baseline 2.51x 2.00x 1.84x 1.88x

You can run benchmarks yourself with:

$ cd benchmarks
$ bundle install
$ bundle exec ruby benchmarks.rb

If your results differ from what's posted here, let us know!

Documentation

Documentation is Automatically Generated

We maintain API documentation using YARD, which is published automatically at RubyDoc.info. To edit documentation locally and see it rendered in your browser, run:

bundle exec yard server

Documentation Examples are Automatically Tested

We use yard-doctest to test all code examples in our YARD documentation. To run doctest locally:

bundle exec yard doctest

We use dokaz to test all code examples in this README.md file, and all other non-code documentation. To run dokaz locally:

bundle exec dokaz

A Note on Testing

When 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 }

Further Reading

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.

Contributing

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.

Releasing

To make a new release of MemoWise to RubyGems, first install the release dependencies (e.g. rake) as follows:

bundle config --local with 'release'
bundle install

Then carry out these steps:

  1. Update CHANGELOG.md:

    • Add an entry for the upcoming version x.y.z
    • Move content from Unreleased to the upcoming version x.y.z
    • Commit with title Update CHANGELOG.md for x.y.z
  2. Update lib/memo_wise/version.rb

    • Replace with upcoming version x.y.z
    • Commit with title Bump version to x.y.z
  3. bundle exec rake release

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the MemoWise project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.