From: Bryan Duxbury Date: Fri, 29 May 2009 20:18:58 +0000 (+0000) Subject: THRIFT-511. rb: Better performing hash method for generated structs X-Git-Tag: 0.2.0~94 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=2fb877a794ce3df3eb488926f991e210f419ebf6;p=common%2Fthrift.git THRIFT-511. rb: Better performing hash method for generated structs This patch uses a hash function that takes into account the hashes of struct elements, instead of just returning 0. This make hashes of Thrift structs O(1) instead of O(n). git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@780094 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index 01aae56b..dfc8a2fe 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -141,9 +141,13 @@ module Thrift self.class == other.class && self == other end - # for the time being, we're ok with a naive hash. this could definitely be improved upon. def hash - 0 + field_values = [] + each_field do |fid, field_info| + name = field_info[:name] + field_values << self.instance_variable_get("@#{name}") + end + field_values.hash end def differences(other)