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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a550fb9fdce2464d1b4f156eee3538ff7c214b53da5d704a2a54c326433e2fc
4
- data.tar.gz: e11bde8d2b58a8cf595352a4d814364a2f08b51b5f33eb8404f6a8c2428c14e8
3
+ metadata.gz: 8e01f31074bbe28459762b9c683d411b6449a1e4ae5e229bcfb6b31f47d5c462
4
+ data.tar.gz: 17871acbf4b3d78aad9e170e2029820a232ef266ad077f0f525a36b77724bbd0
5
5
  SHA512:
6
- metadata.gz: 5d7e83a8427f638bbc0c75c099a85c29cd9f2f8164fa3b2b0334c60409b9fa0a4bc0c0975974bee51db0b29392232c6f90aa53eb0489727bc5552351c6764a3f
7
- data.tar.gz: 3306b542370c0ab7d0fa41a3e2ad1b4c5653f70dbb024c2b373f0224412f5e4e22dd6a1e615c5f37178ea07309a9e88c38d5521d201dec9a7e185ee3636a6fba
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 15.041k i/100ms
65
- FastOpenStruct 116.264k i/100ms
64
+ OpenStruct 6.967k i/100ms
65
+ FastOpenStruct 45.819k i/100ms
66
66
  Calculating -------------------------------------
67
- OpenStruct 152.606k2.4%) i/s - 767.091k in 5.029398s
68
- FastOpenStruct 1.148M (± 1.4%) i/s - 5.813M in 5.063585s
67
+ OpenStruct 70.614k4.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: 1148278.4 i/s
72
- OpenStruct: 152606.3 i/s - 7.52x slower
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 682.010k i/100ms
80
- FastOpenStruct 1.972M i/100ms
79
+ OpenStruct 317.626k i/100ms
80
+ FastOpenStruct 1.413M i/100ms
81
81
  Calculating -------------------------------------
82
- OpenStruct 6.980M3.3%) i/s - 35.465M in 5.086554s
83
- FastOpenStruct 19.964M (± 0.8%) i/s - 100.582M in 5.038516s
82
+ OpenStruct 3.222M0.3%) i/s - 16.199M in 5.027118s
83
+ FastOpenStruct 14.663M1.0%) i/s - 73.465M in 5.010767s
84
84
 
85
85
  Comparison:
86
- FastOpenStruct: 19964034.5 i/s
87
- OpenStruct: 6980499.3 i/s - 2.86x slower
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 1.005M i/100ms
95
- FastOpenStruct 2.185M i/100ms
94
+ OpenStruct 455.965k i/100ms
95
+ FastOpenStruct 1.511M i/100ms
96
96
  Calculating -------------------------------------
97
- OpenStruct 10.085M0.5%) i/s - 51.245M in 5.081712s
98
- FastOpenStruct 21.798M1.1%) i/s - 109.268M in 5.013290s
97
+ OpenStruct 4.533M3.2%) i/s - 22.798M in 5.034950s
98
+ FastOpenStruct 15.154M0.5%) i/s - 77.063M in 5.085386s
99
99
 
100
100
  Comparison:
101
- FastOpenStruct: 21798290.7 i/s
102
- OpenStruct: 10084528.1 i/s - 2.16x slower
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 12.918k i/100ms
110
- FastOpenStruct 111.460k i/100ms
109
+ OpenStruct 5.084k i/100ms
110
+ FastOpenStruct 218.702k i/100ms
111
111
  Calculating -------------------------------------
112
- OpenStruct 127.875k2.7%) i/s - 645.900k in 5.054749s
113
- FastOpenStruct 1.122M1.1%) i/s - 5.684M in 5.066350s
112
+ OpenStruct 52.705k4.7%) i/s - 264.368k in 5.027437s
113
+ FastOpenStruct 2.135M4.1%) i/s - 10.716M in 5.028416s
114
114
 
115
115
  Comparison:
116
- FastOpenStruct: 1122147.0 i/s
117
- OpenStruct: 127874.6 i/s - 8.78x slower
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.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.to_json
44
+ JSON.generate(attributes)
45
45
  end
46
46
  alias serializable_hash attributes
47
47
  alias to_h attributes
48
48
 
49
- # Accessible
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 define_method!(name)
107
+ def define_methods!(name)
108
108
  class_exec do
109
- attr_writer name unless method_defined?("#{name}=")
110
- attr_reader name unless method_defined?(name)
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.define_method!(name) unless self.class.defined_methods.include?(name)
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)
@@ -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
- input_hash = { foo: :bar }
27
- os = OpenStruct.new(input_hash)
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(input_hash)
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(input_hash)
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.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Washington Silva