Skip to content

Commit

Permalink
enhanced items_delete_batch() in Python to avoid double list creation
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
smagnani96 authored and yonghong-song committed Apr 29, 2021
1 parent 19df7ee commit 071f1ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 7 additions & 10 deletions src/python/bcc/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,26 +428,23 @@ def items_delete_batch(self, keys=None):
"""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()
Expand Down
4 changes: 2 additions & 2 deletions tests/python/test_map_batch_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def test_delete_batch_subset(self):
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
Expand Down

0 comments on commit 071f1ec

Please sign in to comment.