Coder Social home page Coder Social logo

memoizable's Introduction

Memoizable

Gem Version CI Dependency Status Code Climate

Memoize method return values

Contributing

See CONTRIBUTING.md for details.

Rationale

Memoization is an optimization that saves the return value of a method so it doesn't need to be re-computed every time that method is called. For example, perhaps you've written a method like this:

class Planet
  # This is the equation for the area of a sphere. If it's true for a
  # particular instance of a planet, then that planet is spherical.
  def spherical?
    4 * Math::PI * radius ** 2 == area
  end
end

This code will re-compute whether a particular planet is spherical every time the method is called. If the method is called more than once, it may be more efficient to save the computed value in an instance variable, like so:

class Planet
  def spherical?
    @spherical ||= 4 * Math::PI * radius ** 2 == area
  end
end

One problem with this approach is that, if the return value is false, the value will still be computed each time the method is called. It also becomes unweildy for methods that grow to be longer than one line.

These problems can be solved by mixing-in the Memoizable module and memoizing the method.

require 'memoizable'

class Planet
  include Memoizable
  def spherical?
    4 * Math::PI * radius ** 2 == area
  end
  memoize :spherical?
end

Warning

The example above assumes that the radius and area of a planet will not change over time. This seems like a reasonable assumption but such an assumption is not safe in every domain. If it was possible for one of the attributes to change between method calls, memoizing that value could produce the wrong result. Please keep this in mind when considering which methods to memoize.

Supported Ruby Versions

This library aims to support and is tested against the following Ruby implementations:

  • Ruby 2.1
  • Ruby 2.2
  • Ruby 2.3
  • Ruby 2.4
  • Ruby 2.5
  • Ruby 2.6
  • Ruby 2.7
  • JRuby

If something doesn't work on one of these versions, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby versions or implementations, however support will only be provided for the implementations listed above.

If you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.

Copyright

Copyright © 2013 Dan Kubb, Erik Michaels-Ober. See LICENSE for details.

memoizable's People

Contributors

dkubb avatar mbj avatar sferik avatar trliner avatar ys avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

memoizable's Issues

Possible performance issues?

I noticed a massive slow down in ROM after upgrading to latest adamantium/memoizable. Do you guys think some recent changes may affect performance (ThreadSafe maybe?).

Calling memoized superclass method from memoized method results in hard to debug exception

require 'memoizable'

class A
  include Memoizable
  def foo
    :foo
  end
  memoize :foo
end

class B < A
  def foo
    super
  end
  memoize :foo
end

B.new.foo

Results in:

/home/mbj/.gem/ruby/2.1.0/gems/memoizable-0.4.0/lib/memoizable/memory.rb:48:in `[]=': The method foo is already memoized (ArgumentError)
        from /home/mbj/.gem/ruby/2.1.0/gems/memoizable-0.4.0/lib/memoizable/memory.rb:63:in `block (3 levels) in fetch'
        from /home/mbj/.gem/ruby/2.1.0/gems/thread_safe-0.1.3/lib/thread_safe/cache.rb:51:in `fetch'
        from /home/mbj/.gem/ruby/2.1.0/gems/memoizable-0.4.0/lib/memoizable/memory.rb:62:in `block (2 levels) in fetch'
        from /home/mbj/.rubies/ruby-2.1.0/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
        from /home/mbj/.gem/ruby/2.1.0/gems/memoizable-0.4.0/lib/memoizable/memory.rb:61:in `block in fetch'
        from /home/mbj/.gem/ruby/2.1.0/gems/thread_safe-0.1.3/lib/thread_safe/cache.rb:51:in `fetch'
        from /home/mbj/.gem/ruby/2.1.0/gems/memoizable-0.4.0/lib/memoizable/memory.rb:60:in `fetch'
        from /home/mbj/.gem/ruby/2.1.0/gems/memoizable-0.4.0/lib/memoizable/method_builder.rb:116:in `block (2 levels) in create_memoized_method'
        from repro.rb:17:in `<main>'

Root cause: Memory#[]= gets hit twice and the memory rightfully raises here.

IMHO we should detect this situation when overriding the memoizer in parent class.

Memoization of singleton methods raises NoMethodError

I was messing around in irb (actually, pry) and felt 😕 when I attempted to memoize a method outside of a class context:

[1] pry(main)> require 'memoizable'
=> true
[2] pry(main)> include Memoizable
=> Object
[3] pry(main)> def foo; end
=> nil
[4] pry(main)> memoize :foo
NoMethodError: undefined method `each' for :foo:Symbol
from /Users/e/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/memoizable-0.2.0/lib/memoizable/memory.rb:59:in `set'

This is the line where the error is occurring, which I believe is being called from here.

This error occurs when attempting to memoize singleton methods, defined on an instance of a class:

require 'memoizable'

class Foo
  include Memoizable
end

foo = Foo.new
def foo.bar; end
foo.memoize :bar

To resolve this issue we must first address the following question: Should one be able to memoize singleton methods?

If the answer is “yes”, resolving this issue entails adding that feature.

If the answer is “no”, we can just raise a better error message.

Support for parameterized methods

Hello.

I just finished publishing https://github.com/KoanHealth/forget-me-not. While tipping the hat to @sferik, he pointed me to this gem. I agree with him that the world doesn't need these ideas developed in two places, so I'd like to offer to help in any way that I can.

Two things that I incorporated into forget-me-not are arguments and caching.

Arguments are pretty straightforward.

Caching is intended to be more of a system wide memoization. We started from @sferik's code to enable some classes that chew on aggregate medical claims data to share the results of their initial queries.

My big question is can these two ideas find a home in this gem?

Cacheable capability?

One thing that got lost in the hubbub over memoizing methods with arguments was the caching capability in forget-me-not which has a similar interface (even if the intent is wildly different).

I know this is a somewhat odd fit in a project called memoizable, but the two ideas occupy a similar region of headspace, so I thought it made sense to combine them in a single gem.

There's a pretty decent write-up of the intent and capabilities of cacheable on the forget-me-not project page. Is it possible that this idea can find a home here?

Memoizing methods with implicit blocks may surprise user

Consider the following code:

class Foo

  def bar
    yield
  end
  memoize :bar

  def baz
    yield if block_given?
  end
  memoize :baz

end

Currently, if a user memoizes a method with an explicit block (or any explicit parameters), they will receive an error at load time (Memoizable::MethodBuilder::InvalidArityError). However, if a method yields to an implicit block, users will receive a LocalJumpError (in the case of bar) or—worse—no error at all (in the case of baz). This behavior may surprise users, who don’t expect memoize to modify the behavior of their methods.

This seems bad but I can’t think of a good solution so I’m opening this issue for discussion. Curious to hear your thoughts…

its not support ruby 3. because of : ruby_dep-1.5.0

Query List: ["memoizable"]
Query Gemcutter Dependency Endpoint API: memoizable
HTTP GET https://gems.ruby-china.com/api/v1/dependencies?gems=memoizable
HTTP 200 OK https://gems.ruby-china.com/api/v1/dependencies?gems=memoizable
Query List: []
Resolving dependencies....
ruby_dep-1.5.0 requires ruby version ~> 2.2, >= 2.2.5, which is incompatible with the current version, ruby 3.0.3p157
Bundler::InstallError: ruby_dep-1.5.0 requires ruby version ~> 2.2, >= 2.2.5, which is incompatible with the current version, ruby 3.0.3p157
/home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/installer.rb:249:in `block in ensure_specs_are_compatible!'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/spec_set.rb:136:in `each'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/spec_set.rb:136:in `each'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/installer.rb:246:in `ensure_specs_are_compatible!'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/installer.rb:83:in `block in run'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/process_lock.rb:12:in `block in lock'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/process_lock.rb:9:in `open'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/process_lock.rb:9:in `lock'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/installer.rb:71:in `run'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/installer.rb:23:in `install'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/cli/install.rb:60:in `run'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/cli.rb:253:in `block in install'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/settings.rb:131:in `temporary'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/cli.rb:252:in `install'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/cli.rb:31:in `dispatch'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/cli.rb:25:in `start'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/libexec/bundle:49:in `block in <top (required)>'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0/bundler/friendly_errors.rb:103:in `with_friendly_errors'
  /home/siwei/.asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/libexec/bundle:37:in `<top (required)>'
  /home/siwei/.asdf/installs/ruby/3.0.3/bin/bundle:23:in `load'
  /home/siwei/.asdf/installs/ruby/3.0.3/bin/bundle:23:in `<main>'

Lazy loader / auto include

Hello,
I wanted to make memoize available across the project and I came up with this:

Class.class_eval do
  def memoize *args
    include ::Memoizable
    memoize *args
  end
end

But that's a bit ugly solution so just wanted to ask how would you recommend achieving this?

Deprecation message

".rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/memoizable-0.4.2/lib/memoizable/method_builder.rb:117:in `call': [DEPRECATION] #verified is deprecated. Use #verified? instead."

I don'y have the memoizable gem within my project so I'm not sure what might be generating the warning.

My rake task still runs - it's just outputting this message for each record I process.

I'm running Rails 4.1.4 and using Postgres for my database.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.