From: Kevin Clark Date: Tue, 8 Jul 2008 23:30:13 +0000 (+0000) Subject: rb: Check Thrift.type_checking earlier for a performance boost [THRIFT-55] X-Git-Tag: 0.2.0~479 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=615da249957987b39887eb12abe887ad6c883114;p=common%2Fthrift.git rb: Check Thrift.type_checking earlier for a performance boost [THRIFT-55] Author: Kevin Ballard git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@675053 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index 0535948f..d8e8f658 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -6,7 +6,7 @@ module Thrift def initialize(d={}) each_field do |fid, type, name, default| value = d.delete(name.to_s) { d.delete(name.to_sym) { default.dup rescue default } } - Thrift.check_type(value, type) + Thrift.check_type(value, type) if Thrift.type_checking instance_variable_set("@#{name}", value) end raise Exception, "Unknown keys given to #{self.class}.new: #{d.keys.join(", ")}" unless d.empty? @@ -72,7 +72,7 @@ module Thrift fields.each do |field| klass.send :attr_reader, field klass.send :define_method, "#{field}=" do |value| - Thrift.check_type(value, klass::FIELDS.values.find { |f| f[:name].to_s == field.to_s }[:type] ) + Thrift.check_type(value, klass::FIELDS.values.find { |f| f[:name].to_s == field.to_s }[:type] ) if Thrift.type_checking instance_variable_set("@#{field}", value) end end diff --git a/lib/rb/lib/thrift/types.rb b/lib/rb/lib/thrift/types.rb index d7d4cec8..6666a486 100644 --- a/lib/rb/lib/thrift/types.rb +++ b/lib/rb/lib/thrift/types.rb @@ -26,7 +26,7 @@ module Thrift end def self.check_type(value, type) - return unless Thrift.type_checking and not value.nil? + return if value.nil? klasses = case type when Types::VOID NilClass diff --git a/lib/rb/spec/types_spec.rb b/lib/rb/spec/types_spec.rb index 4c2b4eab..564d9a69 100644 --- a/lib/rb/spec/types_spec.rb +++ b/lib/rb/spec/types_spec.rb @@ -47,6 +47,7 @@ class ThriftTypesSpec < Spec::ExampleGroup end it "should be disabled when Thrift.type_checking = false" do + pending "disabled, parents should check Thrift.type_checking" Thrift.type_checking = false lambda { Thrift.check_type(3, Types::STRING) }.should_not raise_error(TypeError) end