Skip to content

Commit

Permalink
Minor optimization for linear search
Browse files Browse the repository at this point in the history
Add a small loop unrolling to when BGEN_MAYBELESSEQUAL is set.
  • Loading branch information
tidwall committed Dec 20, 2024
1 parent 5fbb5a1 commit da4b935
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions bgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,12 @@ static int BGEN_SYM(search_linear)(BGEN_ITEM *items, int nitems, BGEN_ITEM key,
int i = 0;
*found = 0;
#ifdef BGEN_MAYBELESSEQUAL
while (nitems-i >= 4) {
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
}
for (; i < nitems; i++) {
if (BGEN_SYM(maybelessequal)(key, items[i], udata)) {
goto compare;
Expand Down
5 changes: 3 additions & 2 deletions tests/test_spatial2x.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ static void shuffle_points(struct point *array, size_t numels) {
#define BGEN_FANOUT 4
#define BGEN_COUNTED
#define BGEN_SPATIAL
#define BGEN_ITEMRECT { point_rect(item, min, max); }
#define BGEN_COMPARE { return point_compare(a, b); }
#define BGEN_ITEMRECT { point_rect(item, min, max); }
#define BGEN_MAYBELESSEQUAL { return a.curve <= b.curve; }
#define BGEN_COMPARE { return point_compare(a, b); }
#include "../bgen.h"


Expand Down

0 comments on commit da4b935

Please sign in to comment.