Skip to content

Commit

Permalink
Merge pull request #305 from ruby-protobuf/ah/revert_memoize_on_encode
Browse files Browse the repository at this point in the history
Remove memoization on encode
  • Loading branch information
liveh2o committed Feb 19, 2016
2 parents 2970c0d + ad4d852 commit 45a2fb1
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 69 deletions.
2 changes: 0 additions & 2 deletions lib/protobuf/field/base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ def define_array_setter

message_class.class_eval do
define_method(method_name) do |val|
@encode = nil
if val.is_a?(Array)
val = val.dup
val.compact!
Expand Down Expand Up @@ -240,7 +239,6 @@ def define_setter

message_class.class_eval do
define_method(method_name) do |val|
@encode = nil
if val.nil? || (val.respond_to?(:empty?) && val.empty?)
@values.delete(field.name)
elsif field.acceptable?(val)
Expand Down
1 change: 0 additions & 1 deletion lib/protobuf/field/bytes_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def define_setter

message_class.class_eval do
define_method(method_name) do |val|
@encode = nil
case val
when String, Symbol
@values[field.name] = "#{val}"
Expand Down
1 change: 0 additions & 1 deletion lib/protobuf/field/enum_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def define_setter
field = self
message_class.class_eval do
define_method("#{field.name}=") do |value|
@encode = nil
orig_value = value
if value.nil?
@values.delete(field.name)
Expand Down
1 change: 0 additions & 1 deletion lib/protobuf/field/message_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def define_setter
field = self
message_class.class_eval do
define_method("#{field.name}=") do |val|
@encode = nil
case
when val.nil?
@values.delete(field.name)
Expand Down
10 changes: 4 additions & 6 deletions lib/protobuf/message/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ def decode_from(stream)
# Encode this message
#
def encode
@encode ||= begin
stream = ::StringIO.new
stream.set_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
encode_to(stream)
stream.string
end
stream = ::StringIO.new
stream.set_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
encode_to(stream)
stream.string
end

# Encode this message to the given stream.
Expand Down
58 changes: 0 additions & 58 deletions spec/lib/protobuf/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,64 +268,6 @@
end
end

describe 'memoization' do
it "should memoize enum message" do
test_enum = Test::EnumTestMessage.new
test_enum.encode
expect(test_enum.instance_variable_get(:@encode)).to eq("")
test_enum.non_default_enum = 2
expect(test_enum.instance_variable_get(:@encode)).to be_nil
end

context "boolean fields" do
let(:values) { { :ext_is_searchable => true, :name => "STEPH CURRY" } }
let(:test_resource) { ::Test::Resource.new(values) }

it "should memoize after bool values change " do
test_resource.encode
expect(test_resource.instance_variable_get(:@encode)).to eq(test_resource.encode)
test_resource.ext_is_searchable = false
expect(test_resource.instance_variable_get(:@encode)).to be_nil
end
end

context "string" do
let(:values) { { :ext_is_searchable => true, :name => "STEPH CURRY" } }
let(:test_resource) { ::Test::Resource.new(values) }

it "should memoize after bool values change " do
test_resource.encode
expect(test_resource.instance_variable_get(:@encode)).to eq(test_resource.encode)
test_resource.name = "MVP"
expect(test_resource.instance_variable_get(:@encode)).to be_nil
end
end

context "string" do
let(:values) { { :ext_is_searchable => true, :name => "STEPH CURRY" } }
let(:test_resource) { ::Test::Resource.new(values) }

it "should memoize after string values change " do
test_resource.encode
expect(test_resource.instance_variable_get(:@encode)).to eq(test_resource.encode)
test_resource.name = "MVP"
expect(test_resource.instance_variable_get(:@encode)).to be_nil
end
end

context "Int64" do
let(:values) { { :name => "STEPH CURRY", :date_created => 1454712125 } }
let(:test_resource) { ::Test::Resource.new(values) }

it "should memoize after Int64 values change " do
test_resource.encode
expect(test_resource.instance_variable_get(:@encode)).to eq(test_resource.encode)
test_resource.date_created = 5554712127
expect(test_resource.instance_variable_get(:@encode)).to be_nil
end
end
end

context "when there's no value for a required field" do
let(:message) { ::Test::ResourceWithRequiredField.new }

Expand Down

0 comments on commit 45a2fb1

Please sign in to comment.