module Bogo::Memoization

Memoization helpers

Constants

EXCLUSIVE_LOCK

Lock for providing exclusive access

GLOBAL_MEMOS

Holder for global memoization items

Public Class Methods

cleanup(object_id) click to toggle source

Clean up isolated memoizations

@param object_id [Object] @return [Proc]

# File lib/bogo/memoization.rb, line 18
def cleanup(object_id)
  proc do
    Thread.current[:bogo_memoization].delete_if do |k,v|
      k.to_s.start_with?(object_id.to_s)
    end
  end
end
clear_current!() click to toggle source

Clear thread memoizations

@return [nil]

# File lib/bogo/memoization.rb, line 29
def clear_current!
  Thread.current[:bogo_memoization] = nil
end
clear_global!() click to toggle source

Clear global memoizations

@return [nil]

# File lib/bogo/memoization.rb, line 36
def clear_global!
  EXCLUSIVE_LOCK.synchronize do
    GLOBAL_MEMOS.clear
  end
end

Public Instance Methods

_memo() click to toggle source

@return [Smash] memoization hash for current thread

# File lib/bogo/memoization.rb, line 71
def _memo
  unless(Thread.current[:bogo_memoization])
    Thread.current[:bogo_memoization] = Smash.new
    ObjectSpace.define_finalizer(self, Bogo::Memoization.cleanup(self.object_id))
  end
  Thread.current[:bogo_memoization]
end
clear_memoizations!() click to toggle source

Remove all memoized values

@return [TrueClass]

# File lib/bogo/memoization.rb, line 118
def clear_memoizations!
  _memo.keys.find_all do |key|
    key.to_s.start_with?("#{self.object_id}_")
  end.each do |key|
    _memo.delete(key)
  end
  true
end
memoize(key, direct=false) { || ... } click to toggle source

Memoize data

@param key [String, Symbol] identifier for data @param direct [Truthy, Falsey] direct skips key prepend of object id @yield block to create data @yieldreturn data to memoize @return [Object] data

# File lib/bogo/memoization.rb, line 51
def memoize(key, direct=false)
  unless(direct)
    key = "#{self.object_id}_#{key}"
  end
  if(direct == :global)
    EXCLUSIVE_LOCK.synchronize do
      unless(GLOBAL_MEMOS.has_key?(key))
        GLOBAL_MEMOS[key] = yield
      end
      GLOBAL_MEMOS[key]
    end
  else
    unless(_memo.has_key?(key))
      _memo[key] = yield
    end
    _memo[key]
  end
end
memoized?(key, direct=false) click to toggle source

Check if memoization entry for given key exists

@param key [String, Symbol] identifier for data @param direct [Truthy, Falsey] direct skips key prepend of object id @return [TrueClass, FalseClass]

# File lib/bogo/memoization.rb, line 84
def memoized?(key, direct=false)
  unless(direct)
    key = "#{self.object_id}_#{key}"
  end
  if(direct == :global)
    EXCLUSIVE_LOCK.synchronize do
      GLOBAL_MEMOS.key?(key)
    end
  else
    _memo.key?(key)
  end
end
unmemoize(key, direct=false) click to toggle source

Remove memoized value

@param key [String, Symbol] identifier for data @param direct [Truthy, Falsey] direct skips key prepend of object id @return [Object] removed instance

# File lib/bogo/memoization.rb, line 102
def unmemoize(key, direct=false)
  unless(direct)
    key = "#{self.object_id}_#{key}"
  end
  if(direct == :global)
    EXCLUSIVE_LOCK.synchronize do
      GLOBAL_MEMOS.delete(key)
    end
  else
    _memo.delete(key)
  end
end