Skip to content

Commit 9326716

Browse files
authored
Merge pull request #356 from ruby-protobuf/abrandoned/optimize_acceptable_varint
pull in acceptable? optimization for varint and fix rubocop for previ…
2 parents 58512a2 + 709a5ee commit 9326716

5 files changed

Lines changed: 43 additions & 16 deletions

File tree

lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class File < ::Protobuf::Message; end
2727

2828

2929

30+
##
31+
# File Options
32+
#
33+
set_option :java_package, "com.google.protobuf.compiler"
34+
set_option :java_outer_classname, "PluginProtos"
35+
36+
3037
##
3138
# Message Fields
3239
#

lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ class Location
295295
repeated ::Google::Protobuf::SourceCodeInfo::Location, :location, 1
296296
end
297297

298-
299298
end
300299

301300
end

lib/protobuf/field/bool_field.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
module Protobuf
44
module Field
55
class BoolField < VarintField
6+
FALSE_ENCODE = [0].pack('C')
67
FALSE_STRING = "false".freeze
8+
TRUE_ENCODE = [1].pack('C')
79
TRUE_STRING = "true".freeze
810

911
##
@@ -19,24 +21,24 @@ def self.default
1921
# #
2022

2123
def acceptable?(val)
22-
[true, false].include?(val) || %w(true false).include?(val)
24+
val == true || val == false || val == TRUE_STRING || val == FALSE_STRING
2325
end
2426

2527
def coerce!(val)
26-
case val
27-
when String
28-
val == TRUE_STRING
29-
else
30-
val
31-
end
28+
return true if val == true
29+
return false if val == false
30+
return true if val == TRUE_STRING
31+
return false if val == FALSE_STRING
32+
33+
val
3234
end
3335

3436
def decode(value)
3537
value == 1
3638
end
3739

3840
def encode(value)
39-
[value ? 1 : 0].pack('C')
41+
value ? TRUE_ENCODE : FALSE_ENCODE
4042
end
4143

4244
private

lib/protobuf/field/varint_field.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ def self.encode(value, use_cache = true)
5050
##
5151
# Public Instance Methods
5252
#
53-
5453
def acceptable?(val)
55-
int_val = coerce!(val)
54+
int_val = if val.is_a?(Integer)
55+
return true if val >= 0 && val < INT32_MAX # return quickly for smallest integer size, hot code path
56+
val
57+
else
58+
coerce!(val)
59+
end
60+
5661
int_val >= self.class.min && int_val <= self.class.max
5762
rescue
5863
false

spec/lib/protobuf/generators/base_spec.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ def compile
111111
end
112112

113113
it 'serializes messages' do
114+
output_string = <<-STRING
115+
{ :foo => "space",
116+
:bar => [{
117+
:foo => "station",
118+
:bar => { :foo => "orbit" },
119+
:boom => 123,
120+
:goat => ::MyEnum::FOO,
121+
:bam => false,
122+
:fire => 3.5 }],
123+
:boom => 456,
124+
:goat => ::MyEnum::BOO,
125+
:bam => true, :fire => 1.2 }
126+
STRING
127+
128+
output_string.lstrip!
129+
output_string.rstrip!
130+
output_string.delete!("\n")
131+
output_string.squeeze!(" ")
114132
expect(generator.serialize_value(MyMessage3.new(
115133
:foo => 'space',
116134
:bar => [MyMessage2.new(
@@ -125,11 +143,7 @@ def compile
125143
:goat => MyEnum::BOO,
126144
:bam => true,
127145
:fire => 1.2,
128-
))).to eq(
129-
'{ :foo => "space", :bar => [{ '\
130-
':foo => "station", :bar => { :foo => "orbit" }, :boom => 123, :goat => ::MyEnum::FOO, :bam => false, :fire => 3.5 '\
131-
'}], :boom => 456, :goat => ::MyEnum::BOO, :bam => true, :fire => 1.2 }'
132-
)
146+
))).to eq(output_string)
133147
end
134148

135149
it 'serializes enums' do

0 commit comments

Comments
 (0)