From d5f053711bd5b78f2eff035b4b287995ae286901 Mon Sep 17 00:00:00 2001 From: Joseph Sutton Date: Fri, 27 Jan 2023 08:06:47 +1300 Subject: [PATCH] ldb: Make ldb_msg_remove_attr O(n) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously it was O(n²). Signed-off-by: Joseph Sutton Reviewed-by: Andrew Bartlett --- lib/ldb/common/ldb_msg.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/ldb/common/ldb_msg.c b/lib/ldb/common/ldb_msg.c index 9cd7998e21c..4146de185d7 100644 --- a/lib/ldb/common/ldb_msg.c +++ b/lib/ldb/common/ldb_msg.c @@ -1464,11 +1464,18 @@ void ldb_msg_remove_element(struct ldb_message *msg, struct ldb_message_element */ void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr) { - struct ldb_message_element *el; + unsigned int i; + unsigned int num_del = 0; - while ((el = ldb_msg_find_element(msg, attr)) != NULL) { - ldb_msg_remove_element(msg, el); + for (i = 0; i < msg->num_elements; ++i) { + if (ldb_attr_cmp(msg->elements[i].name, attr) == 0) { + ++num_del; + } else if (num_del) { + msg->elements[i - num_del] = msg->elements[i]; + } } + + msg->num_elements -= num_del; } /*