-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a new rcur
read-only cursor type, adapt code to use it.
#45
Open
rustyrussell
wants to merge
13
commits into
damus-io:master
Choose a base branch
from
rustyrussell:guilt/new-cursor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b0ae492
content_parser.c: refactor.
rustyrussell 8c874a7
rcur: read-only cursor.
rustyrussell 0b55c52
nostrdb.c: use rcur for text_search_key_compare.
rustyrussell 67d1894
nostrdb.c: use rcur instead of cursor for text search funcs.
rustyrussell 48c6226
nostrdb.c: use rcur inside ndb_parse_words().
rustyrussell b8c8747
content_parser.c: consume_url_path doesn't fail, return void.
rustyrussell 2874287
content_parser.c: convert to rcur.
rustyrussell 904d37d
nostr_bech32.c: use rcur.
rustyrussell 79e1ee6
invoice.c: use rcur.
rustyrussell d8191d9
block.c: use rcur.
rustyrussell 0a784d1
block.c: make iterator use an rcur, not a simple pointer.
rustyrussell 423d76b
Clean up direct access to rcur.
rustyrussell c67ae4e
rcur: simplify.
rustyrussell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
#include "rcur.h" | ||
#include "ccan/utf8/utf8.h" | ||
#include "cursor.h" | ||
#include <errno.h> | ||
#include <string.h> | ||
|
||
/* Note that only rcur_pull and rcur_peek_remainder actually access | ||
* rcur. The rest are built on top of them! */ | ||
|
||
/* Pull @len bytes from rcur, if space available. | ||
* Return NULL if not valid. | ||
*/ | ||
const void *rcur_pull(struct rcur *rcur, size_t len) | ||
{ | ||
const void *p = rcur_peek(rcur, len); | ||
if (!p) { | ||
rcur_fail(rcur); | ||
return NULL; | ||
} | ||
rcur->p += len; | ||
return rcur->p - len; | ||
} | ||
|
||
/* Access the remaining bytes. Returns NULL and *len=0 if invalid. */ | ||
const void *rcur_peek_remainder(const struct rcur *rcur, size_t *len) | ||
{ | ||
if (!rcur_valid(rcur)) { | ||
*len = 0; | ||
return NULL; | ||
} | ||
*len = rcur->end - rcur->p; | ||
return rcur->p; | ||
} | ||
|
||
bool rcur_copy(struct rcur *rcur, void *dst, size_t len) | ||
{ | ||
const void *src = rcur_pull(rcur, len); | ||
if (!src) | ||
return false; | ||
memcpy(dst, src, len); | ||
return true; | ||
} | ||
|
||
/* Look ahead: returns NULL if @len too long. Does *not* alter | ||
* rcur */ | ||
const void *rcur_peek(const struct rcur *rcur, size_t len) | ||
{ | ||
size_t actual_len; | ||
const void *p = rcur_peek_remainder(rcur, &actual_len); | ||
|
||
if (len > actual_len) | ||
return NULL; | ||
return p; | ||
} | ||
|
||
/* All these are based on rcur_pull. */ | ||
bool rcur_skip(struct rcur *rcur, size_t len) | ||
{ | ||
return rcur_pull(rcur, len) != NULL; | ||
} | ||
|
||
unsigned char rcur_pull_byte(struct rcur *rcur) | ||
{ | ||
unsigned char v; | ||
|
||
if (!rcur_copy(rcur, &v, sizeof(v))) | ||
return 0; | ||
return v; | ||
} | ||
|
||
uint16_t rcur_pull_u16(struct rcur *rcur) | ||
{ | ||
uint16_t v; | ||
|
||
if (!rcur_copy(rcur, &v, sizeof(v))) | ||
return 0; | ||
return v; | ||
} | ||
|
||
uint32_t rcur_pull_u32(struct rcur *rcur) | ||
{ | ||
uint32_t v; | ||
|
||
if (!rcur_copy(rcur, &v, sizeof(v))) | ||
return 0; | ||
return v; | ||
} | ||
|
||
uint64_t rcur_pull_varint(struct rcur *rcur) | ||
{ | ||
uint64_t v = 0; | ||
unsigned char c; | ||
|
||
for (size_t i = 0; i < 10; i++) { // Loop up to 10 bytes for 64-bit | ||
c = rcur_pull_byte(rcur); | ||
v |= ((uint64_t)c & 0x7F) << (i * 7); | ||
|
||
if ((c & 0x80) == 0) | ||
break; | ||
} | ||
return v; | ||
} | ||
|
||
uint32_t rcur_pull_varint_u32(struct rcur *rcur) | ||
{ | ||
uint64_t v = rcur_pull_varint(rcur); | ||
if (v >= UINT32_MAX) { | ||
rcur_fail(rcur); | ||
v = 0; | ||
} | ||
return v; | ||
} | ||
|
||
size_t rcur_pull_whitespace(struct rcur *rcur) | ||
{ | ||
size_t len, wslen; | ||
const unsigned char *c; | ||
|
||
c = rcur_peek_remainder(rcur, &len); | ||
for (wslen = 0; wslen < len; wslen++) { | ||
if (!is_whitespace(c[wslen])) | ||
break; | ||
} | ||
|
||
rcur_skip(rcur, wslen); | ||
return wslen; | ||
} | ||
|
||
// Returns 0 on error. Adds length to *totlen. | ||
static uint32_t rcur_pull_utf8(struct rcur *rcur, size_t *totlen) | ||
{ | ||
struct utf8_state state = UTF8_STATE_INIT; | ||
|
||
/* Since 0 is treated as a bad encoding, this terminated if we run | ||
* out of chars */ | ||
while (!utf8_decode(&state, rcur_pull_byte(rcur))) | ||
(*totlen)++; | ||
|
||
if (errno != 0) { | ||
rcur_fail(rcur); | ||
return 0; | ||
} | ||
return state.c; | ||
} | ||
|
||
/* Remove is_punctuation(), return bytes removed */ | ||
size_t rcur_pull_punctuation(struct rcur *rcur) | ||
{ | ||
size_t totlen = 0; | ||
|
||
while (rcur_bytes_remaining(*rcur)) { | ||
uint32_t c = rcur_pull_utf8(rcur, &totlen); | ||
if (!is_punctuation(c)) | ||
break; | ||
} | ||
|
||
if (!rcur_valid(rcur)) | ||
return 0; | ||
return totlen; | ||
} | ||
|
||
/* Remove !is_alphanumeric(), return bytes removed */ | ||
size_t rcur_pull_non_alphanumeric(struct rcur *rcur) | ||
{ | ||
size_t len, nonalpha_len; | ||
const char *p = rcur_peek_remainder(rcur, &len); | ||
|
||
for (nonalpha_len = 0; nonalpha_len < len; nonalpha_len++) { | ||
if (!is_alphanumeric(p[nonalpha_len])) | ||
break; | ||
} | ||
|
||
rcur_skip(rcur, nonalpha_len); | ||
return nonalpha_len; | ||
} | ||
|
||
const char *rcur_pull_word(struct rcur *rcur, size_t *len) | ||
{ | ||
const char *start = rcur_peek(rcur, 0); | ||
|
||
/* consume_until_boundary */ | ||
*len = 0; | ||
while (rcur_bytes_remaining(*rcur)) { | ||
uint32_t c = rcur_pull_utf8(rcur, len); | ||
if (!c || is_right_boundary(c)) | ||
break; | ||
} | ||
|
||
if (!rcur_valid(rcur) || *len == 0) | ||
return NULL; | ||
|
||
return start; | ||
} | ||
|
||
const char *rcur_pull_c_string(struct rcur *rcur) | ||
{ | ||
size_t len; | ||
const char *p = rcur_peek_remainder(rcur, &len); | ||
|
||
for (size_t i = 0; i < len; i++) { | ||
if (p[i] == '\0') | ||
return rcur_pull(rcur, i+1); | ||
} | ||
rcur_fail(rcur); | ||
return NULL; | ||
} | ||
|
||
bool rcur_skip_if_match(struct rcur *rcur, const void *p, size_t len) | ||
{ | ||
const void *peek = rcur_peek(rcur, len); | ||
|
||
if (!peek) | ||
return false; | ||
|
||
if (memcmp(p, peek, len) != 0) | ||
return false; | ||
rcur_skip(rcur, len); | ||
return true; | ||
} | ||
|
||
bool rcur_skip_if_str_anycase(struct rcur *rcur, const char *str) | ||
{ | ||
size_t len = strlen(str); | ||
const char *peek = rcur_peek(rcur, len); | ||
|
||
if (!peek) | ||
return false; | ||
|
||
for (size_t i = 0; i < len; i++) { | ||
if (tolower(peek[i]) != tolower(str[i])) | ||
return false; | ||
} | ||
rcur_skip(rcur, len); | ||
return true; | ||
} | ||
|
||
const char *rcur_pull_prefixed_str(struct rcur *rcur, size_t *len) | ||
{ | ||
*len = rcur_pull_varint(rcur); | ||
return rcur_pull(rcur, *len); | ||
} | ||
|
||
bool rcur_trim_if_char(struct rcur *rcur, char c) | ||
{ | ||
const char *p; | ||
size_t len; | ||
|
||
p = rcur_peek_remainder(rcur, &len); | ||
if (len > 0 && p[len-1] == c) { | ||
rcur->p--; | ||
return true; | ||
} | ||
return false; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a very hot function (especially when used in
ndb_text_search_key_compare
>rcur_pull_varint
) where lmdb can call this many times when walking the btree during search queries.it seems this hot code path now calls (
rcur_pull
>rcur_peek
>rcur_peek_remainer
+memcpy
for a single byte)when before it was just:
this makes me a bit nervous, but maybe the compiler will optimize this all away. will try to do some search benchmarking and compare