-
Notifications
You must be signed in to change notification settings - Fork 101
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
Abrandoned/decoding setters without copy #297
base: master
Are you sure you want to change the base?
Changes from 2 commits
22909fb
7637d60
e06f642
d096a27
0a9bfbb
481e730
476844f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,20 @@ def decode(value) | |
value == 1 | ||
end | ||
|
||
def define_decode_setter | ||
field = self | ||
name_method_name = "_protobuf_decode_setter_#{field.name}" | ||
tag_method_name = "_protobuf_decode_setter_#{field.tag}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this private, |
||
|
||
message_class.class_eval do | ||
define_method(name_method_name) do |val| | ||
@values[field.name] = field.decode(val) | ||
end | ||
|
||
alias_method tag_method_name, name_method_name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can take out the name versions; I was originally just looking for a tag version so decoding doesn't need to lookup the field; will change this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome, I think that this will help avoid some extension vs field issues later down the line as well, since tags are 100% unambiguous |
||
end | ||
end | ||
|
||
def encode(value) | ||
[value ? 1 : 0].pack('C') | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ def self.encode(value, use_cache = true) | |
# | ||
|
||
def acceptable?(val) | ||
return true if val.is_a?(Integer) && val >= 0 && val < INT32_MAX | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we still want to compare to I understand wanting to avoid Just an idea def acceptable?(val)
int_val = val.is_a?(Integer)
val
else
coerce!(val)
end
int_val >= self.class.min && int_val <= self.class.max
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we do want to compare the max (which happens after this if it is larger than the smallest max); the smallest max is the INT32_MAX, so if it passes it (and is an Integer; which is the most common case, especially in deserialization) then we just set it instead of taking the long route (which in our usage is very uncommon) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess as a reader of this patch, I'm having a hard following what it's supposed to do. I didn't realize it would fall through to the old case. Maybe we could do something like this (comments included) for posterity? def acceptable?(val)
int_val = if val.is_a?(Integer)
return true if val >= 0 && val < INT32_MAX # return quickly for smallest integer size, hot code path
val
else
coerce!(val)
end
int_val >= self.class.min && int_val <= self.class.max
end |
||
int_val = coerce!(val) | ||
int_val >= self.class.min && int_val <= self.class.max | ||
rescue | ||
|
@@ -63,6 +64,20 @@ def coerce!(val) | |
Integer(val, 10) | ||
end | ||
|
||
def define_decode_setter | ||
field = self | ||
name_method_name = "_protobuf_decode_setter_#{field.name}" | ||
tag_method_name = "_protobuf_decode_setter_#{field.tag}" | ||
|
||
message_class.class_eval do | ||
define_method(name_method_name) do |val| | ||
@values[field.name] = val | ||
end | ||
|
||
alias_method tag_method_name, name_method_name | ||
end | ||
end | ||
|
||
def decode(value) | ||
value | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,8 +77,7 @@ def each_field | |
return to_enum(:each_field) unless block_given? | ||
|
||
self.class.all_fields.each do |field| | ||
value = __send__(field.getter) | ||
yield(field, value) | ||
yield(field, __send__(field.getter)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After #302 it would be the style to use |
||
end | ||
end | ||
|
||
|
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.
Only a few variants of this method. Let's bring the duplicated common implementation here.
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.
probably best, I wrote this while I was testing the string copy only and now need to refactor since a common pattern has emerged
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.
👍