Skip to content

Commit

Permalink
Fixed fetch call
Browse files Browse the repository at this point in the history
  • Loading branch information
jemmaissroff committed Jan 4, 2022
1 parent 8242f44 commit 81b2ff0
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions lib/memo_wise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,38 +107,20 @@ def #{method_name}
memo_wise_module.module_eval <<~HEREDOC, __FILE__, __LINE__ + 1
def #{method_name}(#{MemoWise::InternalAPI.args_str(method)})
_memo_wise_hash = (_memo_wise[:#{method_name}] ||= {})
_memo_wise_output = _memo_wise_hash[#{key}]
if _memo_wise_output || _memo_wise_hash.key?(#{key})
_memo_wise_output
else
_memo_wise_hash.fetch(#{key}) do
_memo_wise_hash[#{key}] = super
end
end
HEREDOC
# MemoWise::InternalAPI::MULTIPLE_REQUIRED, MemoWise::InternalAPI::SPLAT,
# MemoWise::InternalAPI::DOUBLE_SPLAT, MemoWise::InternalAPI::SPLAT_AND_DOUBLE_SPLAT
else
# NOTE: When benchmarking this implementation against something like:
#
# @_memo_wise.fetch(key) do
# ...
# end
#
# this implementation may sometimes perform worse than the above. This
# is because this case uses a more complex hash key (see
# `MemoWise::InternalAPI.key_str`), and hashing that key has less
# consistent performance. In general, this should still be faster for
# truthy results because `Hash#[]` generally performs hash lookups
# faster than `Hash#fetch`.
memo_wise_module.module_eval <<~HEREDOC, __FILE__, __LINE__ + 1
def #{method_name}(#{MemoWise::InternalAPI.args_str(method)})
_memo_wise_hash = (_memo_wise[:#{method_name}] ||= {})
_memo_wise_key = #{MemoWise::InternalAPI.key_str(method)}
_memo_wise_output = _memo_wise_hash[_memo_wise_key]
if _memo_wise_output || _memo_wise_hash.key?(_memo_wise_key)
_memo_wise_output
else
_memo_wise_hash.fetch(_memo_wise_key) do
_memo_wise_hash[_memo_wise_key] = super
end
end
Expand Down

0 comments on commit 81b2ff0

Please sign in to comment.