Skip to content

common/json_parse_simple: make convenience functions inline #8414

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

whitslack
Copy link
Collaborator

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.

Checklist

Before submitting the PR, ensure the following tasks are completed. If an item is not applicable to your PR, please mark it as checked:

  • The changelog has been updated in the relevant commit(s) according to the guidelines. Not a user-facing change.
  • Tests have been added or modified to reflect the changes. Not applicable.
  • Documentation has been reviewed and updated as needed. Not applicable.
  • Related issues have been listed and linked, including any that this PR closes. Not applicable.

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
@whitslack whitslack force-pushed the inline-json-convenience branch from 2ed1697 to 7cae9a6 Compare July 12, 2025 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant