ruby-changes:26378
From: tenderlove <ko1@a...>
Date: Tue, 18 Dec 2012 03:05:01 +0900 (JST)
Subject: [ruby-changes:26378] tenderlove:r38429 (trunk): * ext/psych/lib/psych/visitors/to_ruby.rb: speed up node mapping so
tenderlove 2012-12-18 03:04:50 +0900 (Tue, 18 Dec 2012) New Revision: 38429 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38429 Log: * ext/psych/lib/psych/visitors/to_ruby.rb: speed up node mapping so common cases are evaluated first. Thanks Kevin Menard! Modified files: trunk/ChangeLog trunk/ext/psych/lib/psych/visitors/to_ruby.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 38428) +++ ChangeLog (revision 38429) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Dec 18 03:03:10 2012 Aaron Patterson <aaron@t...> + + * ext/psych/lib/psych/visitors/to_ruby.rb: speed up node mapping so + common cases are evaluated first. Thanks Kevin Menard! + Tue Dec 18 02:35:00 2012 Zachary Scott <zachary@z...> * lib/optparse.rb: Remove 'developer documentation' section from rdoc Index: ext/psych/lib/psych/visitors/to_ruby.rb =================================================================== --- ext/psych/lib/psych/visitors/to_ruby.rb (revision 38428) +++ ext/psych/lib/psych/visitors/to_ruby.rb (revision 38429) @@ -141,28 +141,6 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L141 return revive_hash({}, o) unless o.tag case o.tag - when /^!(?:str|ruby\/string)(?::(.*))?/, 'tag:yaml.org,2002:str' - klass = resolve_class($1) - members = Hash[*o.children.map { |c| accept c }] - string = members.delete 'str' - - if klass - string = klass.allocate.replace string - register(o, string) - end - - init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o) - when /^!ruby\/array:(.*)$/ - klass = resolve_class($1) - list = register(o, klass.allocate) - - members = Hash[o.children.map { |c| accept c }.each_slice(2).to_a] - list.replace members['internal'] - - members['ivars'].each do |ivar, v| - list.instance_variable_set ivar, v - end - list when /^!ruby\/struct:?(.*)?$/ klass = resolve_class($1) @@ -187,6 +165,43 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L165 Struct.new(*h.map { |k,v| k.to_sym }).new(*h.map { |k,v| v }) end + when /^!ruby\/object:?(.*)?$/ + name = $1 || 'Object' + + if name == 'Complex' + h = Hash[*o.children.map { |c| accept c }] + register o, Complex(h['real'], h['image']) + elsif name == 'Rational' + h = Hash[*o.children.map { |c| accept c }] + register o, Rational(h['numerator'], h['denominator']) + else + obj = revive((resolve_class(name) || Object), o) + obj + end + + when /^!(?:str|ruby\/string)(?::(.*))?/, 'tag:yaml.org,2002:str' + klass = resolve_class($1) + members = Hash[*o.children.map { |c| accept c }] + string = members.delete 'str' + + if klass + string = klass.allocate.replace string + register(o, string) + end + + init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o) + when /^!ruby\/array:(.*)$/ + klass = resolve_class($1) + list = register(o, klass.allocate) + + members = Hash[o.children.map { |c| accept c }.each_slice(2).to_a] + list.replace members['internal'] + + members['ivars'].each do |ivar, v| + list.instance_variable_set ivar, v + end + list + when '!ruby/range' h = Hash[*o.children.map { |c| accept c }] register o, Range.new(h['begin'], h['end'], h['excl']) @@ -206,19 +221,6 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L221 end set - when '!ruby/object:Complex' - h = Hash[*o.children.map { |c| accept c }] - register o, Complex(h['real'], h['image']) - - when '!ruby/object:Rational' - h = Hash[*o.children.map { |c| accept c }] - register o, Rational(h['numerator'], h['denominator']) - - when /^!ruby\/object:?(.*)?$/ - name = $1 || 'Object' - obj = revive((resolve_class(name) || Object), o) - obj - when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/ revive_hash resolve_class($1).new, o -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/