Skip to content

Commit

Permalink
blocks: expose block iterator internals
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jb55 committed Dec 31, 2023
1 parent b975cff commit d21643a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 60 deletions.
36 changes: 11 additions & 25 deletions src/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down
20 changes: 0 additions & 20 deletions src/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/invoice.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "cursor.h"
#include "invoice.h"
#include "nostrdb.h"
#include "bolt11/bolt11.h"
#include "bolt11/amount.h"

Expand Down
10 changes: 1 addition & 9 deletions src/invoice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 37 additions & 2 deletions src/nostrdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,16 @@ static void test_parse_nevent() {
struct ndb_blocks *blocks;
struct ndb_block *block = NULL;
struct nostr_bech32 *bech32;
struct ndb_block_iterator iterator, *iter;
iter = &iterator;
int ok = 0;

static unsigned char event_id[] = { 0x50, 0x5f, 0x0f, 0x0c, 0x98, 0x1d, 0x4b, 0xea, 0x76, 0x82, 0x1b, 0x53,
0x09, 0x59, 0xf2, 0xb8, 0x1d, 0x37, 0x31, 0x3c, 0x9c, 0x03, 0x12, 0x98,
0x7c, 0x88, 0x9d, 0xd7, 0x91, 0x1e, 0x6a, 0x74 };

assert(ndb_parse_content(buf, sizeof(buf), content, strlen(content), &blocks));
struct ndb_block_iterator *iter = ndb_blocks_iterate_start(content, blocks);
ndb_blocks_iterate_start(content, blocks, iter);
assert(blocks->num_blocks == 3);
while ((block = ndb_blocks_iterate_next(iter))) {
switch (++ok) {
Expand Down Expand Up @@ -821,11 +823,13 @@ static void test_url_parsing() {
struct ndb_blocks *blocks;
struct ndb_block *block;
struct ndb_str_block *str;
struct ndb_block_iterator iterator, *iter;
iter = &iterator;

assert(ndb_parse_content(buf, sizeof(buf), content, strlen(content), &blocks));
assert(blocks->num_blocks == 5);

struct ndb_block_iterator *iter = ndb_blocks_iterate_start(content, blocks);
ndb_blocks_iterate_start(content, blocks, iter);
int i = 0;
while ((block = ndb_blocks_iterate_next(iter))) {
str = ndb_block_str(block);
Expand All @@ -839,8 +843,6 @@ static void test_url_parsing() {
}

assert(i == 5);
ndb_blocks_iterate_free(iter);

}


Expand Down

0 comments on commit d21643a

Please sign in to comment.