Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Memoizable

no longer ActiveSupport::Memoizable
no dependency on ActiveSupport.
  • Loading branch information...
commit 35fe922d427fabcbb2947dba225c8b8a7f2801da 1 parent 036485c
@matthewrudy authored
View
13 lib/core_ext/singleton_class.rb
@@ -0,0 +1,13 @@
+module Kernel
+ # Returns the object's singleton class.
+ def singleton_class
+ class << self
+ self
+ end
+ end unless respond_to?(:singleton_class) # exists in 1.9.2
+
+ # class_eval on an object acts like singleton_class.class_eval.
+ def class_eval(*args, &block)
+ singleton_class.class_eval(*args, &block)
+ end
+end
View
175 lib/memoizable.rb
@@ -1,110 +1,109 @@
-require 'active_support/core_ext/kernel/singleton_class'
-require 'active_support/core_ext/module/aliasing'
+# require 'active_support/core_ext/kernel/singleton_class'
+require 'core_ext/singleton_class'
-module ActiveSupport
- module Memoizable
+module Memoizable
- def self.memoized_ivar_for(symbol)
- "@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
- end
+ def self.memoized_ivar_for(symbol)
+ "@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
+ end
- module InstanceMethods
- def self.included(base)
- base.class_eval do
- unless base.method_defined?(:freeze_without_memoizable)
- alias_method_chain :freeze, :memoizable
- end
+ module InstanceMethods
+ def self.included(base)
+ base.class_eval do
+ unless base.method_defined?(:freeze_without_memoizable)
+ alias_method :freeze_without_memoizable, :freeze
+ alias_method :freeze, :freeze_with_memoizable
end
end
+ end
- def freeze_with_memoizable
- memoize_all unless frozen?
- freeze_without_memoizable
- end
+ def freeze_with_memoizable
+ memoize_all unless frozen?

I know this project came about since Rails deprecated ActiveSupport::Memoizable, but what's the purpose of this logic?

Currently, I have remote API calls that are memoized and they are being called on save and on delete regardless if I use them.

@matthewrudy Owner

Memoizable functions on a per object basis, and is designed to avoid performing a costly calculation multiple times.

The simple example is;

user.something_costly # does the work
user.something_costly # is fetched from the cache

But it works for a specific object

So;

User.find(4).something_costly # does the work
User.find(4).something_costly # does the work again
@zmoazeni
zmoazeni added a note

I get that, but why the aggressive caching with memoize_all unless frozen?? Why not wait until the memoized methods are called and have them cached at that point?

Imagine a class like this:

class Person < ActiveRecord::Base
  extend ActiveSupport::Memoizable

  def some_remote_call1
    # some expensive http call
  end
  memoize :some_remote_call1

  def some_remote_call2
    # some other expensive http call
  end
  memoize :some_remote_call2
end

some_remote_call1 and some_remote_call2 are called on save and destroy regardless if they are used.

@matthewrudy Owner

Sorry I missed your subtler point.
I didn't write this gem.
It's abstracted from the Rails codebase.

So the intent of this line is lost to me.
But this method is only called when you a freeze an object which has Memoizable mixed in.

I guess a frozen object should have frozen state.
And because Memoizable is implemented with instance variables as a cache, then its important to prime these all when the object is frozen.

But regarding your particular problem, does saving a record freeze it?
If so this is a problem.

@zmoazeni
zmoazeni added a note

But regarding your particular problem, does saving a record freeze it? If so this is a problem.

This is the behavior I'm 80% sure I saw. I ended up doing manual memoization for that expensive http method and everything magically worked. I haven't dug into the rails codebase yet to track down who triggers freeze but I think it's happening somewhere there.

It should be a simple test case to show the behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ freeze_without_memoizable
+ end
- def memoize_all
- prime_cache ".*"
- end
+ def memoize_all
+ prime_cache ".*"
+ end
- def unmemoize_all
- flush_cache ".*"
- end
+ def unmemoize_all
+ flush_cache ".*"
+ end
- def prime_cache(*syms)
- syms.each do |sym|
- methods.each do |m|
- if m.to_s =~ /^_unmemoized_(#{sym})/
- if method(m).arity == 0
- __send__($1)
- else
- ivar = ActiveSupport::Memoizable.memoized_ivar_for($1)
- instance_variable_set(ivar, {})
- end
+ def prime_cache(*syms)
+ syms.each do |sym|
+ methods.each do |m|
+ if m.to_s =~ /^_unmemoized_(#{sym})/
+ if method(m).arity == 0
+ __send__($1)
+ else
+ ivar = Memoizable.memoized_ivar_for($1)
+ instance_variable_set(ivar, {})
end
end
end
end
+ end
- def flush_cache(*syms)
- syms.each do |sym|
- (methods + private_methods + protected_methods).each do |m|
- if m.to_s =~ /^_unmemoized_(#{sym.to_s.gsub(/\?\Z/, '\?')})/
- ivar = ActiveSupport::Memoizable.memoized_ivar_for($1)
- instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
- end
+ def flush_cache(*syms)
+ syms.each do |sym|
+ (methods + private_methods + protected_methods).each do |m|
+ if m.to_s =~ /^_unmemoized_(#{sym.to_s.gsub(/\?\Z/, '\?')})/
+ ivar = Memoizable.memoized_ivar_for($1)
+ instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
end
end
end
end
+ end
- def memoize(*symbols)
- symbols.each do |symbol|
- original_method = :"_unmemoized_#{symbol}"
- memoized_ivar = ActiveSupport::Memoizable.memoized_ivar_for(symbol)
+ def memoize(*symbols)
+ symbols.each do |symbol|
+ original_method = :"_unmemoized_#{symbol}"
+ memoized_ivar = Memoizable.memoized_ivar_for(symbol)
- class_eval <<-EOS, __FILE__, __LINE__ + 1
- include InstanceMethods # include InstanceMethods
- #
- if method_defined?(:#{original_method}) # if method_defined?(:_unmemoized_mime_type)
- raise "Already memoized #{symbol}" # raise "Already memoized mime_type"
- end # end
- alias #{original_method} #{symbol} # alias _unmemoized_mime_type mime_type
- #
- if instance_method(:#{symbol}).arity == 0 # if instance_method(:mime_type).arity == 0
- def #{symbol}(reload = false) # def mime_type(reload = false)
- if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty? # if reload || !defined?(@_memoized_mime_type) || @_memoized_mime_type.empty?
- #{memoized_ivar} = [#{original_method}] # @_memoized_mime_type = [_unmemoized_mime_type]
- end # end
- #{memoized_ivar}[0] # @_memoized_mime_type[0]
- end # end
- else # else
- def #{symbol}(*args) # def mime_type(*args)
- #{memoized_ivar} ||= {} unless frozen? # @_memoized_mime_type ||= {} unless frozen?
- args_length = method(:#{original_method}).arity # args_length = method(:_unmemoized_mime_type).arity
- if args.length == args_length + 1 && # if args.length == args_length + 1 &&
- (args.last == true || args.last == :reload) # (args.last == true || args.last == :reload)
- reload = args.pop # reload = args.pop
- end # end
- #
- if defined?(#{memoized_ivar}) && #{memoized_ivar} # if defined?(@_memoized_mime_type) && @_memoized_mime_type
- if !reload && #{memoized_ivar}.has_key?(args) # if !reload && @_memoized_mime_type.has_key?(args)
- #{memoized_ivar}[args] # @_memoized_mime_type[args]
- elsif #{memoized_ivar} # elsif @_memoized_mime_type
- #{memoized_ivar}[args] = #{original_method}(*args) # @_memoized_mime_type[args] = _unmemoized_mime_type(*args)
- end # end
- else # else
- #{original_method}(*args) # _unmemoized_mime_type(*args)
- end # end
- end # end
- end # end
- #
- if private_method_defined?(#{original_method.inspect}) # if private_method_defined?(:_unmemoized_mime_type)
- private #{symbol.inspect} # private :mime_type
- elsif protected_method_defined?(#{original_method.inspect}) # elsif protected_method_defined?(:_unmemoized_mime_type)
- protected #{symbol.inspect} # protected :mime_type
- end # end
- EOS
- end
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
+ include InstanceMethods # include InstanceMethods
+ #
+ if method_defined?(:#{original_method}) # if method_defined?(:_unmemoized_mime_type)
+ raise "Already memoized #{symbol}" # raise "Already memoized mime_type"
+ end # end
+ alias #{original_method} #{symbol} # alias _unmemoized_mime_type mime_type
+ #
+ if instance_method(:#{symbol}).arity == 0 # if instance_method(:mime_type).arity == 0
+ def #{symbol}(reload = false) # def mime_type(reload = false)
+ if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty? # if reload || !defined?(@_memoized_mime_type) || @_memoized_mime_type.empty?
+ #{memoized_ivar} = [#{original_method}] # @_memoized_mime_type = [_unmemoized_mime_type]
+ end # end
+ #{memoized_ivar}[0] # @_memoized_mime_type[0]
+ end # end
+ else # else
+ def #{symbol}(*args) # def mime_type(*args)
+ #{memoized_ivar} ||= {} unless frozen? # @_memoized_mime_type ||= {} unless frozen?
+ args_length = method(:#{original_method}).arity # args_length = method(:_unmemoized_mime_type).arity
+ if args.length == args_length + 1 && # if args.length == args_length + 1 &&
+ (args.last == true || args.last == :reload) # (args.last == true || args.last == :reload)
+ reload = args.pop # reload = args.pop
+ end # end
+ #
+ if defined?(#{memoized_ivar}) && #{memoized_ivar} # if defined?(@_memoized_mime_type) && @_memoized_mime_type
+ if !reload && #{memoized_ivar}.has_key?(args) # if !reload && @_memoized_mime_type.has_key?(args)
+ #{memoized_ivar}[args] # @_memoized_mime_type[args]
+ elsif #{memoized_ivar} # elsif @_memoized_mime_type
+ #{memoized_ivar}[args] = #{original_method}(*args) # @_memoized_mime_type[args] = _unmemoized_mime_type(*args)
+ end # end
+ else # else
+ #{original_method}(*args) # _unmemoized_mime_type(*args)
+ end # end
+ end # end
+ end # end
+ #
+ if private_method_defined?(#{original_method.inspect}) # if private_method_defined?(:_unmemoized_mime_type)
+ private #{symbol.inspect} # private :mime_type
+ elsif protected_method_defined?(#{original_method.inspect}) # elsif protected_method_defined?(:_unmemoized_mime_type)
+ protected #{symbol.inspect} # protected :mime_type
+ end # end
+ EOS
end
end
-end
+end
View
14 test/memoizable_test.rb
@@ -1,9 +1,9 @@
require 'test_helper'
require 'memoizable'
-class MemoizableTest < ActiveSupport::TestCase
+class MemoizableTest < Test::Unit::TestCase
class Person
- extend ActiveSupport::Memoizable
+ extend Memoizable
attr_reader :name_calls, :age_calls, :is_developer_calls, :name_query_calls
@@ -66,7 +66,7 @@ def name
end
module Rates
- extend ActiveSupport::Memoizable
+ extend Memoizable
attr_reader :sales_tax_calls
def sales_tax(price)
@@ -78,7 +78,7 @@ def sales_tax(price)
end
class Calculator
- extend ActiveSupport::Memoizable
+ extend Memoizable
include Rates
attr_reader :fib_calls
@@ -216,7 +216,7 @@ def test_memoization_with_boolean_arg
def test_object_memoization
[Company.new, Company.new, Company.new].each do |company|
- company.extend ActiveSupport::Memoizable
+ company.extend Memoizable
company.memoize :name
assert_equal "37signals", company.name
@@ -250,11 +250,11 @@ def test_object_memoized_module_methods
def test_double_memoization
assert_raise(RuntimeError) { Person.memoize :name }
person = Person.new
- person.extend ActiveSupport::Memoizable
+ person.extend Memoizable
assert_raise(RuntimeError) { person.memoize :name }
company = Company.new
- company.extend ActiveSupport::Memoizable
+ company.extend Memoizable
company.memoize :name
assert_raise(RuntimeError) { company.memoize :name }
end
View
1  test/test_helper.rb
@@ -1,5 +1,4 @@
require 'rubygems'
require 'test/unit'
-require 'active_support'
$:.unshift File.expand_path(File.dirname(__FILE__)+"/../lib")
@zmoazeni

I know this project came about since Rails deprecated ActiveSupport::Memoizable, but what's the purpose of this logic?

Currently, I have remote API calls that are memoized and they are being called on save and on delete regardless if I use them.

@matthewrudy

Memoizable functions on a per object basis, and is designed to avoid performing a costly calculation multiple times.

The simple example is;

user.something_costly # does the work
user.something_costly # is fetched from the cache

But it works for a specific object

So;

User.find(4).something_costly # does the work
User.find(4).something_costly # does the work again
@zmoazeni

I get that, but why the aggressive caching with memoize_all unless frozen?? Why not wait until the memoized methods are called and have them cached at that point?

Imagine a class like this:

class Person < ActiveRecord::Base
  extend ActiveSupport::Memoizable

  def some_remote_call1
    # some expensive http call
  end
  memoize :some_remote_call1

  def some_remote_call2
    # some other expensive http call
  end
  memoize :some_remote_call2
end

some_remote_call1 and some_remote_call2 are called on save and destroy regardless if they are used.

@matthewrudy

Sorry I missed your subtler point.
I didn't write this gem.
It's abstracted from the Rails codebase.

So the intent of this line is lost to me.
But this method is only called when you a freeze an object which has Memoizable mixed in.

I guess a frozen object should have frozen state.
And because Memoizable is implemented with instance variables as a cache, then its important to prime these all when the object is frozen.

But regarding your particular problem, does saving a record freeze it?
If so this is a problem.

@zmoazeni

But regarding your particular problem, does saving a record freeze it? If so this is a problem.

This is the behavior I'm 80% sure I saw. I ended up doing manual memoization for that expensive http method and everything magically worked. I haven't dug into the rails codebase yet to track down who triggers freeze but I think it's happening somewhere there.

It should be a simple test case to show the behavior.

Please sign in to comment.
Something went wrong with that request. Please try again.