From 4e27cca12b2c39438c8e5cc23688897304c7acf5 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sun, 12 Jan 2025 17:38:50 -0800 Subject: [PATCH] nostrdb: 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 Signed-off-by: William Casarin --- nostrdb/src/nostrdb.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/nostrdb/src/nostrdb.c b/nostrdb/src/nostrdb.c index cbaf81df..bb64317b 100644 --- a/nostrdb/src/nostrdb.c +++ b/nostrdb/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)