common/json_parse_simple: make convenience functions inline
What changed, and why it matters
This change moves two helper functions from a source file into a header file and marks them 'inline' so the compiler can optimize common calls. It is a performance cleanup, not a security fix. The functions behave the same way as before; only how the compiler sees them changes. Test stub files are updated to match the new internal layout.
No security action required; treat as a routine performance/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit hoists json_tok_streq() and json_get_member() from common/json_parse_simple.c into common/json_parse_simple.h as static inline wrappers around json_tok_strneq() and json_get_membern(). The goal is to let the compiler constant-fold strlen() when these helpers are called with string literals. The implementations are unchanged; only linkage and visibility to the optimizer differ. Corresponding generated test stubs are switched from the old non-inline symbols to the underlying strneq/membern symbols. No behavioral or API change is introduced.
Changed components
common/json_parse_simple.ccommon/json_parse_simple.hplugins/bkpr/test/run-bkpr_db.cplugins/bkpr/test/run-recorder.cplugins/test/run-route-calc.cplugins/test/run-route-overlong.cwallet/test/run-wallet.cInspect captured patch +58 / −51
diff --git a/common/json_parse_simple.c b/common/json_parse_simple.c
index 7f9c630e..a40cb02f 100644
--- a/common/json_parse_simple.c
+++ b/common/json_parse_simple.c
@@ -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;
diff --git a/common/json_parse_simple.h b/common/json_parse_simple.h
index 710520d6..eb7a70d5 100644
--- a/common/json_parse_simple.h
+++ b/common/json_parse_simple.h
@@ -14,13 +14,17 @@ const char *json_tok_full(const char *buffer, const jsmntok_t *t);
/* Include " if it's a string. */
int json_tok_full_len(const jsmntok_t *t);
-/* Is this a string equal to str? */
-bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str);
-
/* Is this a string equal to str of length len? */
bool json_tok_strneq(const char *buffer, const jsmntok_t *tok,
const char *str, size_t len);
+/* Is this a string equal to str? */
+static inline bool json_tok_streq(const char *buffer, const jsmntok_t *tok,
+ const char *str)
+{
+ return json_tok_strneq(buffer, tok, str, strlen(str));
+}
+
/* Does this string token start with prefix? */
bool json_tok_startswith(const char *buffer, const jsmntok_t *tok,
const char *prefix);
@@ -66,8 +70,12 @@ const jsmntok_t *json_get_membern(const char *buffer,
const char *label, size_t len);
/* Get top-level member. */
-const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
- const char *label);
+static inline 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));
+}
/* Get index'th array member. */
const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index);
diff --git a/plugins/bkpr/test/run-bkpr_db.c b/plugins/bkpr/test/run-bkpr_db.c
index 10ba59ae..13801dca 100644
--- a/plugins/bkpr/test/run-bkpr_db.c
+++ b/plugins/bkpr/test/run-bkpr_db.c
@@ -112,10 +112,11 @@ bool is_asterix_notification(const char *notification_name UNNEEDED,
const char *json_get_id(const tal_t *ctx UNNEEDED,
const char *buffer UNNEEDED, const jsmntok_t *obj UNNEEDED)
{ fprintf(stderr, "json_get_id called!\n"); abort(); }
-/* Generated stub for json_get_member */
-const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
- const char *label UNNEEDED)
-{ fprintf(stderr, "json_get_member called!\n"); abort(); }
+/* Generated stub for json_get_membern */
+const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
+ const jsmntok_t tok[] UNNEEDED,
+ const char *label UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
/* Generated stub for json_next */
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
{ fprintf(stderr, "json_next called!\n"); abort(); }
@@ -194,9 +195,10 @@ int json_tok_full_len(const jsmntok_t *t UNNEEDED)
void json_tok_remove(jsmntok_t **tokens UNNEEDED,
jsmntok_t *obj_or_array UNNEEDED, const jsmntok_t *tok UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "json_tok_remove called!\n"); abort(); }
-/* Generated stub for json_tok_streq */
-bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
-{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
+/* Generated stub for json_tok_strneq */
+bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
+ const char *str UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
/* Generated stub for last_fee_state */
enum htlc_state last_fee_state(enum side opener UNNEEDED)
{ fprintf(stderr, "last_fee_state called!\n"); abort(); }
diff --git a/plugins/bkpr/test/run-recorder.c b/plugins/bkpr/test/run-recorder.c
index 16cd8e9b..351d4d26 100644
--- a/plugins/bkpr/test/run-recorder.c
+++ b/plugins/bkpr/test/run-recorder.c
@@ -118,10 +118,11 @@ bool is_asterix_notification(const char *notification_name UNNEEDED,
const char *json_get_id(const tal_t *ctx UNNEEDED,
const char *buffer UNNEEDED, const jsmntok_t *obj UNNEEDED)
{ fprintf(stderr, "json_get_id called!\n"); abort(); }
-/* Generated stub for json_get_member */
-const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
- const char *label UNNEEDED)
-{ fprintf(stderr, "json_get_member called!\n"); abort(); }
+/* Generated stub for json_get_membern */
+const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
+ const jsmntok_t tok[] UNNEEDED,
+ const char *label UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
/* Generated stub for json_next */
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
{ fprintf(stderr, "json_next called!\n"); abort(); }
@@ -200,9 +201,10 @@ int json_tok_full_len(const jsmntok_t *t UNNEEDED)
void json_tok_remove(jsmntok_t **tokens UNNEEDED,
jsmntok_t *obj_or_array UNNEEDED, const jsmntok_t *tok UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "json_tok_remove called!\n"); abort(); }
-/* Generated stub for json_tok_streq */
-bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
-{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
+/* Generated stub for json_tok_strneq */
+bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
+ const char *str UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
/* Generated stub for last_fee_state */
enum htlc_state last_fee_state(enum side opener UNNEEDED)
{ fprintf(stderr, "last_fee_state called!\n"); abort(); }
diff --git a/plugins/test/run-route-calc.c b/plugins/test/run-route-calc.c
index 3898c3f4..73e56ce3 100644
--- a/plugins/test/run-route-calc.c
+++ b/plugins/test/run-route-calc.c
@@ -160,10 +160,11 @@ void json_array_end(struct json_stream *js UNNEEDED)
/* Generated stub for json_array_start */
void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED)
{ fprintf(stderr, "json_array_start called!\n"); abort(); }
-/* Generated stub for json_get_member */
-const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
- const char *label UNNEEDED)
-{ fprintf(stderr, "json_get_member called!\n"); abort(); }
+/* Generated stub for json_get_membern */
+const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
+ const jsmntok_t tok[] UNNEEDED,
+ const char *label UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
/* Generated stub for json_next */
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
{ 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
/* Generated stub for json_tok_full_len */
int json_tok_full_len(const jsmntok_t *t UNNEEDED)
{ fprintf(stderr, "json_tok_full_len called!\n"); abort(); }
-/* Generated stub for json_tok_streq */
-bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
-{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
+/* Generated stub for json_tok_strneq */
+bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
+ const char *str UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
/* Generated stub for jsonrpc_request_start_ */
struct out_req *jsonrpc_request_start_(struct command *cmd UNNEEDED,
const char *method UNNEEDED,
diff --git a/plugins/test/run-route-overlong.c b/plugins/test/run-route-overlong.c
index 3683c0dd..ad0a63ec 100644
--- a/plugins/test/run-route-overlong.c
+++ b/plugins/test/run-route-overlong.c
@@ -157,10 +157,11 @@ void json_array_end(struct json_stream *js UNNEEDED)
/* Generated stub for json_array_start */
void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED)
{ fprintf(stderr, "json_array_start called!\n"); abort(); }
-/* Generated stub for json_get_member */
-const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
- const char *label UNNEEDED)
-{ fprintf(stderr, "json_get_member called!\n"); abort(); }
+/* Generated stub for json_get_membern */
+const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
+ const jsmntok_t tok[] UNNEEDED,
+ const char *label UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
/* Generated stub for json_next */
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
{ 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
/* Generated stub for json_tok_full_len */
int json_tok_full_len(const jsmntok_t *t UNNEEDED)
{ fprintf(stderr, "json_tok_full_len called!\n"); abort(); }
-/* Generated stub for json_tok_streq */
-bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
-{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
+/* Generated stub for json_tok_strneq */
+bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
+ const char *str UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
/* Generated stub for jsonrpc_request_start_ */
struct out_req *jsonrpc_request_start_(struct command *cmd UNNEEDED,
const char *method UNNEEDED,
diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c
index b3279625..881828ae 100644
--- a/wallet/test/run-wallet.c
+++ b/wallet/test/run-wallet.c
@@ -579,10 +579,11 @@ void json_array_end(struct json_stream *js UNNEEDED)
/* Generated stub for json_array_start */
void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED)
{ fprintf(stderr, "json_array_start called!\n"); abort(); }
-/* Generated stub for json_get_member */
-const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
- const char *label UNNEEDED)
-{ fprintf(stderr, "json_get_member called!\n"); abort(); }
+/* Generated stub for json_get_membern */
+const jsmntok_t *json_get_membern(const char *buffer UNNEEDED,
+ const jsmntok_t tok[] UNNEEDED,
+ const char *label UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_get_membern called!\n"); abort(); }
/* Generated stub for json_next */
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
{ 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
/* Generated stub for json_tok_full */
const char *json_tok_full(const char *buffer UNNEEDED, const jsmntok_t *t UNNEEDED)
{ fprintf(stderr, "json_tok_full called!\n"); abort(); }
-/* Generated stub for json_tok_streq */
-bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
-{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
+/* Generated stub for json_tok_strneq */
+bool json_tok_strneq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
+ const char *str UNNEEDED, size_t len UNNEEDED)
+{ fprintf(stderr, "json_tok_strneq called!\n"); abort(); }
/* Generated stub for kill_uncommitted_channel */
void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED,
const char *why UNNEEDED)
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.