fast_ostruct 1.0.0.pre.beta.1 → 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 +28 -24
- data/lib/fast_ostruct.rb +10 -10
- data/lib/tasks/benchmark.rake +22 -9
- metadata +3 -3
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
@@ -1,5 +1,8 @@
|
|
1
1
|
# Fast OpenStruct
|
2
2
|
|
3
|
+
[![FastOpenStruct CI](https://github.com/w-osilva/fast_ostruct/actions/workflows/fast-ostruct_ci.yml/badge.svg)](https://github.com/w-osilva/fast_ostruct/actions/workflows/fast-ostruct_ci.yml)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/fast_ostruct.svg)](https://badge.fury.io/rb/fast_ostruct)
|
5
|
+
|
3
6
|
It was inspirated on [DynamicClass](https://github.com/amcaplan/dynamic_class) gem.
|
4
7
|
|
5
8
|
The main purpose of this gem is behave like an OpenStruct, but with some extra powers.
|
@@ -58,60 +61,61 @@ my_struct.dig(:age) # => 30
|
|
58
61
|
Initialization benchmark
|
59
62
|
|
60
63
|
Warming up --------------------------------------
|
61
|
-
OpenStruct
|
62
|
-
FastOpenStruct
|
64
|
+
OpenStruct 6.967k i/100ms
|
65
|
+
FastOpenStruct 45.819k i/100ms
|
63
66
|
Calculating -------------------------------------
|
64
|
-
OpenStruct
|
65
|
-
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
|
66
69
|
|
67
70
|
Comparison:
|
68
|
-
FastOpenStruct:
|
69
|
-
OpenStruct:
|
71
|
+
FastOpenStruct: 454479.6 i/s
|
72
|
+
OpenStruct: 70614.1 i/s - 6.44x slower
|
70
73
|
|
71
74
|
|
72
75
|
|
73
76
|
Assignment Benchmark
|
74
77
|
|
75
78
|
Warming up --------------------------------------
|
76
|
-
OpenStruct
|
77
|
-
FastOpenStruct 1.
|
79
|
+
OpenStruct 317.626k i/100ms
|
80
|
+
FastOpenStruct 1.413M i/100ms
|
78
81
|
Calculating -------------------------------------
|
79
|
-
OpenStruct
|
80
|
-
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
|
81
84
|
|
82
85
|
Comparison:
|
83
|
-
FastOpenStruct:
|
84
|
-
OpenStruct:
|
86
|
+
FastOpenStruct: 14662985.4 i/s
|
87
|
+
OpenStruct: 3222330.4 i/s - 4.55x slower
|
85
88
|
|
86
89
|
|
87
90
|
|
88
91
|
Access Benchmark
|
89
92
|
|
90
93
|
Warming up --------------------------------------
|
91
|
-
OpenStruct
|
92
|
-
FastOpenStruct
|
94
|
+
OpenStruct 455.965k i/100ms
|
95
|
+
FastOpenStruct 1.511M i/100ms
|
93
96
|
Calculating -------------------------------------
|
94
|
-
OpenStruct
|
95
|
-
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
|
96
99
|
|
97
100
|
Comparison:
|
98
|
-
FastOpenStruct:
|
99
|
-
OpenStruct:
|
101
|
+
FastOpenStruct: 15154237.8 i/s
|
102
|
+
OpenStruct: 4533257.4 i/s - 3.34x slower
|
100
103
|
|
101
104
|
|
102
105
|
|
103
106
|
All-Together Benchmark
|
104
107
|
|
105
108
|
Warming up --------------------------------------
|
106
|
-
OpenStruct
|
107
|
-
FastOpenStruct
|
109
|
+
OpenStruct 5.084k i/100ms
|
110
|
+
FastOpenStruct 218.702k i/100ms
|
108
111
|
Calculating -------------------------------------
|
109
|
-
OpenStruct
|
110
|
-
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
|
111
114
|
|
112
115
|
Comparison:
|
113
|
-
FastOpenStruct:
|
114
|
-
OpenStruct:
|
116
|
+
FastOpenStruct: 2135322.0 i/s
|
117
|
+
OpenStruct: 52704.7 i/s - 40.51x slower
|
118
|
+
|
115
119
|
```
|
116
120
|
|
117
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
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast_ostruct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Silva
|
@@ -162,9 +162,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
162
162
|
version: '3.0'
|
163
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
164
|
requirements:
|
165
|
-
- - "
|
165
|
+
- - ">="
|
166
166
|
- !ruby/object:Gem::Version
|
167
|
-
version:
|
167
|
+
version: '0'
|
168
168
|
requirements: []
|
169
169
|
rubygems_version: 3.1.6
|
170
170
|
signing_key:
|