commit f7aac3215575f2f6089ce7ac91da36347fb83b79
parent 51c9f8c41bdffbffe7d784f24429dca6f6fb6cd4
Author: William Casarin <jb55@jb55.com>
Date: Sun, 12 Jan 2025 17:38:50 -0800
filter: introduce ndb_filter_init_with
Just a static function for now for creating smaller filter sizes. We
will use this for filters that we know are small so that we don't have
to allocate so many pages at once. It's likely the OS will only allocate
a single page anyways, but its nice to be explicit.
Changelog-Added: Add ndb_filter_init_with
Diffstat:
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/src/nostrdb.c b/src/nostrdb.c
@@ -676,15 +676,15 @@ ndb_filter_get_int_element(const struct ndb_filter_elements *els, int index)
return els->elements[index];
}
-int ndb_filter_init(struct ndb_filter *filter)
+static int ndb_filter_init_with(struct ndb_filter *filter, int pages)
{
struct cursor cur;
int page_size, elem_pages, data_pages, buf_size;
page_size = 4096; // assuming this, not a big deal if we're wrong
- elem_pages = NDB_FILTER_PAGES / 4;
- data_pages = NDB_FILTER_PAGES - elem_pages;
- buf_size = page_size * NDB_FILTER_PAGES;
+ elem_pages = pages / 4;
+ data_pages = pages - elem_pages;
+ buf_size = page_size * pages;
unsigned char *buf = malloc(buf_size);
if (!buf)
@@ -710,6 +710,10 @@ int ndb_filter_init(struct ndb_filter *filter)
return 1;
}
+int ndb_filter_init(struct ndb_filter *filter) {
+ return ndb_filter_init_with(filter, NDB_FILTER_PAGES);
+}
+
void ndb_filter_destroy(struct ndb_filter *filter)
{
if (filter->elem_buf.start)