Skip to content

Commit

Permalink
Performance patch about args.hash from @CKolkey
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWayfer committed Sep 25, 2024
1 parent b2a9361 commit 1b2e45a
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/memery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def define_memoized_method(method_name, condition:, ttl:)

store = memery_store method_name

return store[args][:result] if memoized_result_actual?(store, args, ttl: ttl)
return store[args.hash][:result] if memoized_result_actual?(store, args, ttl: ttl)

call_original_and_memoize original_method, args, store
end
Expand All @@ -90,12 +90,12 @@ def memery_store(method_name)
end

def memoized_result_actual?(store, args, ttl:)
store.key?(args) && (ttl.nil? || Memery.monotonic_clock <= store[args][:time] + ttl)
store.key?(args.hash) && (ttl.nil? || Memery.monotonic_clock <= store[args.hash][:time] + ttl)
end

def call_original_and_memoize(original_method, args, store)
result = original_method.bind_call(self, *args)
store[args] = { result: result, time: Memery.monotonic_clock }
store[args.hash] = { result: result, time: Memery.monotonic_clock }
result
end
end

0 comments on commit 1b2e45a

Please sign in to comment.