Skip to content

Commit 81a5652

Browse files
authored
Merge pull request #391 from ruby-protobuf/bdewitt/remove_helpers
Bdewitt/remove helpers
2 parents 21a5dd9 + a01a4d1 commit 81a5652

24 files changed

Lines changed: 6097 additions & 244 deletions

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ AllCops:
44
DisplayCopNames: true
55
Exclude:
66
- 'spec/support/protos/*.pb.rb'
7+
- 'varint_prof.rb'
78

89
Lint/EndAlignment:
910
AlignWith: keyword

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@ before_install:
1111
- java -Xmx1g -version
1212
- javac -J-Xmx1g -version
1313
- export JRUBY_OPTS=-J-Xmx1g
14-
# Required for rainbow installation issue, https://github.com/sickill/rainbow/issues/44
15-
- gem update --system
1614
- gem update bundler
1715
language: ruby
1816
rvm:
1917
- 1.9.3
2018
- 2.0.0
2119
- 2.1
2220
- 2.2
23-
- 2.2.2
2421
- 2.3
2522
- 2.4
26-
- jruby-9.1.7.0
23+
- 2.5
24+
- jruby-9.1.17.0
25+
- jruby-9.2.5.0
2726
- rbx-2
2827
env:
2928
- PROTOBUF_VERSION=2.6.1

lib/protobuf.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
require 'active_support/inflector'
1010
require 'active_support/json'
1111
require 'active_support/notifications'
12+
# Under MRI, this optimizes proto decoding by around 15% in tests.
13+
# When unavailable, we fall to pure Ruby.
14+
# rubocop:disable Lint/HandleExceptions
15+
begin
16+
require 'varint/varint'
17+
rescue LoadError
18+
end
19+
# rubocop:enable Lint/HandleExceptions
20+
# rubocop:disable Lint/HandleExceptions
21+
begin
22+
require 'protobuf_java_helpers'
23+
rescue LoadError
24+
end
25+
# rubocop:enable Lint/HandleExceptions
1226

1327
# All top-level run time code requires, ordered by necessity
1428
require 'protobuf/wire_type'

lib/protobuf/code_generator.rb

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,35 @@ def response_bytes
5050
end
5151

5252
Protobuf::Field::BaseField.module_eval do
53+
def define_set_method!
54+
end
55+
56+
def set_without_options(message_instance, bytes)
57+
return message_instance[name] = decode(bytes) unless repeated?
58+
59+
if map?
60+
hash = message_instance[name]
61+
entry = decode(bytes)
62+
# decoded value could be nil for an
63+
# enum value that is not recognized
64+
hash[entry.key] = entry.value unless entry.value.nil?
65+
return hash[entry.key]
66+
end
67+
68+
return message_instance[name] << decode(bytes) unless packed?
69+
70+
array = message_instance[name]
71+
stream = StringIO.new(bytes)
72+
73+
if wire_type == ::Protobuf::WireType::VARINT
74+
array << decode(Varint.decode(stream)) until stream.eof?
75+
elsif wire_type == ::Protobuf::WireType::FIXED64
76+
array << decode(stream.read(8)) until stream.eof?
77+
elsif wire_type == ::Protobuf::WireType::FIXED32
78+
array << decode(stream.read(4)) until stream.eof?
79+
end
80+
end
81+
5382
# Sets a MessageField that is known to be an option.
5483
# We must allow fields to be set one at a time, as option syntax allows us to
5584
# set each field within the option using a separate "option" line.
@@ -72,6 +101,7 @@ def set_with_options(message_instance, bytes)
72101

73102
set_without_options(message_instance, bytes)
74103
end
104+
alias_method :set, :set_with_options
75105

76106
def option_set(message_field, subfield, subvalue)
77107
return unless yield
@@ -85,9 +115,6 @@ def option_set(message_field, subfield, subvalue)
85115
message_field[subfield.tag] = subvalue
86116
end
87117
end
88-
89-
alias_method :set_without_options, :set
90-
alias_method :set, :set_with_options
91118
end
92119
end
93120
end

lib/protobuf/encoder.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@ module Protobuf
22
class Encoder
33
def self.encode(message, stream)
44
message.each_field_for_serialization do |field, value|
5-
if field.repeated?
6-
if field.packed?
7-
packed_value = value.map { |val| field.encode(val) }.join
8-
stream << "#{field.tag_encoded}#{::Protobuf::Field::VarintField.encode(packed_value.size)}#{packed_value}"
9-
else
10-
value.each do |val|
11-
field.encode_to_stream(val, stream)
12-
end
13-
end
14-
else
15-
field.encode_to_stream(value, stream)
16-
end
5+
field.encode_to_stream(value, stream)
176
end
187

198
stream

lib/protobuf/enum.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def self.define(name, tag)
5858
# to be incorrect; reset them
5959
@mapped_enums = @values = nil
6060
const_set(name, enum)
61+
mapped_enums
6162
end
6263

6364
# Internal: A mapping of enum number -> enums defined
@@ -132,6 +133,10 @@ def self.enum_for_tag(tag)
132133
tag && (mapped_enums[tag.to_i] || []).first
133134
end
134135

136+
def self.enum_for_tag_integer(tag)
137+
(@mapped_enums[tag] || []).first
138+
end
139+
135140
# Public: Get an Enum by a variety of type-checking mechanisms.
136141
#
137142
# candidate - An Enum, Numeric, String, or Symbol object.
@@ -153,7 +158,7 @@ def self.enum_for_tag(tag)
153158
# Returns an Enum object or nil.
154159
#
155160
def self.fetch(candidate)
156-
return enum_for_tag(candidate) if candidate.is_a?(::Integer)
161+
return enum_for_tag_integer(candidate) if candidate.is_a?(::Integer)
157162

158163
case candidate
159164
when self
@@ -308,10 +313,6 @@ def inspect
308313
"\#<Protobuf::Enum(#{parent_class})::#{name}=#{tag}>"
309314
end
310315

311-
def to_i
312-
tag
313-
end
314-
315316
def to_int
316317
tag.to_int
317318
end
@@ -352,12 +353,13 @@ def try(*args, &block)
352353
end
353354
end
354355

355-
::Protobuf.deprecator.define_deprecated_methods(self, :value => :to_i)
356-
357356
##
358357
# Instance Aliases
359358
#
360-
alias :to_hash_value to_i
361-
alias :to_json_hash_value to_i
359+
alias :to_i tag
360+
alias :to_hash_value tag
361+
alias :to_json_hash_value tag
362+
363+
::Protobuf.deprecator.define_deprecated_methods(self, :value => :to_i)
362364
end
363365
end

0 commit comments

Comments
 (0)