enhanced items_delete_batch() in Python to avoid double list creation
authorSimone Magnani <simonemagnani.96@gmail.com>
Wed, 28 Apr 2021 10:04:52 +0000 (12:04 +0200)
committeryonghong-song <ys114321@gmail.com>
Thu, 29 Apr 2021 19:04:16 +0000 (12:04 -0700)
This commit enhances the items_delete_batch() function by accepting a ct.Array instead of a Python list.
This way, the array does not need to be re-created, allowing to directly perform the requested operation.

Signed-off-by: Simone Magnani <simonemagnani.96@gmail.com>
src/python/bcc/table.py
tests/python/test_map_batch_ops.py

index cb4adaed7468c1372e273ca8ef081a9b1681a1f1..f1a4f795e56730dad2b115221342d4c2597006b1 100644 (file)
@@ -428,26 +428,23 @@ class TableBase(MutableMapping):
         """Delete all the key-value pairs in the map if no key are given.
         In that case, it is faster to call lib.bpf_lookup_and_delete_batch than
         create keys list and then call lib.bpf_delete_batch on these keys.
-        If a list of keys is given then it deletes the related key-value.
+        If the array of keys is given then it deletes the related key-value.
         """
         if keys is not None:
-            # a list is expected
-            if type(keys) != list:
+            # a ct.Array is expected
+            if not isinstance(keys, ct.Array):
                 raise TypeError
 
             batch_size = len(keys)
-            if batch_size < 1 and batch_size > self.max_entries:
+
+            # check that batch between limits and coherent with the provided values
+            if batch_size < 1 or batch_size > self.max_entries:
                 raise KeyError
 
             count = ct.c_uint32(batch_size)
 
-            # build the array aka list of key_t that will be deleted
-            keylist = (type(self.Key()) * batch_size)()
-            for i, k in enumerate(keys):
-                keylist[i] = k
-
             res = lib.bpf_delete_batch(self.map_fd,
-                                       ct.byref(keylist),
+                                       ct.byref(keys),
                                        ct.byref(count)
                                        )
             errcode = ct.get_errno()
index 0b7419aebb7b354773e938f6fe5e6e8633b6630d..c302828ba958e4952b9a5fed2a0317176cbc6d5f 100755 (executable)
@@ -81,9 +81,9 @@ class TestMapBatch(TestCase):
         hmap = self.fill_hashmap()
         # Get 4 keys in this map.
         subset_size = 32
-        keys = [None] * subset_size
+        keys = (hmap.Key * subset_size)()
         i = 0
-        for k, v in hmap.items_lookup_batch():
+        for k, _ in hmap.items_lookup_batch():
             if i < subset_size:
                 keys[i] = k
                 i += 1