Project

memo_wise

0.18
The project is in a healthy, maintained state
The wise choice for Ruby memoization
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
 Dependencies
 Project Readme

MemoWise

Tests Code Coverage Yard Docs Gem Version Gem Downloads

Why MemoWise?

MemoWise is the wise choice for Ruby memoization, featuring:

  • Fast performance of memoized reads (with benchmarks)
  • Support for resetting and presetting memoized values
  • Support for memoization on frozen objects
  • Support for memoization of class and module methods
  • Support for inheritance of memoized class and instance methods
  • Documented and tested thread-safety guarantees
  • Full documentation and test coverage!

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

  private

  # maintains privacy of the memoized method
  def private_slow_method(x)
    sleep x
    x
  end
  memo_wise :private_slow_method
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 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.9.2).

Results using Ruby 3.1.2:

Method arguments Dry::Core* (0.7.1) Memery (1.4.0)
() (none) 1.14x 13.06x
(a) 1.22x 7.24x
(a, b) 1.17x 6.79x
(a:) 1.33x 16.68x
(a:, b:) 1.24x 13.73x
(a, b:) 1.16x 13.58x
(a, *args) 0.84x 1.95x
(a:, **kwargs) 0.84x 3.41x
(a, *args, b:, **kwargs) 0.75x 2.08x

* Dry::Core may cause incorrect behavior caused by hash collisions.

Results using Ruby 2.7.6 (because these gems raise errors in Ruby 3.x):

Method arguments DDMemoize (1.0.0) Memoist (0.16.2) Memoized (1.1.1) Memoizer (1.0.3)
() (none) 24.72x 2.36x 27.25x 2.88x
(a) 22.13x 14.22x 23.37x 12.54x
(a, b) 17.70x 12.04x 17.69x 10.66x
(a:) 31.24x 24.94x 26.47x 22.97x
(a:, b:) 25.76x 20.78x 21.41x 19.47x
(a, b:) 23.97x 19.53x 19.91x 18.01x
(a, *args) 3.23x 2.21x 3.34x 1.98x
(a:, **kwargs) 2.92x 2.45x 2.57x 2.24x
(a, *args, b:, **kwargs) 2.22x 1.88x 1.99x 1.78x

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!

Thread Safety

MemoWise makes the following thread safety guarantees on all supported Ruby versions:

  1. Before a value has been memoized

    • Contended calls from multiple threads...
      • May each call the original method
      • May return different valid results (when the method is nondeterministic, like rand)
      • Will memoize exactly one valid return value
  2. After a value has been memoized

    • Contended calls from multiple threads...
      • Always return the same memoized value

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 presented at RubyConf 2021:

  • Achieving Fast Method Metaprogramming: Lessons from MemoWise (slides / benchmarks)

And we've written more about MemoWise in a series of blog posts:

Logo

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
    • Add a link for this version's comparison to the bottom of CHANGELOG.md
    • Move content from Unreleased to the upcoming version x.y.z
    • Change Unreleased section to say:
      **Gem enhancements:** none
      
      _No breaking changes!_
      
      **Project enhancements:** none
      
    • Commit with title Update CHANGELOG.md for x.y.z
  2. Update lib/memo_wise/version.rb

    • Replace with upcoming version x.y.z
    • Run bundle install to update Gemfile.lock
    • 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.