MemoWise

Tests Yard Docs Inline docs Gem Version Gem Downloads Code Coverage

Why MemoWise?

MemoWise is the wise choice for memoization in Ruby.

  • Fast performance of memoized reads (see Benchmarks)
  • Support for memoization on frozen objects
  • Support for memoization of class and module methods (COMING SOON!)
  • 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

See API docs for more details.

class Example
  prepend MemoWise

  def method_to_memoize(x)
    @method_called_times = (@method_called_times || 0) + 1
  end
  memo_wise :method_to_memoize
end

ex = Example.new

ex.method_to_memoize("a") #=> 1
ex.method_to_memoize("a") #=> 1

ex.method_to_memoize("b") #=> 2
ex.method_to_memoize("b") #=> 2

Benchmarks

Memoized value retrieval time using Ruby 2.7.2 and benchmark-ips 2.8.3:

Method arguments memo_wise (0.1.0) memery (1.3.0) memoist (0.16.2) memoized (1.0.2) memoizer (1.0.3)
() (none) baseline 11.90x slower 2.50x slower 1.25x slower 3.24x slower
(a, b) baseline 1.96x slower 2.25x slower 1.80x slower 1.97x slower
(a:, b:) baseline 2.16x slower 2.34x slower 2.09x slower 2.16x slower
(a, b:) baseline 1.55x slower 1.68x slower 1.49x slower 1.54x slower
(a, *args) baseline 2.01x slower 2.29x slower 1.93x slower 1.98x slower
(a:, **kwargs) baseline 1.87x slower 2.04x slower 1.86x slower 1.88x slower
(a, *args, b:, **kwargs) baseline 1.90x slower 2.15x slower 1.88x slower 1.88x slower

Benchmarks are run in GitHub Actions and updated in every PR.

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!

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

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

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.

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.