fast_ostruct 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -24
- data/lib/fast_ostruct.rb +10 -10
- data/lib/tasks/benchmark.rake +22 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e01f31074bbe28459762b9c683d411b6449a1e4ae5e229bcfb6b31f47d5c462
|
4
|
+
data.tar.gz: 17871acbf4b3d78aad9e170e2029820a232ef266ad077f0f525a36b77724bbd0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6657426960492212688eb4976f0f81ab6404bb6e078d29c6b9eebe7fdbcbfe8299572f869b8df8b7dad2796b7e607a3989d12f240bb4ff82764d34e2e18a0e2d
|
7
|
+
data.tar.gz: 21564c9d519e0d53faaf5b0e7b136e43fafc8364da2e1828dd7809b8325fe86ae2169a3ee5cfa575a44299f23c4eb7429d4c20b475d9bce4ae00bc83031813c9
|
data/README.md
CHANGED
@@ -61,60 +61,61 @@ my_struct.dig(:age) # => 30
|
|
61
61
|
Initialization benchmark
|
62
62
|
|
63
63
|
Warming up --------------------------------------
|
64
|
-
OpenStruct
|
65
|
-
FastOpenStruct
|
64
|
+
OpenStruct 6.967k i/100ms
|
65
|
+
FastOpenStruct 45.819k i/100ms
|
66
66
|
Calculating -------------------------------------
|
67
|
-
OpenStruct
|
68
|
-
FastOpenStruct
|
67
|
+
OpenStruct 70.614k (± 4.4%) i/s - 355.317k in 5.042038s
|
68
|
+
FastOpenStruct 454.480k (± 1.6%) i/s - 2.291M in 5.042112s
|
69
69
|
|
70
70
|
Comparison:
|
71
|
-
FastOpenStruct:
|
72
|
-
OpenStruct:
|
71
|
+
FastOpenStruct: 454479.6 i/s
|
72
|
+
OpenStruct: 70614.1 i/s - 6.44x slower
|
73
73
|
|
74
74
|
|
75
75
|
|
76
76
|
Assignment Benchmark
|
77
77
|
|
78
78
|
Warming up --------------------------------------
|
79
|
-
OpenStruct
|
80
|
-
FastOpenStruct 1.
|
79
|
+
OpenStruct 317.626k i/100ms
|
80
|
+
FastOpenStruct 1.413M i/100ms
|
81
81
|
Calculating -------------------------------------
|
82
|
-
OpenStruct
|
83
|
-
FastOpenStruct
|
82
|
+
OpenStruct 3.222M (± 0.3%) i/s - 16.199M in 5.027118s
|
83
|
+
FastOpenStruct 14.663M (± 1.0%) i/s - 73.465M in 5.010767s
|
84
84
|
|
85
85
|
Comparison:
|
86
|
-
FastOpenStruct:
|
87
|
-
OpenStruct:
|
86
|
+
FastOpenStruct: 14662985.4 i/s
|
87
|
+
OpenStruct: 3222330.4 i/s - 4.55x slower
|
88
88
|
|
89
89
|
|
90
90
|
|
91
91
|
Access Benchmark
|
92
92
|
|
93
93
|
Warming up --------------------------------------
|
94
|
-
OpenStruct
|
95
|
-
FastOpenStruct
|
94
|
+
OpenStruct 455.965k i/100ms
|
95
|
+
FastOpenStruct 1.511M i/100ms
|
96
96
|
Calculating -------------------------------------
|
97
|
-
OpenStruct
|
98
|
-
FastOpenStruct
|
97
|
+
OpenStruct 4.533M (± 3.2%) i/s - 22.798M in 5.034950s
|
98
|
+
FastOpenStruct 15.154M (± 0.5%) i/s - 77.063M in 5.085386s
|
99
99
|
|
100
100
|
Comparison:
|
101
|
-
FastOpenStruct:
|
102
|
-
OpenStruct:
|
101
|
+
FastOpenStruct: 15154237.8 i/s
|
102
|
+
OpenStruct: 4533257.4 i/s - 3.34x slower
|
103
103
|
|
104
104
|
|
105
105
|
|
106
106
|
All-Together Benchmark
|
107
107
|
|
108
108
|
Warming up --------------------------------------
|
109
|
-
OpenStruct
|
110
|
-
FastOpenStruct
|
109
|
+
OpenStruct 5.084k i/100ms
|
110
|
+
FastOpenStruct 218.702k i/100ms
|
111
111
|
Calculating -------------------------------------
|
112
|
-
OpenStruct
|
113
|
-
FastOpenStruct
|
112
|
+
OpenStruct 52.705k (± 4.7%) i/s - 264.368k in 5.027437s
|
113
|
+
FastOpenStruct 2.135M (± 4.1%) i/s - 10.716M in 5.028416s
|
114
114
|
|
115
115
|
Comparison:
|
116
|
-
FastOpenStruct:
|
117
|
-
OpenStruct:
|
116
|
+
FastOpenStruct: 2135322.0 i/s
|
117
|
+
OpenStruct: 52704.7 i/s - 40.51x slower
|
118
|
+
|
118
119
|
```
|
119
120
|
|
120
121
|
## Contributing
|
data/lib/fast_ostruct.rb
CHANGED
@@ -4,7 +4,7 @@ require 'json'
|
|
4
4
|
require 'set'
|
5
5
|
|
6
6
|
class FastOpenStruct
|
7
|
-
VERSION = '1.0.
|
7
|
+
VERSION = '1.0.1'
|
8
8
|
|
9
9
|
INITIALIZE_OPTIONS = { deep_initialize: true }.freeze
|
10
10
|
ATTRIBUTES_OPTIONS = { symbolize_keys: false }.freeze
|
@@ -41,12 +41,12 @@ class FastOpenStruct
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def to_json(*_args)
|
44
|
-
attributes
|
44
|
+
JSON.generate(attributes)
|
45
45
|
end
|
46
46
|
alias serializable_hash attributes
|
47
47
|
alias to_h attributes
|
48
48
|
|
49
|
-
#
|
49
|
+
# Hasheable
|
50
50
|
def [](name)
|
51
51
|
attribute_get(name)
|
52
52
|
end
|
@@ -104,10 +104,10 @@ class FastOpenStruct
|
|
104
104
|
@defined_methods ||= Set.new
|
105
105
|
end
|
106
106
|
|
107
|
-
def
|
107
|
+
def define_methods!(name)
|
108
108
|
class_exec do
|
109
|
-
|
110
|
-
|
109
|
+
attr_accessor name
|
110
|
+
|
111
111
|
defined_methods << name
|
112
112
|
end
|
113
113
|
end
|
@@ -126,16 +126,16 @@ class FastOpenStruct
|
|
126
126
|
return if name.nil?
|
127
127
|
|
128
128
|
name = name.to_sym
|
129
|
-
self.class.
|
130
|
-
instance_variable_set("@#{name}", value)
|
129
|
+
self.class.define_methods!(name) unless self.class.defined_methods.include?(name)
|
130
|
+
instance_variable_set(:"@#{name}", value)
|
131
131
|
end
|
132
132
|
|
133
133
|
def attribute_get(name)
|
134
|
-
instance_variable_get("@#{name}")
|
134
|
+
instance_variable_get(:"@#{name}")
|
135
135
|
end
|
136
136
|
|
137
137
|
def attribute_unset(name)
|
138
|
-
remove_instance_variable("@#{name}")
|
138
|
+
remove_instance_variable(:"@#{name}")
|
139
139
|
end
|
140
140
|
|
141
141
|
def method_missing(mid, *args)
|
data/lib/tasks/benchmark.rake
CHANGED
@@ -10,7 +10,7 @@ namespace :fast_open_struct do
|
|
10
10
|
task :benchmark do
|
11
11
|
puts "Initialization benchmark\n\n"
|
12
12
|
Benchmark.ips do |x|
|
13
|
-
input_hash = { foo: :bar }
|
13
|
+
input_hash = { foo: :bar, bar: :baz, baz: :foo }
|
14
14
|
|
15
15
|
x.report('OpenStruct') do
|
16
16
|
OpenStruct.new(input_hash)
|
@@ -23,47 +23,60 @@ namespace :fast_open_struct do
|
|
23
23
|
|
24
24
|
puts "\n\nAssignment Benchmark\n\n"
|
25
25
|
Benchmark.ips do |x|
|
26
|
-
|
27
|
-
|
28
|
-
fos = FastOpenStruct.new(input_hash)
|
26
|
+
os = OpenStruct.new
|
27
|
+
fos = FastOpenStruct.new
|
29
28
|
|
30
29
|
x.report('OpenStruct') do
|
31
30
|
os.foo = :bar
|
31
|
+
os.bar = :baz
|
32
|
+
os.baz = :foo
|
32
33
|
end
|
33
34
|
x.report('FastOpenStruct') do
|
34
35
|
fos.foo = :bar
|
36
|
+
fos.bar = :baz
|
37
|
+
fos.baz = :foo
|
35
38
|
end
|
36
39
|
x.compare!
|
37
40
|
end
|
38
41
|
|
39
42
|
puts "\n\nAccess Benchmark\n\n"
|
40
43
|
Benchmark.ips do |x|
|
41
|
-
input_hash = { foo: :bar }
|
44
|
+
input_hash = { foo: :bar, bar: :baz, baz: :foo }
|
42
45
|
os = OpenStruct.new(input_hash)
|
43
46
|
fos = FastOpenStruct.new(input_hash)
|
44
47
|
|
45
48
|
x.report('OpenStruct') do
|
46
49
|
os.foo
|
50
|
+
os.bar
|
51
|
+
os.baz
|
47
52
|
end
|
48
53
|
x.report('FastOpenStruct') do
|
49
54
|
fos.foo
|
55
|
+
fos.bar
|
56
|
+
fos.baz
|
50
57
|
end
|
51
58
|
x.compare!
|
52
59
|
end
|
53
60
|
|
54
61
|
puts "\n\nAll-Together Benchmark\n\n"
|
55
62
|
Benchmark.ips do |x|
|
56
|
-
input_hash = { foo: :bar }
|
57
|
-
|
58
63
|
x.report('OpenStruct') do
|
59
|
-
os = OpenStruct.new
|
64
|
+
os = OpenStruct.new
|
60
65
|
os.foo = :bar
|
61
66
|
os.foo
|
67
|
+
os.bar = :baz
|
68
|
+
os.bar
|
69
|
+
os.baz = :foo
|
70
|
+
os.baz
|
62
71
|
end
|
63
72
|
x.report('FastOpenStruct') do
|
64
|
-
fos = FastOpenStruct.new
|
73
|
+
fos = FastOpenStruct.new
|
65
74
|
fos.foo = :bar
|
66
75
|
fos.foo
|
76
|
+
fos.bar = :baz
|
77
|
+
fos.bar
|
78
|
+
fos.baz = :foo
|
79
|
+
fos.baz
|
67
80
|
end
|
68
81
|
x.compare!
|
69
82
|
end
|