From 2c8748ccfd4feaa16206899599663ff3aac03c6a Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Fri, 8 Dec 2017 17:35:53 -0500 Subject: optimize util.crc32 (#1304) --- kafka/util.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/kafka/util.py b/kafka/util.py index de8f228..385fd56 100644 --- a/kafka/util.py +++ b/kafka/util.py @@ -12,14 +12,21 @@ from kafka.vendor import six from kafka.errors import BufferUnderflowError -def crc32(data): - crc = binascii.crc32(data) - # py2 and py3 behave a little differently - # CRC is encoded as a signed int in kafka protocol - # so we'll convert the py3 unsigned result to signed - if six.PY3 and crc >= 2**31: - crc -= 2**32 - return crc +if six.PY3: + MAX_INT = 2 ** 31 + TO_SIGNED = 2 ** 32 + + def crc32(data): + crc = binascii.crc32(data) + # py2 and py3 behave a little differently + # CRC is encoded as a signed int in kafka protocol + # so we'll convert the py3 unsigned result to signed + if crc >= MAX_INT: + crc -= TO_SIGNED + return crc +else: + def crc32(data): + return binascii.crc32(data) def write_int_string(s): -- cgit v1.2.1