From 03f53ed890d55b9646736ad533fa40ff7bef654b Mon Sep 17 00:00:00 2001 From: oranagra Date: Mon, 11 Dec 2017 08:56:18 +0200 Subject: [PATCH] fix #106 - reinstate python 2.6 long type looks like both the original code, and the one in the following commit: d7965253799df9850b27a8c8106e89c303041e15 assumed a simple int type is 64bit (8 bytes), and a long int is 128bit (16 bytes). this was wrong, but besides we now have the architecture flag that tells us the size of an int on the target platform. --- rdbtools/memprofiler.py | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/rdbtools/memprofiler.py b/rdbtools/memprofiler.py index d4d1719..990d231 100644 --- a/rdbtools/memprofiler.py +++ b/rdbtools/memprofiler.py @@ -1,3 +1,4 @@ +import sys import codecs from collections import namedtuple import random @@ -184,7 +185,7 @@ def set(self, key, value, expiry, info): self._current_encoding = info['encoding'] size = self.top_level_object_overhead(key, expiry) + self.sizeof_string(value) - length = element_length(value) + length = self.element_length(value) self.emit_record("string", key, size, self._current_encoding, length, length) self.end_key() @@ -202,10 +203,10 @@ def start_hash(self, key, length, expiry, info): self._current_size = size def hset(self, key, field, value): - if(element_length(field) > self._len_largest_element) : - self._len_largest_element = element_length(field) - if(element_length(value) > self._len_largest_element) : - self._len_largest_element = element_length(value) + if(self.element_length(field) > self._len_largest_element) : + self._len_largest_element = self.element_length(field) + if(self.element_length(value) > self._len_largest_element) : + self._len_largest_element = self.element_length(value) if self._current_encoding == 'hashtable': self._current_size += self.sizeof_string(field) @@ -224,8 +225,8 @@ def start_set(self, key, cardinality, expiry, info): self.start_hash(key, cardinality, expiry, info) def sadd(self, key, member): - if(element_length(member) > self._len_largest_element) : - self._len_largest_element = element_length(member) + if(self.element_length(member) > self._len_largest_element) : + self._len_largest_element = self.element_length(member) if self._current_encoding == 'hashtable': self._current_size += self.sizeof_string(member) @@ -265,8 +266,8 @@ def rpush(self, key, value): self._current_length += 1 size = self.sizeof_string(value) if type(value) != int else 4 - if(element_length(value) > self._len_largest_element): - self._len_largest_element = element_length(value) + if(self.element_length(value) > self._len_largest_element): + self._len_largest_element = self.element_length(value) if self._current_encoding == "ziplist": self._list_items_zipped_size += self.ziplist_entry_overhead(value) @@ -325,8 +326,8 @@ def start_sorted_set(self, key, length, expiry, info): self._current_size = size def zadd(self, key, score, member): - if(element_length(member) > self._len_largest_element): - self._len_largest_element = element_length(member) + if(self.element_length(member) > self._len_largest_element): + self._len_largest_element = self.element_length(member) if self._current_encoding == 'skiplist': self._current_size += 8 # score (double) @@ -489,16 +490,12 @@ def zset_random_level(self): else: return ZSKIPLIST_MAXLEVEL -MAXINT = 2**63 - 1 - - -def element_length(element): - if isinstance(element, int): - if element < - MAXINT - 1 or element > MAXINT: - return 16 - else: - return 8 - else: + def element_length(self, element): + if isinstance(element, int): + return self._long_size + if sys.version_info < (3,): + if isinstance(element, long): + return self._long_size return len(element)