On a Thursday in 2022, Daniel P. Berrangé wrote:
The current use of an array for nwfilter objects requires
the caller to iterate over all elements to find a filter,
and also requires locking each filter.
Switching to a pair of hash tables enables O(1) lookups
both by name and uuid, with no locking required.
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
src/conf/virnwfilterobj.c | 264 +++++++++++++++++--------
src/nwfilter/nwfilter_gentech_driver.c | 8 +-
2 files changed, 179 insertions(+), 93 deletions(-)
diff --git a/src/conf/virnwfilterobj.c b/src/conf/virnwfilterobj.c
index 6bbdf6e6fa..808283e4ed 100644
--- a/src/conf/virnwfilterobj.c
+++ b/src/conf/virnwfilterobj.c
+static void
+virNWFilterObjListCount(void *payload,
+ void *key G_GNUC_UNUSED,
+ void *opaque)
+{
+ virNWFilterObj *obj = payload;
+ struct virNWFilterObjListData *data = opaque;
+ g_auto(virLockGuard) lock = virObjectLockGuard(obj);
VIR_LOCK_GUARD lock =
Otherwise clang complains about an unused variable.
+
+ if (data->filter(data->conn, obj->def))
+ data->count++;
+}
+
+
[...]
+static void
+virNWFilterObjListCopyNames(void *payload,
+ void *key G_GNUC_UNUSED,
+ void *opaque)
+{
+ virNWFilterObj *obj = payload;
+ struct virNWFilterNameData *data = opaque;
+ g_auto(virLockGuard) lock = virObjectLockGuard(obj);
+
Same here.
+ if (data->filter &&
+ !data->filter(data->conn, obj->def))
+ return;
+
+ if (data->numnames < data->maxnames) {
+ data->names[data->numnames] = g_strdup(obj->def->name);
+ data->numnames++;
+ }
+}
+
Reviewed-by: Ján Tomko <jtomko(a)redhat.com>
Jano