-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow objects with memoized values to be marshaled/unmarshaled across Ruby processes #51
Open
obrie
wants to merge
12
commits into
tycooon:master
Choose a base branch
from
Tapjoy:feature/marshal-compat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This allows marshaled objects to retain their memoized values when being unmarshaled in a separate Ruby process where the memery module object_id may not be the same
…ven if the method's arity is non-zero) This has several positive impacts: * You don't encounter any performance overhead associated with allocating arrays and generating hashes * The cache keys are equal across multiple Ruby processes when not providing arguments
This allows marshaled objects to retain their memoized values when being unmarshaled in a separate Ruby process where the object hashes may be different
obrie
force-pushed
the
feature/marshal-compat
branch
from
December 16, 2024 18:11
fd9d7ef
to
88c879d
Compare
tycooon
reviewed
Jan 7, 2025
@tycooon I've updated the PR based on the feedback. The main thing to address is the Rubocop failure -- let me know what you'd like me to do there. |
…emoizationModule#define_memoized_method!
…in order for allow for class inheritance with memoized method overrides
I've addressed a few failures resulting from the changes in this PR:
The remaining CI failures are due to Coveralls builds (I think a CI config issue). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi there! This pull request makes 2 main changes:
I'm happy to break this apart into 2 separate PRs, but the performance improvement is mostly a side effect of other changes.
Performance improvements
The performance improvement here is mostly straightforward. Consider the current implementation for generating cache keys:
memery/lib/memery.rb
Line 83 in 9bcae5e
In this code, we will only use
method_key
if thearity
is zero. However, if we have a method like so:...then the
arity
is non-zero and we'll be required to generate a hash for the cache key. This isn't necessary because a call with no arguments should be treated identical to a call to a method that accepts no arguments. By making this change, we can improve performance by ~50%. Benchmarks below:Benchmark before
Benchmark after
Marshal improvements
In our application, we store ActiveRecord objects in Memcached, including the state for memoized fields. When we moved to
memery
, we found that these memoized fields weren't being read from correctly when unmarshaling them from Memcached. There are currently 2 pieces of code that cause issues:memery/lib/memery.rb
Line 72 in 9bcae5e
....and:
memery/lib/memery.rb
Line 83 in 9bcae5e
In these 2 pieces of code, the cache keys generated for a method call can be different across Ruby processes:
object_id
of the prepended module will be different based on the order in which classes are loaded and code is executedhash
of an object will be different (introduced in Improve performance of memoized methods with arguments by ~50% #50)In this PR, I'm proposing that we solve this by:
object_id
of the prepended module from the cache key. I can't figure out what the use case is for this, so I'm just removing it altogetherBy making these changes, I can do the following:
...then load that marshaled value into a separate Ruby process and retain the memoized values:
Notes
Hope all of that makes sense! Let me know what you think and whether you'd like me to split this out into multiple PRs. Thanks!