commit c677233dcb438449575158efe86b6b4c95feffff
parent d063362bd79d798784c1d9a14596dc3786671d65
Author: William Casarin <jb55@jb55.com>
Date: Sat, 30 Dec 2023 19:19:15 -0800
nostrdb/blocks: expose block iterator internals
so we don't need heap allocation. we will be calling this a lot in tight
render loops, we don't want to be allocating on each frame.
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
5 files changed, 50 insertions(+), 56 deletions(-)
diff --git a/nostrdb/src/block.c b/nostrdb/src/block.c
@@ -4,13 +4,6 @@
#include "block.h"
#include <stdlib.h>
-struct ndb_block_iterator {
- const char *content;
- struct ndb_blocks *blocks;
- struct ndb_block block;
- struct cursor cur;
-};
-
int push_str_block(struct cursor *buf, const char *content, struct ndb_str_block *block) {
return cursor_push_varint(buf, block->str - content) &&
cursor_push_varint(buf, block->len);
@@ -133,32 +126,25 @@ enum ndb_block_type ndb_get_block_type(struct ndb_block *block) {
}
// BLOCK ITERATORS
-struct ndb_block_iterator *ndb_blocks_iterate_start(const char *content, struct ndb_blocks *blocks) {
- struct ndb_block_iterator *iter = malloc(sizeof(*iter));
- if (!iter)
- return NULL;
-
+void ndb_blocks_iterate_start(const char *content, struct ndb_blocks *blocks, struct ndb_block_iterator *iter) {
iter->blocks = blocks;
iter->content = content;
-
- make_cursor((unsigned char *)blocks->blocks,
- blocks->blocks + blocks->blocks_size, &iter->cur);
-
- return iter;
-}
-
-void ndb_blocks_iterate_free(struct ndb_block_iterator *iter)
-{
- if (iter)
- free(iter);
+ iter->p = blocks->blocks;
}
struct ndb_block *ndb_blocks_iterate_next(struct ndb_block_iterator *iter)
{
- while (iter->cur.p < iter->cur.end) {
- if (!pull_block(iter->content, &iter->cur, &iter->block)) {
+ struct cursor cur;
+ cur.start = iter->blocks->blocks;
+ cur.p = iter->p;
+ cur.end = iter->blocks->blocks + iter->blocks->blocks_size;
+
+ while (cur.p < cur.end) {
+ if (!pull_block(iter->content, &cur, &iter->block)) {
+ iter->p = cur.p;
return NULL;
} else {
+ iter->p = cur.p;
return &iter->block;
}
}
diff --git a/nostrdb/src/block.h b/nostrdb/src/block.h
@@ -29,26 +29,6 @@ struct ndb_blocks {
#pragma pack(pop)
-struct ndb_mention_bech32_block {
- struct ndb_str_block str;
- struct nostr_bech32 bech32;
-};
-
-struct ndb_invoice_block {
- struct ndb_str_block invstr;
- struct ndb_invoice invoice;
-};
-
-struct ndb_block {
- enum ndb_block_type type;
- union {
- struct ndb_str_block str;
- struct ndb_invoice_block invoice;
- struct ndb_mention_bech32_block mention_bech32;
- uint32_t mention_index;
- } block;
-};
-
int push_str_block(struct cursor *buf, const char *content, struct ndb_str_block *block);
int pull_str_block(struct cursor *buf, const char *content, struct ndb_str_block *block);
diff --git a/nostrdb/src/invoice.c b/nostrdb/src/invoice.c
@@ -1,6 +1,7 @@
#include "cursor.h"
#include "invoice.h"
+#include "nostrdb.h"
#include "bolt11/bolt11.h"
#include "bolt11/amount.h"
diff --git a/nostrdb/src/invoice.h b/nostrdb/src/invoice.h
@@ -4,18 +4,10 @@
#include <inttypes.h>
#include "cursor.h"
+#include "nostrdb.h"
struct bolt11;
-struct ndb_invoice {
- unsigned char version;
- uint64_t amount;
- uint64_t timestamp;
- uint64_t expiry;
- char *description;
- unsigned char *description_hash;
-};
-
// ENCODING
int ndb_encode_invoice(struct cursor *cur, struct bolt11 *invoice);
int ndb_decode_invoice(struct cursor *cur, struct ndb_invoice *invoice);
diff --git a/nostrdb/src/nostrdb.h b/nostrdb/src/nostrdb.h
@@ -360,6 +360,42 @@ struct nostr_bech32 {
};
+struct ndb_mention_bech32_block {
+ struct ndb_str_block str;
+ struct nostr_bech32 bech32;
+};
+
+struct ndb_invoice {
+ unsigned char version;
+ uint64_t amount;
+ uint64_t timestamp;
+ uint64_t expiry;
+ char *description;
+ unsigned char *description_hash;
+};
+
+struct ndb_invoice_block {
+ struct ndb_str_block invstr;
+ struct ndb_invoice invoice;
+};
+
+struct ndb_block {
+ enum ndb_block_type type;
+ union {
+ struct ndb_str_block str;
+ struct ndb_invoice_block invoice;
+ struct ndb_mention_bech32_block mention_bech32;
+ uint32_t mention_index;
+ } block;
+};
+
+struct ndb_block_iterator {
+ const char *content;
+ struct ndb_blocks *blocks;
+ struct ndb_block block;
+ unsigned char *p;
+};
+
// CONFIG
void ndb_default_config(struct ndb_config *);
void ndb_config_set_ingest_threads(struct ndb_config *config, int threads);
@@ -479,8 +515,7 @@ void ndb_blocks_free(struct ndb_blocks *blocks);
struct ndb_blocks *ndb_get_blocks_by_key(struct ndb *ndb, struct ndb_txn *txn, uint64_t note_key);
// BLOCK ITERATORS
-struct ndb_block_iterator *ndb_blocks_iterate_start(const char *, struct ndb_blocks *);
-void ndb_blocks_iterate_free(struct ndb_block_iterator *);
+void ndb_blocks_iterate_start(const char *, struct ndb_blocks *, struct ndb_block_iterator *);
struct ndb_block *ndb_blocks_iterate_next(struct ndb_block_iterator *);
// STR BLOCKS