加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

我应该如何避免在Ruby中导致错误的memoization?

发布时间:2020-12-16 19:07:59 所属栏目:百科 来源:网络整理
导读:关于如何避免由于可变状态导致错误的memoization,是否有共识? 在此示例中,缓存的结果的状态发生了突变,因此在第二次调用时会产生错误的结果. class Greeter def initialize @greeting_cache = {} end def expensive_greeting_calculation(formality) case f
关于如何避免由于可变状态导致错误的memoization,是否有共识?

在此示例中,缓存的结果的状态发生了突变,因此在第二次调用时会产生错误的结果.

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

我可以看到避免这种情况的方法是:

>问候语可以返回@greeting_cache的复制或克隆[形式]
>问候可以冻结@greeting_cache [形式]的结果.当memoization_mutator向其追加字符串时,这会引发异常.
>检查使用问候结果的所有代码,以确保它们不会对字符串进行任何变更.

对最佳方法有共识吗?做(1)或(2)的唯一缺点是性能下降? (我还怀疑如果某个对象引用了其他对象,它可能无法完全冻结)

旁注:这个问题不影响memoization的主要应用:由于Fixnums是不可变的,计算Fibonacci序列没有可变状态的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读