papi: vpp_serializer.py - replace slow bytes() with fast bytearray() 08/36308/7
authorViktor Velichkin <avisom@yandex.ru>
Tue, 31 May 2022 19:12:15 +0000 (22:12 +0300)
committerBeno�t Ganne <bganne@cisco.com>
Wed, 1 Jun 2022 14:36:40 +0000 (14:36 +0000)
https://docs.python.org/3/library/stdtypes.html

"if concatenating bytes objects, you can similarly use bytes.join() or io.BytesIO, or you can do in-place concatenation with a bytearray object. bytearray objects are mutable and have an efficient overallocation mechanism"

Type: improvement

Signed-off-by: Viktor Velichkin <avisom@yandex.ru>
Change-Id: Id20d337f909cce83fcd9e08e8049bb0bf5970fbc

src/vpp-api/python/vpp_papi/vpp_serializer.py

index a99e16a..d7da457 100644 (file)
@@ -262,10 +262,10 @@ class FixedList(Packer):
                     len(list), self.num
                 )
             )
-        b = bytes()
+        b = bytearray()
         for e in list:
             b += self.packer.pack(e)
-        return b
+        return bytes(b)
 
     def unpack(self, data, offset=0, result=None, ntc=False):
         # Return a list of arguments
@@ -311,10 +311,10 @@ class VLAList(Packer):
                 return b"".join(lst)
             return bytes(lst)
 
-        b = bytes()
+        b = bytearray()
         for e in lst:
             b += self.packer.pack(e)
-        return b
+        return bytes(b)
 
     def unpack(self, data, offset=0, result=None, ntc=False):
         # Return a list of arguments
@@ -355,10 +355,10 @@ class VLAList_legacy(Packer):
         if self.packer.size == 1:
             return bytes(list)
 
-        b = bytes()
+        b = bytearray()
         for e in list:
             b += self.packer.pack(e)
-        return b
+        return bytes(b)
 
     def unpack(self, data, offset=0, result=None, ntc=False):
         total = 0
@@ -627,7 +627,7 @@ class VPPType(Packer):
     def pack(self, data, kwargs=None):
         if not kwargs:
             kwargs = data
-        b = bytes()
+        b = bytearray()
 
         # Try one of the format functions
         if data and conversion_required(data, self.name):
@@ -651,7 +651,7 @@ class VPPType(Packer):
             else:
                 b += self.packers[i].pack(arg, kwargs)
 
-        return b
+        return bytes(b)
 
     def unpack(self, data, offset=0, result=None, ntc=False):
         # Return a list of arguments