Ruby: Call "Class#new" over rb_class_new_instance in decoding #7352
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.
This patch has almost no change in behaviour where users have not
patched the implementation of new on either a specific proto object
class, or
Google::Protobuf::MessageExts::ClassMethods
. The defaultimplementation of
new
, andrb_class_new_instance
have the samebehaviour.
By default when we call
new
on a class in Ruby, it goes to theClass
class's implementation:
the
Class
implementation ofnew
is (pseudocode, it's actually in c):rb_class_new_instance
does the same thing, it calls down torb_class_s_new
,which calls
rb_class_alloc
, thenrb_obj_call_init
.rb_funcall
is a variadic c function for calling a ruby method on an object,it takes:
VALUE
on to which the method should be calledID
which should be an ID of a method, usually created withrb_intern
,to get an ID from a string
VALUE
s, to send to the method call.rb_funcall
is the same as calling a method directly in Ruby, and will performancestor chain respecting method lookup.
This means that in C extensions, if nobody has defined the
new
method on anyclasses or modules in a class's inheritance chain calling
rb_class_new_instance
is the same as callingrb_funcall(klass, rb_intern("new"))
, however this removes the ability for users to define ormonkey patch their own constructors in to the objects created by the C
extension.
In Ads, we define
new
onGoogle::Protobuf::MessageExts::ClassMethods
to allow us to insert amonkeypatch which makes it possible to assign primitive values to wrapper type
fields (e.g. Google::Protobuf::StringValue). The monkeypatch we apply works for
objects that we create for the user via the
new
method. Before this commit,however, the patch does not work for the
decode
method, for the reasonsoutlined above. Before this commit, protobuf uses
rb_class_new_instance
.After this commit, we use
rb_funcall(klass, rb_intern("new"), 0);
to constructprotobuf objects during decoding. While I haven't measured it this will have
a very minor performance impact for decoding (
rb_funcall
will have to go to themethod cache, which
rb_class_new_instance
will not). This does however dothe "more rubyish" thing of respecting the protobuf object's inheritance chain
to construct them during decoding. What this means practically is that the monkeypatch where we override new works for the ads library in decoding. Other users could override new on
Google::Protobuf::MessageExts::ClassMethods
to introduce their own behavioursI have run both Ads and Cloud's test suites for Ruby libraries against this
patch, as well as the protobuf Ruby gem's test suite locally. I also tested what happens if a user does not return the right object type from their overriden new method. A reasonable exception is produced:
TypeError: wrong argument type Object (expected Message)
, and no crash occurs in the C code.