diff options
author | Ofek Lev <ofekmeister@gmail.com> | 2017-12-08 17:35:53 -0500 |
---|---|---|
committer | Dana Powers <dana.powers@gmail.com> | 2017-12-08 14:35:53 -0800 |
commit | 2c8748ccfd4feaa16206899599663ff3aac03c6a (patch) | |
tree | 150908fe40fc1577d31cb8c9e7272f6849103eb0 | |
parent | 009290ddd5d4616d70bff93f841e773af8b22750 (diff) | |
download | kafka-python-2c8748ccfd4feaa16206899599663ff3aac03c6a.tar.gz |
optimize util.crc32 (#1304)
-rw-r--r-- | kafka/util.py | 23 |
1 files 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): |