Ruby: Use class inheritance to save memory#10281
Merged
deannagarcia merged 1 commit intoprotocolbuffers:mainfrom Aug 12, 2022
Merged
Ruby: Use class inheritance to save memory#10281deannagarcia merged 1 commit intoprotocolbuffers:mainfrom
deannagarcia merged 1 commit intoprotocolbuffers:mainfrom
Conversation
Member
|
I wish there was a way to do this that didn't affect the public API. If we do this, developers can start writing: if obj.kind_of? Google::Protobuf::AbstractMessage
# This is a protobuf
endThat might not be the worst thing, but it would commit us to maintaining this inheritance hierarchy. |
Contributor
Author
|
I can make that constant private. |
The only difference between all the `Message` classes is just their `descriptor` instance variable. So rather than create an entirely new class from scratch every time we can simply inherit from an abstract class. This shink each `Message` class from `1456` bytes to `944` bytes, and the singleton class of each from `960` to `792`, for a total of `680` bytes saved per message class, so a ~28% reduction.
6c5cacb to
7309e88
Compare
Contributor
Author
|
Done: >> require 'google/protobuf'
=> true
>> Google::Protobuf::AbstractMessage
(irb):2:in `<main>': private constant Google::Protobuf::AbstractMessage referenced (NameError) |
haberman
approved these changes
Aug 10, 2022
This was referenced Apr 25, 2023
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.
The only difference between all the
Messageclasses is just theirdescriptorinstance variable.So rather than create an entirely new class from scratch every time we can simply inherit from an abstract class.
This shink each
Messageclass from1456bytes to944bytes, and the singleton class of each from960to792, for a total of680bytes saved per message class, so a ~28% reduction.