]> arthur.barton.de Git - bup.git/commitdiff
vint: implement pack() without BytesIO
authorJohannes Berg <johannes@sipsolutions.net>
Fri, 31 Jan 2020 22:28:29 +0000 (23:28 +0100)
committerRob Browning <rlb@defaultvalue.org>
Sat, 15 May 2021 18:56:36 +0000 (13:56 -0500)
Using BytesIO here makes it slower since it resizes the
memory quite bit, using a list to collect and then joining
that list makes it faster.

Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
lib/bup/vint.py

index 8d6a39725bc8ccc093cdedec86678bcb0e9a53ed..a27204b0df268bad66f9cbcaa522ff49bc7b599d 100644 (file)
@@ -131,6 +131,10 @@ def read_bvec(port):
     return port.read(n)
 
 
+def encode_bvec(x):
+    return _helpers.vuint_encode(len(x)) + x
+
+
 def skip_bvec(port):
     port.read(read_vuint(port))
 
@@ -161,9 +165,19 @@ def recv(port, types):
     return result
 
 def pack(types, *args):
-    port = BytesIO()
-    send(port, types, *args)
-    return port.getvalue()
+    if len(types) != len(args):
+        raise Exception('number of arguments does not match format string')
+    ret = []
+    for (type, value) in zip(types, args):
+        if type == 'V':
+            ret.append(encode_vuint(value))
+        elif type == 'v':
+            ret.append(encode_vint(value))
+        elif type == 's':
+            ret.append(encode_bvec(value))
+        else:
+            raise Exception('unknown xpack format string item "' + type + '"')
+    return b''.join(ret)
 
 def unpack(types, data):
     port = BytesIO(data)