[前][次][番号順一覧][スレッド一覧]

ruby-changes:39863

From: zzak <ko1@a...>
Date: Sun, 27 Sep 2015 01:06:17 +0900 (JST)
Subject: [ruby-changes:39863] zzak:r51944 (trunk): * lib/ostruct.rb: Move method definitions for getter/setter to be lazy

zzak	2015-09-27 01:05:52 +0900 (Sun, 27 Sep 2015)

  New Revision: 51944

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51944

  Log:
    * 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 (?\194?\177 8.8%) i/s -     11.395k
       after       24.702k (?\194?\17712.8%) i/s -    122.600k
    -------------------------------------------------

  Modified files:
    trunk/ChangeLog
    trunk/lib/ostruct.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51943)
+++ ChangeLog	(revision 51944)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Sep 27 00:34:31 2015  Zachary Scott  <zzak@r...>
+
+        * 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@r...>
 
 	* lib/net/http.rb: removed unused variable. It's removed at r13648.
Index: lib/ostruct.rb
===================================================================
--- lib/ostruct.rb	(revision 51943)
+++ lib/ostruct.rb	(revision 51944)
@@ -90,7 +90,6 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L90
       hash.each_pair do |k, v|
         k = k.to_sym
         @table[k] = v
-        new_ostruct_member(k)
       end
     end
   end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]