-
-
Notifications
You must be signed in to change notification settings - Fork 640
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
Don't hold unnecessary references to subjects in @rules_index #714
Conversation
The @rules_index ends up holding a reference to any subject that is authorized, by virtue of the hash having a default value of an empty array. This in effect facilitiates a memory leak, by which objects can't be garbage collected until the Ability instance is discarded, even though the Ability has no use for these objects. This is especially problematic for long-running background jobs that need to authorize many objects (e.g. hundreds of thousands) against a single Ability instance. By getting rid of @rule_index's default empty array value, we can allow for better garbage collection and reduce the size of the hash. These differences add up greatly when authorizing objects at scale.
Hi @coorasse, wondering if you could give a review or suggest someone who could. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot really say if this has any impact on the GC. I'd have expected a test to cover this, but I am not an expert on this. What I can say is that this code is perfectly fine for me
@coorasse Thanks for your review! Before submitting this patch I confirmed in a long-running Sidekiq job that ActiveRecord objects I pass in as subjects (e.g. On normal web requests or background tasks this change has no appreciable effect. Only when authorizing objects at scale in one go (e.g. tens or hundreds of thousands of objects) would the memory issue start to become apparent. Unfortunately I can't think of an automated test that would confirm an object can be garbage collected after being authorized, since garbage collection can't be invoked on-demand ( I've fixed the linting issue and updated the CHANGELOG. Please let me know if there is anything else you need from me in order to include this change in the next release. Thanks! |
That's really nice! Your explanation, here, as a comment will suffice. |
The @rules_index ends up holding a reference to any subject that is
authorized, by virtue of the hash having a default value of an empty
array. This in effect facilitiates a memory leak, by which objects can't
be garbage collected until the Ability instance is discarded, even
though the Ability has no use for these objects. This is especially
problematic for long-running background jobs that need to authorize many
objects (e.g. hundreds of thousands) against a single Ability instance.
By getting rid of @rule_index's default empty array value, we can allow
for better garbage collection and reduce the size of the hash. These
differences add up greatly when authorizing objects at scale.