rb: Check Thrift.type_checking earlier for a performance boost [THRIFT-55]
Author: Kevin Ballard <kevin@rapleaf.com>
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 0535948..d8e8f65 100644
--- a/lib/rb/lib/thrift/struct.rb
+++ b/lib/rb/lib/thrift/struct.rb
@@ -6,7 +6,7 @@
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 @@
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 d7d4cec..6666a48 100644
--- a/lib/rb/lib/thrift/types.rb
+++ b/lib/rb/lib/thrift/types.rb
@@ -26,7 +26,7 @@
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 4c2b4ea..564d9a6 100644
--- a/lib/rb/spec/types_spec.rb
+++ b/lib/rb/spec/types_spec.rb
@@ -47,6 +47,7 @@
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