All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Christian Göttsche" <cgzones@googlemail.com>
To: selinux@vger.kernel.org
Subject: [PATCH 4/7] libsepol: optimize ebitmap_not
Date: Tue, 12 Jul 2022 18:08:55 +0200	[thread overview]
Message-ID: <20220712160858.22677-4-cgzones@googlemail.com> (raw)
In-Reply-To: <20220712160858.22677-1-cgzones@googlemail.com>

Iterate on nodes instead of single bits to save node resolution for each
single bit.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libsepol/src/ebitmap.c | 48 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 42 insertions(+), 6 deletions(-)

diff --git a/libsepol/src/ebitmap.c b/libsepol/src/ebitmap.c
index fb20e994..6a63e559 100644
--- a/libsepol/src/ebitmap.c
+++ b/libsepol/src/ebitmap.c
@@ -101,14 +101,50 @@ int ebitmap_xor(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2)
 
 int ebitmap_not(ebitmap_t *dst, const ebitmap_t *e1, unsigned int maxbit)
 {
-	unsigned int i;
+	const ebitmap_node_t *n;
+	ebitmap_node_t *new, *prev = NULL;
+	uint32_t startbit, cur_startbit;
+	MAPTYPE map;
+
 	ebitmap_init(dst);
-	for (i=0; i < maxbit; i++) {
-		int val = ebitmap_get_bit(e1, i);
-		int rc = ebitmap_set_bit(dst, i, !val);
-		if (rc < 0)
-			return rc;
+
+	n = e1->node;
+	for (cur_startbit = 0; cur_startbit < maxbit; cur_startbit += MAPSIZE) {
+		if (n && n->startbit == cur_startbit) {
+			startbit = n->startbit;
+			map = ~n->map;
+
+			n = n->next;
+		} else {
+			startbit = cur_startbit;
+			map = ~((MAPTYPE) 0);
+		}
+
+		if (maxbit - cur_startbit < MAPSIZE)
+			map &= (((MAPTYPE)1) << (maxbit - cur_startbit)) - 1;
+
+		if (map != 0) {
+			new = malloc(sizeof(ebitmap_node_t));
+			if (!new) {
+				ebitmap_destroy(dst);
+				return -ENOMEM;
+			}
+
+			new->startbit = startbit;
+			new->map = map;
+			new->next = NULL;
+
+			if (prev)
+				prev->next = new;
+			else
+				dst->node = new;
+			prev = new;
+		}
 	}
+
+	if (prev)
+		dst->highbit = prev->startbit + MAPSIZE;
+
 	return 0;
 }
 
-- 
2.36.1


  parent reply	other threads:[~2022-07-12 16:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-12 16:08 [PATCH 1/7] libsepol/tests: add ebitmap tests Christian Göttsche
2022-07-12 16:08 ` [PATCH 2/7] libsepol: add ebitmap_init_range Christian Göttsche
2022-07-18 20:43   ` James Carter
2022-07-12 16:08 ` [PATCH 3/7] libsepol/cil: use ebitmap_init_range Christian Göttsche
2022-07-18 20:44   ` James Carter
2022-07-12 16:08 ` Christian Göttsche [this message]
2022-07-18 20:46   ` [PATCH 4/7] libsepol: optimize ebitmap_not James Carter
2022-07-12 16:08 ` [PATCH 5/7] libsepol: optimize ebitmap_and Christian Göttsche
2022-07-18 20:47   ` James Carter
2022-07-12 16:08 ` [PATCH 6/7] libsepol: optimize ebitmap_xor Christian Göttsche
2022-07-18 20:48   ` James Carter
2022-07-12 16:08 ` [PATCH 7/7] libsepol: skip superfluous memset calls in ebitmap operations Christian Göttsche
2022-07-18 20:48   ` James Carter
2022-07-18 20:39 ` [PATCH 1/7] libsepol/tests: add ebitmap tests James Carter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220712160858.22677-4-cgzones@googlemail.com \
    --to=cgzones@googlemail.com \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.