From 4654d75ea31041d5ab90a4ead43b53743f20f391 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Mon, 17 Dec 2012 18:04:50 +0000 Subject: [PATCH] * ext/psych/lib/psych/visitors/to_ruby.rb: speed up node mapping so common cases are evaluated first. Thanks Kevin Menard! git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38429 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++ ext/psych/lib/psych/visitors/to_ruby.rb | 72 +++++++++++++------------ 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2c5b0bf54c..2771f94544 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Dec 18 03:03:10 2012 Aaron Patterson + + * 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 * lib/optparse.rb: Remove 'developer documentation' section from rdoc diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb index c6cf018888..725f1f9f5d 100644 --- a/ext/psych/lib/psych/visitors/to_ruby.rb +++ b/ext/psych/lib/psych/visitors/to_ruby.rb @@ -141,28 +141,6 @@ module Psych 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 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 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