Skip to content

Commit 2ed1697

Browse files
committed
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
1 parent 2b8b709 commit 2ed1697

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

common/json_parse_simple.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ bool json_tok_strneq(const char *buffer, const jsmntok_t *tok,
3333
return memeq(buffer + tok->start, tok->end - tok->start, str, len);
3434
}
3535

36-
bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str)
37-
{
38-
return json_tok_strneq(buffer, tok, str, strlen(str));
39-
}
40-
4136
bool json_tok_startswith(const char *buffer, const jsmntok_t *tok,
4237
const char *prefix)
4338
{
@@ -217,12 +212,6 @@ const jsmntok_t *json_get_membern(const char *buffer,
217212
return NULL;
218213
}
219214

220-
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
221-
const char *label)
222-
{
223-
return json_get_membern(buffer, tok, label, strlen(label));
224-
}
225-
226215
const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index)
227216
{
228217
const jsmntok_t *t;

common/json_parse_simple.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ const char *json_tok_full(const char *buffer, const jsmntok_t *t);
1414
/* Include " if it's a string. */
1515
int json_tok_full_len(const jsmntok_t *t);
1616

17-
/* Is this a string equal to str? */
18-
bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str);
19-
2017
/* Is this a string equal to str of length len? */
2118
bool json_tok_strneq(const char *buffer, const jsmntok_t *tok,
2219
const char *str, size_t len);
2320

21+
/* Is this a string equal to str? */
22+
static inline bool json_tok_streq(const char *buffer, const jsmntok_t *tok,
23+
const char *str)
24+
{
25+
return json_tok_strneq(buffer, tok, str, strlen(str));
26+
}
27+
2428
/* Does this string token start with prefix? */
2529
bool json_tok_startswith(const char *buffer, const jsmntok_t *tok,
2630
const char *prefix);
@@ -66,8 +70,12 @@ const jsmntok_t *json_get_membern(const char *buffer,
6670
const char *label, size_t len);
6771

6872
/* Get top-level member. */
69-
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
70-
const char *label);
73+
static inline const jsmntok_t *json_get_member(const char *buffer,
74+
const jsmntok_t tok[],
75+
const char *label)
76+
{
77+
return json_get_membern(buffer, tok, label, strlen(label));
78+
}
7179

7280
/* Get index'th array member. */
7381
const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index);

0 commit comments

Comments
 (0)