mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/ostruct.rb: Move method definitions for getter/setter to be lazy
Patch by @sferik in [GH-1033]: https://github.com/ruby/ruby/pull/1033 Instead of defining two methods -- a reader and writer -- for each OpenStruct attribute when it is initialized, define them lazily, the first time either one is called. This adheres to the principle of "pay for use": methods that are never accessed are never defined. This optimization makes initialization an order of magnitude faster for objects with 100 attributes. In the worst-case scenario, where every attribute is accessed, performance is no worse than it is today. Benchmark --------- require 'benchmark/ips' require 'ostruct' N = 100 ATTRS = (:aa..:zz).take(N) HASH = Hash[ATTRS.map { |x| [x, x] }] def ostruct OpenStruct.new(HASH) end Benchmark.ips do |x| x.report('ostruct') { ostruct } end ------------------------------------------------- before 2.279k (± 8.8%) i/s - 11.395k after 24.702k (±12.8%) i/s - 122.600k ------------------------------------------------- git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51944 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0d71c07d49
commit
7fa2155805
2 changed files with 5 additions and 1 deletions
|
@ -1,3 +1,8 @@
|
|||
Sun Sep 27 00:34:31 2015 Zachary Scott <zzak@ruby-lang.org>
|
||||
|
||||
* lib/ostruct.rb: Move method definitions for getter/setter to be lazy
|
||||
Patch by @sferik in [GH-1033]: https://github.com/ruby/ruby/pull/1033
|
||||
|
||||
Fri Sep 25 10:07:25 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
||||
|
||||
* lib/net/http.rb: removed unused variable. It's removed at r13648.
|
||||
|
|
|
@ -90,7 +90,6 @@ class OpenStruct
|
|||
hash.each_pair do |k, v|
|
||||
k = k.to_sym
|
||||
@table[k] = v
|
||||
new_ostruct_member(k)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue