From 5160d263075d28b0e3a7dbe40a7c999ba27fb3f7 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Wed, 23 Jan 2013 12:41:43 +0100 Subject: [PATCH] Significantly speed up transfers by not using the bitstring module. --- pyreactor/baseclient.py | 29 +++++++++++++++++++---------- test.py | 2 +- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/pyreactor/baseclient.py b/pyreactor/baseclient.py index 4f1e609..f13dcb6 100644 --- a/pyreactor/baseclient.py +++ b/pyreactor/baseclient.py @@ -1,6 +1,5 @@ import socket, ssl, msgpack, tempfile, os from collections import deque -from bitstring import BitArray from filelike import FileLike class BaseClient: @@ -59,17 +58,27 @@ class BaseClient: self.stream.send(chunk) def _encode_header(self, chunktype, size, channel): - header_type = BitArray(uint=chunktype, length=7) - header_size = BitArray(uint=size, length=25) - header_channel = BitArray(uint=channel, length=24) - header = header_type + header_size + header_channel - return header.bytes + bits = "" + bits += bin(chunktype)[2:].zfill(7) + bits += bin(size)[2:].zfill(25) + bits += bin(channel)[2:].zfill(24) + + header = b"" + + for i in xrange(0, 7): + header += chr(int(bits[i*8:(i+1)*8], 2)) + + return header def _decode_header(self, header): - bits = BitArray(bytes=header) # 7 byte chunk header - chunktype = bits[:7].uint # Bits 0-6 inc - chunksize = bits[7:32].uint # Bits 7-31 inc - channel = bits[32:56].uint # Bits 32-55 inc + bits = "" + + for i in xrange(0, len(header)): + bits += bin(ord(header[i]))[2:].zfill(8) + + chunktype = int(bits[:7], 2) # Bits 0-6 inc + chunksize = int(bits[7:32], 2) # Bits 7-31 inc + channel = int(bits[32:56], 2) # Bits 32-55 inc return (chunktype, chunksize, channel) diff --git a/test.py b/test.py index 801a3a5..5c9d242 100644 --- a/test.py +++ b/test.py @@ -13,4 +13,4 @@ def main(): reactor.add(c) reactor.loop() -profile.run("main()") +profile.run("main()", None, "time")