tycooon / memery Goto Github PK
View Code? Open in Web Editor NEWA gem for memoization in Ruby
License: MIT License
A gem for memoization in Ruby
License: MIT License
See discussion and checks status in #26.
Maybe we can use GitHub Actions? @AlexWayfer what do you think?
I was wondering if this is threadsafe. It seems https://github.com/yegor256/zache is and it mentions it in the README.
Would be useful to know!
We have a change merged in the middle of last year, but it looks like the latest release is still from 2021 - would you mind dropping another version?
code:
require "memery"
require "pry"
class A
include Memery
memoize def test
end
end
a = A.new
binding.pry
and in pry execute $ a.test
current behavior:
> $ a.test
From: .../.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/memery-1.3.0/lib/memery.rb:58:
Owner: #<Module:0x000055b9f6294130>
Visibility: public
Signature: test(*args, &block)
Number of lines: 17
define_method method_name do |*args, &block|
if block || (condition && !instance_exec(&condition))
return super(*args, &block)
end
store = (@_memery_memoized_values ||= {})[method_key] ||= {}
if store.key?(args) &&
(ttl.nil? || Memery.monotonic_clock <= store[args][:time] + ttl)
return store[args][:result]
end
result = super(*args)
@_memery_memoized_values[method_key][args] =
{ result: result, time: Memery.monotonic_clock }
result
end
preferred behavior:
> $ a.test
From: test.rb:6:
Owner: A
Visibility: public
Signature: test()
Number of lines: 2
def test
end
Is it possible?
I know that the prevailing memoized_attribute(true)
approach is somewhat flaky but is there a way to recalculate a given memoized property without expiring the entire cache so to speak?
As well I apologize if my knowledge of memery is somewhat limited but does it memoize functions that accept arguments?
Usage:
my_macro memoize def foo
...
end
Reading this essay:
https://island94.org/2023/10/writing-object-shape-friendly-code-in-ruby
And the reddit conversation:
https://www.reddit.com/r/ruby/comments/16xwjh9/writing_object_shape_friendly_code_in_ruby/
Made me think there was a place for a gem that would support memoization that complies with recent ruby object shape performance optimizations.
It's a bit tricky, because it does require hooking into initializer too. I haven't looked at memery code atall to see how hard it would be (or if you are already compliant, in which case you could advertise that in README?), but at least theoretically should be possible?
In an ideal world, memery API would otherwise remain the same. Perhaps you'd opt into the new behavior with include Memery::Optimized
instead of just include Memery
or something like that.
It looks like default method arguments are not cached separately if they change implicitly, take this for example:
Object.include Memery
I18n = Struct.new(:locale).new(:en)
def test_method(locale: I18n.locale)
locale
end
Object.memoize :test_method
test_method
# => :en, correct
test_method(locale: :fr)
# => :fr, correct
I18n.locale = :fr
test_method
# => :en, wrong, should be :fr
Didn't have a chance to look at the code yet, but wanted to raise the issue before I forget :-)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.