From 762e9bd15f84e8dc45eec60308a0519be3bdda9a Mon Sep 17 00:00:00 2001 From: Kevin Clark Date: Fri, 31 Oct 2008 00:39:20 +0000 Subject: [PATCH] rb: Performance tweaks in Struct#initialize [THRIFT-188] Author: Bryan Duxbury git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@709313 13f79535-47bb-0310-9956-ffa450edef68 --- lib/rb/lib/thrift/struct.rb | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index 6e6b8ad8..c530b33b 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -6,22 +6,31 @@ module Thrift def initialize(d={}) # get a copy of the default values to work on, removing defaults in favor of arguments fields_with_defaults = fields_with_default_values.dup - d.each_key do |name| - fields_with_defaults.delete(name.to_s) + + # check if the defaults is empty, or if there are no parameters for this + # instantiation, and if so, don't bother overriding defaults. + unless fields_with_defaults.empty? || d.empty? + d.each_key do |name| + fields_with_defaults.delete(name.to_s) + end end # assign all the user-specified arguments - d.each do |name, value| - unless name_to_id(name.to_s) - raise Exception, "Unknown key given to #{self.class}.new: #{name}" + unless d.empty? + d.each do |name, value| + unless name_to_id(name.to_s) + raise Exception, "Unknown key given to #{self.class}.new: #{name}" + end + Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking + instance_variable_set("@#{name}", value) end - Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking - instance_variable_set("@#{name}", value) end # assign all the default values - fields_with_defaults.each do |name, default_value| - instance_variable_set("@#{name}", (default_value.dup rescue default_value)) + unless fields_with_defaults.empty? + fields_with_defaults.each do |name, default_value| + instance_variable_set("@#{name}", (default_value.dup rescue default_value)) + end end end -- 2.17.1