Andrew Grimm
Andrew Grimm

Reputation: 81530

How should I avoid memoization causing bugs in Ruby?

Is there a consensus on how to avoid memoization causing bugs due to mutable state?

In this example, a cached result had its state mutated, and therefore gave the wrong result the second time it was called.

class Greeter

  def initialize
    @greeting_cache = {}
  end

  def expensive_greeting_calculation(formality)
    case formality
      when :casual then "Hi"
      when :formal then "Hello"
    end
  end

  def greeting(formality)
    unless @greeting_cache.has_key?(formality)
      @greeting_cache[formality] = expensive_greeting_calculation(formality)
    end
    @greeting_cache[formality]
  end

end

def memoization_mutator
  greeter = Greeter.new
  first_person = "Bob"
  # Mildly contrived in this case,
  # but you could encounter this in more complex scenarios
  puts(greeter.greeting(:casual) << " " << first_person) # => Hi Bob
  second_person = "Sue"
  puts(greeter.greeting(:casual) << " " << second_person) # => Hi Bob Sue
end

memoization_mutator

Approaches I can see to avoid this are:

  1. greeting could return a dup or clone of @greeting_cache[formality]
  2. greeting could freeze the result of @greeting_cache[formality]. That'd cause an exception to be raised when memoization_mutator appends strings to it.
  3. Check all code that uses the result of greeting to ensure none of it does any mutating of the string.

Is there a consensus on the best approach? Is the only disadvantage of doing (1) or (2) decreased performance? (I also suspect freezing an object may not work fully if it has references to other objects)

Side note: this problem doesn't affect the main application of memoization: as Fixnums are immutable, calculating Fibonacci sequences doesn't have problems with mutable state. :)

Upvotes: 8

Views: 634

Answers (2)

OldBoy
OldBoy

Reputation: 110

I am still 'ruby newbie', and I don't know if you were aware about the difference between '<<' and '+' methods to a String.

first_person = "Bob"
puts(greeter.greeting(:casual) + " " + first_person) # => Hi Bob
second_person = "Sue"
puts(greeter.greeting(:casual) + " " + second_person) # => Hi Sue

# str << obj → str
# str + other_str → new_str

Upvotes: 0

zaius
zaius

Reputation: 6409

I would lean towards returning a cloned object. The performance hit of creating a new string is next to nothing. And freezing exposes implementation details.

Upvotes: 4

Related Questions