Reduce RubyModule/RubyClass size and creation overhead#8617
Merged
headius merged 6 commits intojruby:10-devfrom Feb 11, 2025
Merged
Reduce RubyModule/RubyClass size and creation overhead#8617headius merged 6 commits intojruby:10-devfrom
headius merged 6 commits intojruby:10-devfrom
Conversation
All classes and modules in the system are reachable via the weak subclasses collections on each class. This reduces overhead of creating a new module, class or metaclass by avoiding the extra runtime structures and reduces the overall memory footprint by N classes * ConcurrentHashMap per-object memory plus WeakReference memory and overhead.
These are only used for one method cache: initialize. This method is only used for constructing new objects of this class's type, so this is wasted overhead for everything except concrete classes. The patch here initializes it lazily since many (most?) RubyModule subclasses will never need it. We don't bother making it volatile or atomic since there's no interesting state (occasional double init will hurt nothing). Worth pointing out that with indy `new` logic, we may also never need this cache. The first call may be slow path in indy sites, but so is the first init of this cache and the first call to use it. A future commit may remove this altogether.
07d0b75 to
6796bbf
Compare
Eagerly initializing these adds memory churn to the allocation of many RubyModule types that will never even access these fields. Instead we lazily initialize them using an atomic update.
cfe5b3a to
fd326ce
Compare
Hardly any RubyModule instances will ever use these fields, so punt them into a separate data object.
fd326ce to
797546e
Compare
Member
Author
This file contains hidden or 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
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.
We have too much allocation and overhead in the construction logic for RubyModule and all subtypes. This increase overall memory footprint, slows startup, and slows down runtime class creation (especially for singleton objects). This PR will reduce that overhead by:
The goal is to have only the minimum overhead and state needed at creation time for each type of "module", so lightweight classes can be created and discarded quickly.