Track the memoized methods for faster flush "all"
Track the memoized methods for faster flush "all"

When flushing all memoized methods, we can easily track which ones we
memoized and flush just these instead of using methods, private_methods,
protected_methods, and doing string/symbol manipulation to detect
our methods.

For a class with 40 memoized methods flushed 10,000 times:

Before: 3.22s user 0.05s system 99% cpu 3.279 total
After: 1.40s user 0.06s system 99% cpu 1.456 total

SCRIPT:

require './lib/memoist'
require 'set'

$methods = Set.new
class Person
  extend Memoist
  1.upto(20) do |n|
    method = define_method("test_#{n}".to_sym) {}
    memoize(method)
    $methods.add(method)
  end

  1.upto(20) do |n|
    method = define_method("test_#{n}?".to_sym) {}
    memoize(method)
    $methods.add(method)
  end
end

p = Person.new
10_000.times do
  p.flush_cache
end

matthewrudy/memoist