Ruby: Use class inheritance to save memory
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.
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
end
That might not be the worst thing, but it would commit us to maintaining this inheritance hierarchy.
I can make that constant private.
Done:
>> require 'google/protobuf'
=> true
>> Google::Protobuf::AbstractMessage
(irb):2:in `<main>': private constant Google::Protobuf::AbstractMessage referenced (NameError)