common/json_parse_simple: make convenience functions inline
json_tok_streq(…) and json_get_member(…) are convenience wrappers for
json_tok_strneq(…) and json_get_membern(…) respectively. Unfortunately, using
them incurs a performance penalty in the common case where they are called with
a string literal argument because the compiler is unable to substitute a
compile-time constant in place of the buried call to strlen(…).
For example,
json_get_member(buf, tok, "example");
…will have worse performance than…
json_get_membern(buf, tok, "example", strlen("example"));
…because the former is forced to scan over "example" at run-time to count its
length whereas the latter is able to elide the strlen(…) call at compile time.
Hoist these convenience functions up into common/json_parse_simple.h and mark
them as inline so that the compiler can elide the strlen(…) call in the common
case of calling these functions with a string literal argument.
Changelog-None
This commit is contained in:
committed by
Rusty Russell
parent
5f5440383d
commit
9112c1d518
@@ -33,11 +33,6 @@ bool json_tok_strneq(const char *buffer, const jsmntok_t *tok,
|
||||
return memeq(buffer + tok->start, tok->end - tok->start, str, len);
|
||||
}
|
||||
|
||||
bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str)
|
||||
{
|
||||
return json_tok_strneq(buffer, tok, str, strlen(str));
|
||||
}
|
||||
|
||||
bool json_tok_startswith(const char *buffer, const jsmntok_t *tok,
|
||||
const char *prefix)
|
||||
{
|
||||
@@ -217,12 +212,6 @@ const jsmntok_t *json_get_membern(const char *buffer,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
|
||||
const char *label)
|
||||
{
|
||||
return json_get_membern(buffer, tok, label, strlen(label));
|
||||
}
|
||||
|
||||
const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index)
|
||||
{
|
||||
const jsmntok_t *t;
|
||||
|
||||
Reference in New Issue
Block a user