All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [PATCH 4/5] json-streamer: do not heap-allocate JSONToken
Date: Tue, 10 Feb 2026 09:30:36 +0100	[thread overview]
Message-ID: <87pl6d80gj.fsf@pond.sub.org> (raw)
In-Reply-To: <20260107084840.150843-5-pbonzini@redhat.com> (Paolo Bonzini's message of "Wed, 7 Jan 2026 09:48:39 +0100")

Paolo Bonzini <pbonzini@redhat.com> writes:

> This is not needed with a push parser.  Since it processes tokens
> immediately, the JSONToken can be created directly on the stack
> and does not need to copy the lexer's string data.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  qobject/json-parser-int.h |  8 ++++++--
>  qobject/json-parser.c     | 18 ------------------
>  qobject/json-streamer.c   |  4 ++--
>  3 files changed, 8 insertions(+), 22 deletions(-)
>
> diff --git a/qobject/json-parser-int.h b/qobject/json-parser-int.h
> index 1f435cb8eb2..5a6b5c9af90 100644
> --- a/qobject/json-parser-int.h
> +++ b/qobject/json-parser-int.h
> @@ -35,7 +35,12 @@ typedef enum json_token_type {
>      JSON_MAX = JSON_END_OF_INPUT
>  } JSONTokenType;
>  
> -typedef struct JSONToken JSONToken;
> +typedef struct JSONToken {
> +    JSONTokenType type;
> +    int x;
> +    int y;
> +    char *str;
> +} JSONToken;
>  
>  /* json-lexer.c */
>  void json_lexer_init(JSONLexer *lexer, bool enable_interpolation);
> @@ -48,7 +53,6 @@ void json_message_process_token(JSONLexer *lexer, GString *input,
>                                  JSONTokenType type, int x, int y);
>  
>  /* json-parser.c */
> -JSONToken *json_token(JSONTokenType type, int x, int y, GString *tokstr);
>  void json_parser_init(JSONParserContext *ctxt, va_list *ap);
>  void json_parser_reset(JSONParserContext *ctxt);
>  QObject *json_parser_feed(JSONParserContext *ctxt, const JSONToken *token, Error **errp);
> diff --git a/qobject/json-parser.c b/qobject/json-parser.c
> index 7abdea4dacb..2fede59842f 100644
> --- a/qobject/json-parser.c
> +++ b/qobject/json-parser.c
> @@ -24,13 +24,6 @@
>  #include "qobject/qstring.h"
>  #include "json-parser-int.h"
>  
> -struct JSONToken {
> -    JSONTokenType type;
> -    int x;
> -    int y;
> -    char str[];
> -};
> -
>  /*
>   * Objects: { } | { members }
>   * - Empty: { -> AFTER_LCURLY -> }
> @@ -529,17 +522,6 @@ static QObject *json_parser_parse_token(JSONParserContext *ctxt, const JSONToken
>      return NULL;
>  }
>  
> -JSONToken *json_token(JSONTokenType type, int x, int y, GString *tokstr)
> -{
> -    JSONToken *token = g_malloc(sizeof(JSONToken) + tokstr->len + 1);
> -
> -    token->type = type;
> -    memcpy(token->str, tokstr->str, tokstr->len);
> -    token->str[tokstr->len] = 0;
> -    token->x = x;
> -    token->y = y;
> -    return token;
> -}
>  
>  void json_parser_reset(JSONParserContext *ctxt)
>  {
> diff --git a/qobject/json-streamer.c b/qobject/json-streamer.c
> index a1210128ac1..07e0ef51ce3 100644
> --- a/qobject/json-streamer.c
> +++ b/qobject/json-streamer.c
> @@ -23,7 +23,7 @@ void json_message_process_token(JSONLexer *lexer, GString *input,
>                                  JSONTokenType type, int x, int y)
>  {
>      JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
> -    g_autofree JSONToken *token = json_token(type, x, y, input);
> +    JSONToken token = (JSONToken) { .type = type, .x = x, .y = y, .str = input->str };

checkpatch points out:

    0004-json-streamer-do-not-heap-allocate-JSONToken.patch:89: WARNING: line over 80 characters

Easy enough to avoid.

>      Error *err = NULL;
>  
>      parser->token_size += input->len;
> @@ -64,7 +64,7 @@ void json_message_process_token(JSONLexer *lexer, GString *input,
>          } else if (parser->bracket_count + parser->brace_count > MAX_NESTING) {
>              error_setg(&err, "JSON nesting depth limit exceeded");
>          } else {
> -            QObject *json = json_parser_feed(&parser->parser, token, &err);
> +            QObject *json = json_parser_feed(&parser->parser, &token, &err);
>              if (json) {
>                  parser->emit(parser->opaque, json, NULL);
>              }

With the long line tidied up
Reviewed-by: Markus Armbruster <armbru@redhat.com>



  reply	other threads:[~2026-02-10  8:31 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-07  8:48 [PATCH 0/5] qobject: switch JSON parser to push Paolo Bonzini
2026-01-07  8:48 ` [PATCH 1/5] json-parser: pass around lookahead token, constify Paolo Bonzini
2026-02-06 10:45   ` Markus Armbruster
2026-02-06 10:54     ` Paolo Bonzini
2026-01-07  8:48 ` [PATCH 2/5] json-parser: replace with a push parser Paolo Bonzini
2026-02-09  9:36   ` Markus Armbruster
2026-02-09 10:53     ` Paolo Bonzini
2026-02-12 13:12       ` Markus Armbruster
2026-02-16 16:41         ` Paolo Bonzini
2026-02-17  7:39           ` Markus Armbruster
2026-01-07  8:48 ` [PATCH 3/5] json-streamer: remove token queue Paolo Bonzini
2026-02-10  7:58   ` Markus Armbruster
2026-02-10  8:22     ` Paolo Bonzini
2026-02-11  7:13       ` Markus Armbruster
2026-01-07  8:48 ` [PATCH 4/5] json-streamer: do not heap-allocate JSONToken Paolo Bonzini
2026-02-10  8:30   ` Markus Armbruster [this message]
2026-01-07  8:48 ` [PATCH 5/5] json-parser: add location to JSON parsing errors Paolo Bonzini
2026-02-10  9:21   ` Markus Armbruster
2026-02-10  9:44     ` Paolo Bonzini
2026-02-11  7:18       ` Markus Armbruster
2026-01-21  5:57 ` [PATCH 0/5] qobject: switch JSON parser to push Paolo Bonzini
2026-01-30 13:00 ` Markus Armbruster
2026-01-30 13:36   ` Paolo Bonzini
2026-02-10 13:06     ` Markus Armbruster
2026-02-10 13:12       ` Paolo Bonzini
2026-02-10 15:52         ` Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87pl6d80gj.fsf@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.