Skip to content

Commit 7cae9a6

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 7cae9a6

File tree

7 files changed

+58
-51
lines changed

7 files changed

+58
-51
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);

plugins/bkpr/test/run-bkpr_db.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ bool is_asterix_notification(const char *notification_name UNNEEDED,
112112
const char *json_get_id(const tal_t *ctx UNNEEDED,
113113
const char *buffer UNNEEDED, const jsmntok_t *obj UNNEEDED)
114114
{ fprintf(stderr, "json_get_id called!\n"); abort(); }
115-
/* Generated stub for json_get_member */
116-
const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
117-
const char *label UNNEEDED)
118-
{ fprintf(stderr, "json_get_member called!\n"); abort(); }
115+
/* Generated stub for json_get_membern */
116+
const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
117+
const jsmntok_t tok[] UNNEEDED,
118+
const char *label UNNEEDED, size_t len UNNEEDED)
119+
{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
119120
/* Generated stub for json_next */
120121
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
121122
{ fprintf(stderr, "json_next called!\n"); abort(); }
@@ -194,9 +195,10 @@ int json_tok_full_len(const jsmntok_t *t UNNEEDED)
194195
void json_tok_remove(jsmntok_t **tokens UNNEEDED,
195196
jsmntok_t *obj_or_array UNNEEDED, const jsmntok_t *tok UNNEEDED, size_t num UNNEEDED)
196197
{ fprintf(stderr, "json_tok_remove called!\n"); abort(); }
197-
/* Generated stub for json_tok_streq */
198-
bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
199-
{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
198+
/* Generated stub for json_tok_strneq */
199+
bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
200+
const char *str UNNEEDED, size_t len UNNEEDED)
201+
{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
200202
/* Generated stub for last_fee_state */
201203
enum htlc_state last_fee_state(enum side opener UNNEEDED)
202204
{ fprintf(stderr, "last_fee_state called!\n"); abort(); }

plugins/bkpr/test/run-recorder.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ bool is_asterix_notification(const char *notification_name UNNEEDED,
118118
const char *json_get_id(const tal_t *ctx UNNEEDED,
119119
const char *buffer UNNEEDED, const jsmntok_t *obj UNNEEDED)
120120
{ fprintf(stderr, "json_get_id called!\n"); abort(); }
121-
/* Generated stub for json_get_member */
122-
const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
123-
const char *label UNNEEDED)
124-
{ fprintf(stderr, "json_get_member called!\n"); abort(); }
121+
/* Generated stub for json_get_membern */
122+
const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
123+
const jsmntok_t tok[] UNNEEDED,
124+
const char *label UNNEEDED, size_t len UNNEEDED)
125+
{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
125126
/* Generated stub for json_next */
126127
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
127128
{ fprintf(stderr, "json_next called!\n"); abort(); }
@@ -200,9 +201,10 @@ int json_tok_full_len(const jsmntok_t *t UNNEEDED)
200201
void json_tok_remove(jsmntok_t **tokens UNNEEDED,
201202
jsmntok_t *obj_or_array UNNEEDED, const jsmntok_t *tok UNNEEDED, size_t num UNNEEDED)
202203
{ fprintf(stderr, "json_tok_remove called!\n"); abort(); }
203-
/* Generated stub for json_tok_streq */
204-
bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
205-
{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
204+
/* Generated stub for json_tok_strneq */
205+
bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
206+
const char *str UNNEEDED, size_t len UNNEEDED)
207+
{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
206208
/* Generated stub for last_fee_state */
207209
enum htlc_state last_fee_state(enum side opener UNNEEDED)
208210
{ fprintf(stderr, "last_fee_state called!\n"); abort(); }

plugins/test/run-route-calc.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,11 @@ void json_array_end(struct json_stream *js UNNEEDED)
160160
/* Generated stub for json_array_start */
161161
void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED)
162162
{ fprintf(stderr, "json_array_start called!\n"); abort(); }
163-
/* Generated stub for json_get_member */
164-
const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
165-
const char *label UNNEEDED)
166-
{ fprintf(stderr, "json_get_member called!\n"); abort(); }
163+
/* Generated stub for json_get_membern */
164+
const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
165+
const jsmntok_t tok[] UNNEEDED,
166+
const char *label UNNEEDED, size_t len UNNEEDED)
167+
{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
167168
/* Generated stub for json_next */
168169
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
169170
{ fprintf(stderr, "json_next called!\n"); abort(); }
@@ -245,9 +246,10 @@ const char *json_tok_full(const char *buffer UNNEEDED, const jsmntok_t *t UNNEED
245246
/* Generated stub for json_tok_full_len */
246247
int json_tok_full_len(const jsmntok_t *t UNNEEDED)
247248
{ fprintf(stderr, "json_tok_full_len called!\n"); abort(); }
248-
/* Generated stub for json_tok_streq */
249-
bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
250-
{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
249+
/* Generated stub for json_tok_strneq */
250+
bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
251+
const char *str UNNEEDED, size_t len UNNEEDED)
252+
{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
251253
/* Generated stub for jsonrpc_request_start_ */
252254
struct out_req *jsonrpc_request_start_(struct command *cmd UNNEEDED,
253255
const char *method UNNEEDED,

plugins/test/run-route-overlong.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ void json_array_end(struct json_stream *js UNNEEDED)
157157
/* Generated stub for json_array_start */
158158
void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED)
159159
{ fprintf(stderr, "json_array_start called!\n"); abort(); }
160-
/* Generated stub for json_get_member */
161-
const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
162-
const char *label UNNEEDED)
163-
{ fprintf(stderr, "json_get_member called!\n"); abort(); }
160+
/* Generated stub for json_get_membern */
161+
const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
162+
const jsmntok_t tok[] UNNEEDED,
163+
const char *label UNNEEDED, size_t len UNNEEDED)
164+
{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
164165
/* Generated stub for json_next */
165166
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
166167
{ fprintf(stderr, "json_next called!\n"); abort(); }
@@ -242,9 +243,10 @@ const char *json_tok_full(const char *buffer UNNEEDED, const jsmntok_t *t UNNEED
242243
/* Generated stub for json_tok_full_len */
243244
int json_tok_full_len(const jsmntok_t *t UNNEEDED)
244245
{ fprintf(stderr, "json_tok_full_len called!\n"); abort(); }
245-
/* Generated stub for json_tok_streq */
246-
bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
247-
{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
246+
/* Generated stub for json_tok_strneq */
247+
bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
248+
const char *str UNNEEDED, size_t len UNNEEDED)
249+
{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
248250
/* Generated stub for jsonrpc_request_start_ */
249251
struct out_req *jsonrpc_request_start_(struct command *cmd UNNEEDED,
250252
const char *method UNNEEDED,

wallet/test/run-wallet.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,11 @@ void json_array_end(struct json_stream *js UNNEEDED)
579579
/* Generated stub for json_array_start */
580580
void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED)
581581
{ fprintf(stderr, "json_array_start called!\n"); abort(); }
582-
/* Generated stub for json_get_member */
583-
const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
584-
const char *label UNNEEDED)
585-
{ fprintf(stderr, "json_get_member called!\n"); abort(); }
582+
/* Generated stub for json_get_membern */
583+
const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
584+
const jsmntok_t tok[] UNNEEDED,
585+
const char *label UNNEEDED, size_t len UNNEEDED)
586+
{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
586587
/* Generated stub for json_next */
587588
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
588589
{ fprintf(stderr, "json_next called!\n"); abort(); }
@@ -636,9 +637,10 @@ bool json_tok_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEE
636637
/* Generated stub for json_tok_full */
637638
const char *json_tok_full(const char *buffer UNNEEDED, const jsmntok_t *t UNNEEDED)
638639
{ fprintf(stderr, "json_tok_full called!\n"); abort(); }
639-
/* Generated stub for json_tok_streq */
640-
bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
641-
{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
640+
/* Generated stub for json_tok_strneq */
641+
bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
642+
const char *str UNNEEDED, size_t len UNNEEDED)
643+
{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
642644
/* Generated stub for kill_uncommitted_channel */
643645
void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED,
644646
const char *why UNNEEDED)

0 commit comments

Comments
 (0)