Skip to content

Commit f093db6

Browse files
committed
Format once with stree to avoid large diffs
1 parent cb0fd48 commit f093db6

File tree

7 files changed

+324
-228
lines changed

7 files changed

+324
-228
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ require "rspec/core/rake_task"
33

44
RSpec::Core::RakeTask.new(:spec)
55

6-
task :default => :spec
6+
task default: :spec

bin/benchmark.rb

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
require 'securerandom'
2-
require 'benchmark'
3-
require 'msgpack'
4-
require 'yaml'
5-
require 'json'
6-
require 'protobuf_nested_struct'
1+
require "securerandom"
2+
require "benchmark"
3+
require "msgpack"
4+
require "yaml"
5+
require "json"
6+
require "protobuf_nested_struct"
77

88
BIG_NUMBER = 100_000
9-
AVG_NUMBER = 10_000
9+
AVG_NUMBER = 10_000
1010
PRNS = ProtobufNestedStruct
1111

1212
MessagePack.singleton_class.class_eval do
@@ -15,26 +15,10 @@
1515
end
1616

1717
primitives = [
18-
{
19-
value: nil,
20-
description: "nil",
21-
count: BIG_NUMBER,
22-
},
23-
{
24-
value: 782,
25-
description: "small int",
26-
count: BIG_NUMBER,
27-
},
28-
{
29-
value: "whoa, what's up",
30-
description: "short string",
31-
count: BIG_NUMBER,
32-
},
33-
{
34-
value: 2.5,
35-
description: "float",
36-
count: BIG_NUMBER,
37-
},
18+
{ value: nil, description: "nil", count: BIG_NUMBER },
19+
{ value: 782, description: "small int", count: BIG_NUMBER },
20+
{ value: "whoa, what's up", description: "short string", count: BIG_NUMBER },
21+
{ value: 2.5, description: "float", count: BIG_NUMBER },
3822
{
3923
value: Date.new(2018, 11, 11),
4024
description: "date",
@@ -46,26 +30,29 @@
4630
description: "time",
4731
count: BIG_NUMBER,
4832
exclude: [MessagePack]
49-
},
33+
}
5034
]
5135

52-
arrays = primitives.map do |spec|
53-
spec = spec.dup
54-
spec[:value] = [spec[:value]]*BIG_NUMBER
55-
spec[:description] = "array of #{spec[:description]}"
56-
spec[:count] = 10
57-
spec
58-
end
36+
arrays =
37+
primitives.map do |spec|
38+
spec = spec.dup
39+
spec[:value] = [spec[:value]] * BIG_NUMBER
40+
spec[:description] = "array of #{spec[:description]}"
41+
spec[:count] = 10
42+
spec
43+
end
5944

60-
hashes = primitives.map do |spec|
61-
spec = spec.dup
62-
spec[:value] = BIG_NUMBER.times.each.with_object({}) do |i, obj|
63-
obj[SecureRandom.hex(5)] = spec[:value]
45+
hashes =
46+
primitives.map do |spec|
47+
spec = spec.dup
48+
spec[:value] = BIG_NUMBER
49+
.times
50+
.each
51+
.with_object({}) { |i, obj| obj[SecureRandom.hex(5)] = spec[:value] }
52+
spec[:description] = "hash, values of #{spec[:description]}"
53+
spec[:count] = 10
54+
spec
6455
end
65-
spec[:description] = "hash, values of #{spec[:description]}"
66-
spec[:count] = 10
67-
spec
68-
end
6956

7057
tests = primitives + arrays + hashes
7158

@@ -74,22 +61,26 @@
7461
count = job.fetch(:count)
7562

7663
Benchmark.bm do |x|
77-
[YAML, JSON, MessagePack, PRNS].reject{|x| job.fetch(:exclude, []).include?(x) }.each do |serializer|
78-
x.report("#{serializer.name.rjust(20)}: #{job[:description].rjust(30)} serialization") do
79-
count.times { serializer.dump(value) }
64+
[YAML, JSON, MessagePack, PRNS].reject do |x|
65+
job.fetch(:exclude, []).include?(x)
66+
end
67+
.each do |serializer|
68+
x.report(
69+
"#{serializer.name.rjust(20)}: #{job[:description].rjust(30)} serialization"
70+
) { count.times { serializer.dump(value) } }
8071
end
81-
end
8272
end
8373

8474
Benchmark.bm do |x|
85-
[YAML, JSON, MessagePack, PRNS].reject{|x| job.fetch(:exclude, []).include?(x) }.each do |serializer|
86-
serialized = serializer.dump(value)
75+
[YAML, JSON, MessagePack, PRNS].reject do |x|
76+
job.fetch(:exclude, []).include?(x)
77+
end
78+
.each do |serializer|
79+
serialized = serializer.dump(value)
8780

88-
x.report("#{serializer.name.rjust(20)}: #{job[:description].rjust(30)} deserialization") do
89-
count.times { serializer.load(serialized) }
81+
x.report(
82+
"#{serializer.name.rjust(20)}: #{job[:description].rjust(30)} deserialization"
83+
) { count.times { serializer.load(serialized) } }
9084
end
91-
end
9285
end
93-
9486
end
95-

lib/google/type/date_pb.rb

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/protobuf_nested_struct.rb

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
require 'date'
2-
require 'google/protobuf'
3-
require 'google/protobuf/well_known_types'
1+
require "date"
2+
require "google/protobuf"
3+
require "google/protobuf/well_known_types"
44

5-
require 'protobuf_nested_struct/version'
6-
require 'protobuf_nested_struct/struct_pb'
5+
require "protobuf_nested_struct/version"
6+
require "protobuf_nested_struct/struct_pb"
77

88
module ProtobufNestedStruct
99
class Value
@@ -20,54 +20,56 @@ def self.load(string)
2020

2121
def from_ruby(obj)
2222
case obj
23-
when nil
24-
self.null_value = 0
25-
when Integer
26-
self.int_value = obj
27-
when Float
28-
self.double_value = obj
29-
when String
30-
self.string_value = obj
31-
when TrueClass, FalseClass
32-
self.bool_value = obj
33-
when Date
34-
self.date_value = Google::Type::Date.new(day: obj.day, month: obj.month, year: obj.year)
35-
when Time
36-
self.timestamp_value = Google::Protobuf::Timestamp.new.tap{|gpt| gpt.from_time(obj) }
37-
when Hash
38-
self.string_map_value = HashMapStringValue.new.tap{|ps| ps.from_ruby(obj) }
39-
when Array
40-
self.list_value = ListValue.new.tap{|ps| ps.from_ruby(obj) }
41-
else
42-
raise ArgumentError, "not allowed: #{obj.inspect}"
23+
when nil
24+
self.null_value = 0
25+
when Integer
26+
self.int_value = obj
27+
when Float
28+
self.double_value = obj
29+
when String
30+
self.string_value = obj
31+
when TrueClass, FalseClass
32+
self.bool_value = obj
33+
when Date
34+
self.date_value =
35+
Google::Type::Date.new(day: obj.day, month: obj.month, year: obj.year)
36+
when Time
37+
self.timestamp_value =
38+
Google::Protobuf::Timestamp.new.tap { |gpt| gpt.from_time(obj) }
39+
when Hash
40+
self.string_map_value =
41+
HashMapStringValue.new.tap { |ps| ps.from_ruby(obj) }
42+
when Array
43+
self.list_value = ListValue.new.tap { |ps| ps.from_ruby(obj) }
44+
else
45+
raise ArgumentError, "not allowed: #{obj.inspect}"
4346
end
4447
end
4548

4649
def to_ruby
4750
case kind
48-
when :null_value
49-
nil
50-
when :int_value
51-
int_value
52-
when :double_value
53-
double_value
54-
when :string_value
55-
string_value
56-
when :bool_value
57-
bool_value
58-
when :date_value
59-
Date.new(date_value.year, date_value.month, date_value.day)
60-
when :timestamp_value
61-
timestamp_value.to_time
62-
when :string_map_value
63-
string_map_value.to_ruby
64-
when :list_value
65-
list_value.to_ruby
66-
else
67-
raise ArgumentError
51+
when :null_value
52+
nil
53+
when :int_value
54+
int_value
55+
when :double_value
56+
double_value
57+
when :string_value
58+
string_value
59+
when :bool_value
60+
bool_value
61+
when :date_value
62+
Date.new(date_value.year, date_value.month, date_value.day)
63+
when :timestamp_value
64+
timestamp_value.to_time
65+
when :string_map_value
66+
string_map_value.to_ruby
67+
when :list_value
68+
list_value.to_ruby
69+
else
70+
raise ArgumentError
6871
end
6972
end
70-
7173
end
7274

7375
class HashMapStringValue
@@ -112,13 +114,11 @@ def self.load(string)
112114
def from_ruby(obj)
113115
Array === obj or raise ArgumentError
114116
values.clear
115-
obj.each do |value|
116-
values << Value.new.tap{|v| v.from_ruby(value) }
117-
end
117+
obj.each { |value| values << Value.new.tap { |v| v.from_ruby(value) } }
118118
end
119119

120120
def to_ruby
121-
values.map{|v| v.to_ruby }
121+
values.map { |v| v.to_ruby }
122122
end
123123
end
124124

@@ -129,4 +129,4 @@ def self.dump(obj)
129129
def self.load(string)
130130
Value.load(string)
131131
end
132-
end
132+
end

lib/protobuf_nested_struct/struct_pb.rb

Lines changed: 27 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)