nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | README | LICENSE

commit 47b016f7ff7e655b5a2acabf61a3fdb3605cad52
parent 82fcacd19fbe4fd58234b75f1fe13fdc6df6dfd7
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 23 Jul 2023 12:07:52 -0700

huge perf boost when switching from strncmp to memcmp

not sure why this is so slow.

Diffstat:
Mnostrdb.c | 13++++++++-----
Mtest.c | 2+-
2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -136,7 +136,7 @@ static inline int ndb_builder_find_str(struct ndb_builder *builder, uint32_t index = ((uint32_t*)builder->str_indices.start)[i]; const char *some_str = (const char*)builder->strings.start + index; - if (!strncmp(some_str, str, len)) { + if (!memcmp(some_str, str, len)) { // found an existing matching str, use that index *pstr = ndb_offset_str(index); return 1; @@ -171,10 +171,13 @@ static int ndb_builder_push_packed_id(struct ndb_builder *builder, unsigned char *id, union ndb_packed_str *pstr) { - if (ndb_builder_find_str(builder, (const char*)id, 32, pstr)) { - pstr->packed.flag = NDB_PACKED_ID; - return 1; - } + // Don't both find id duplicates. very rarely are they duplicated + // and it slows things down quite a bit. If we really care about this + // We can switch to a hash table. + //if (ndb_builder_find_str(builder, (const char*)id, 32, pstr)) { + // pstr->packed.flag = NDB_PACKED_ID; + // return 1; + //} if (ndb_builder_push_str(builder, (const char*)id, 32, pstr)) { pstr->packed.flag = NDB_PACKED_ID; diff --git a/test.c b/test.c @@ -126,7 +126,7 @@ static void test_parse_contact_list() size = ndb_note_from_json((const char*)json, written, &note, buf, alloc_size); printf("ndb_note_from_json size %d\n", size); assert(size > 0); - assert(size == 34058); + assert(size == 34322); const char* expected_content = "{\"wss://nos.lol\":{\"write\":true,\"read\":true},"