Skip to content

Commit

Permalink
[WIP] libsepol: optimize ebitmap_cardinality()
Browse files Browse the repository at this point in the history
Make it linear in the bitmap size instead of quadratic
  • Loading branch information
fishilico committed May 26, 2018
1 parent 370e30f commit e3964cc
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion libsepol/src/ebitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,22 @@ int ebitmap_andnot(ebitmap_t *dst, ebitmap_t *e1, ebitmap_t *e2, unsigned int ma
return 0;
}

#include <assert.h>
unsigned int ebitmap_cardinality(ebitmap_t *e1)
{
unsigned int i, count = 0;
ebitmap_node_t *n;

for (n = e1->node; n; n = n->next) {
count += __builtin_popcountll(n->map);
}

/* for testing the optimisation: keep the old inefficient implementation and verify equality */
unsigned int count2 = 0;
for (i=ebitmap_startbit(e1); i < ebitmap_length(e1); i++)
if (ebitmap_get_bit(e1, i))
count++;
count2++;
assert(count2 == count);
return count;
}

Expand Down

0 comments on commit e3964cc

Please sign in to comment.