qjson: save a lot of memory
v2 cover qjson: save a lot of memory
This patch from 2011 (!) saves about 96% of the allocation cost (down from 500 MiB to 20 MiB) for check-qjson.
Paolo
Paolo Bonzini (4):
qjson: replace QString in JSONLexer with GString
qjson: do not save/restore contexts
qjson: store tokens in a GQueue
qjson: surprise, allocating 6 QObjects per token is expensive
include/qapi/qmp/json-lexer.h | 7 +- include/qapi/qmp/json-parser.h | 4 +- include/qapi/qmp/json-streamer.h | 16 +++- monitor.c | 2 +- qga/main.c | 2 +- qobject/json-lexer.c | 22 ++--- qobject/json-parser.c | 189 +++++++++++++++------------------------ qobject/json-streamer.c | 48 +++++----- qobject/qjson.c | 2 +- tests/libqtest.c | 2 +- 10 files changed, 125 insertions(+), 169 deletions(-)
Signature · 1 line
2.5.0
PATCH v2 1/4 qjson: replace QString in JSONLexer with GString
JSONLexer only needs a simple resizable buffer. json-streamer.c can allocate memory for each token instead of relying on reference counting of QStrings.
--- include/qapi/qmp/json-lexer.h | 7 +++---- include/qapi/qmp/json-streamer.h | 1 + qobject/json-lexer.c | 22 ++++++++-------------- qobject/json-streamer.c | 10 +++++----- 4 files changed, 17 insertions(+), 23 deletions(-)
include/qapi/qmp/json-lexer.h +3 -4
@@ -14,8 +14,7 @@ #ifndef QEMU_JSON_LEXER_H #define QEMU_JSON_LEXER_H -#include "qapi/qmp/qstring.h" -#include "qapi/qmp/qlist.h" +#include "glib-compat.h" typedef enum json_token_type { JSON_OPERATOR = 100,
@@ -30,13 +29,13 @@ typedef enum json_token_type {typedef struct JSONLexer JSONLexer ; -typedef void (JSONLexerEmitter )(JSONLexer *,QString *,JSONTokenType ,int x,int y); +typedef void (JSONLexerEmitter )(JSONLexer *,GString *,JSONTokenType ,int x,int y);struct JSONLexer {JSONLexerEmitter *emit;int state; -QString *token; +GString *token;int x, y; };
include/qapi/qmp/json-streamer.h +1 -0
@@ -14,6 +14,7 @@#ifndef QEMU_JSON_STREAMER_H #define QEMU_JSON_STREAMER_H +#include <stdint.h> #include "qapi/qmp/qlist.h" #include "qapi/qmp/json-lexer.h"
qobject/json-lexer.c +8 -14
@@ -11,12 +11,9 @@* */ -#include "qapi/qmp/qstring.h" -#include "qapi/qmp/qlist.h" -#include "qapi/qmp/qdict.h" -#include "qapi/qmp/qint.h" #include "qemu-common.h" #include "qapi/qmp/json-lexer.h" +#include <stdint.h> #define MAX_TOKEN_SIZE (64ULL << 20)
@@ -272,7 +269,7 @@ void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func) { lexer->emit = func; lexer->state =IN_START ; - lexer->token =qstring_new (); + lexer->token =g_string_sized_new (3 ); lexer->x = lexer->y =0 ; }
@@ -290,7 +287,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush) new_state = json_lexer[lexer->state][(uint8_t )ch]; char_consumed = !TERMINAL_NEEDED_LOOKAHEAD (lexer->state, new_state);if (char_consumed) { -qstring_append_chr (lexer->token, ch); +g_string_append_c (lexer->token, ch); }switch (new_state) {
@@ -303,8 +300,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush) lexer->emit (lexer, lexer->token, new_state, lexer->x, lexer->y);/* fall through */ case JSON_SKIP : -QDECREF (lexer->token); - lexer->token =qstring_new (); +g_string_truncate (lexer->token,0 ); new_state =IN_START ;break ;case IN_ERROR :
@@ -322,8 +318,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)* induce an error/flush state. */ lexer->emit (lexer, lexer->token,JSON_ERROR , lexer->x, lexer->y); -QDECREF (lexer->token); - lexer->token =qstring_new (); +g_string_truncate (lexer->token,0 ); new_state =IN_START ; lexer->state = new_state;return 0 ;
@@ -336,10 +331,9 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)/* Do not let a single token grow to an arbitrarily large size, * this is a security consideration. */ -if (lexer->token->length >MAX_TOKEN_SIZE ) { +if (lexer->token->len >MAX_TOKEN_SIZE ) { lexer->emit (lexer, lexer->token, lexer->state, lexer->x, lexer->y); -QDECREF (lexer->token); - lexer->token =qstring_new (); +g_string_truncate (lexer->token,0 ); lexer->state =IN_START ; }
@@ -369,5 +363,5 @@ int json_lexer_flush(JSONLexer *lexer)void json_lexer_destroy (JSONLexer *lexer) { -QDECREF (lexer->token); +g_string_free (lexer->token, true); }
qobject/json-streamer.c +5 -5
@@ -12,6 +12,7 @@*/ #include "qapi/qmp/qlist.h" +#include "qapi/qmp/qstring.h" #include "qapi/qmp/qint.h" #include "qapi/qmp/qdict.h" #include "qemu-common.h"
@@ -21,13 +22,13 @@#define MAX_TOKEN_SIZE (64ULL << 20)#define MAX_NESTING (1ULL << 10) -static void json_message_process_token (JSONLexer *lexer,QString *token,JSONTokenType type,int x,int y) +static void json_message_process_token (JSONLexer *lexer,GString *input,JSONTokenType type,int x,int y) {JSONMessageParser *parser =container_of (lexer, JSONMessageParser, lexer);QDict *dict;if (type ==JSON_OPERATOR ) { -switch (qstring_get_str (token)[0 ]) { +switch (input->str[0 ]) {case '{' : parser->brace_count++;break ;
@@ -47,12 +48,11 @@ static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTok dict =qdict_new ();qdict_put (dict,"type" ,qint_from_int (type)); -QINCREF (token); -qdict_put (dict,"token" , token); +qdict_put (dict,"token" ,qstring_from_str (input->str));qdict_put (dict,"x" ,qint_from_int (x));qdict_put (dict,"y" ,qint_from_int (y)); - parser->token_size += token->length; + parser->token_size += input->len;qlist_append (parser->tokens, dict);
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH v2 1/4] qjson: replace QString in JSONLexer with GString
Quoted 3 lines · Paolo Bonzini
JSONLexer only needs a simple resizable buffer.
json-streamer.ccan allocate memory for each token instead of relying on reference counting of QStrings.
On its own, this doesn't really save anything, it merely decouples the lexer's buffer from the rest of the machinery, which still allocates the same QStrings as before. But it enables the rest of your work.
Quoted 43 lines
Signed-off-by Paolo Bonzini <[email protected]>--- include/qapi/qmp/json-lexer.h | 7 +++---- include/qapi/qmp/json-streamer.h | 1 + qobject/json-lexer.c | 22 ++++++++-------------- qobject/json-streamer.c | 10 +++++----- 4 files changed, 17 insertions(+), 23 deletions(-)include/qapi/qmp/json-lexer.h +3
-4@@ -14,8 +14,7 @@ #ifndef QEMU_JSON_LEXER_H #define QEMU_JSON_LEXER_H -#include "qapi/qmp/qstring.h" -#include "qapi/qmp/qlist.h" +#include "glib-compat.h" typedef enum json_token_type { JSON_OPERATOR = 100,@@ -30,13 +29,13 @@ typedef enum json_token_type {typedef struct JSONLexer JSONLexer ; -typedef void (JSONLexerEmitter )(JSONLexer *,QString *,JSONTokenType ,int x,int y); +typedef void (JSONLexerEmitter )(JSONLexer *,GString *,JSONTokenType ,int x,int y);struct JSONLexer {JSONLexerEmitter *emit;int state; -QString *token; +GString *token;int x, y; };include/qapi/qmp/json-streamer.h +0
-0
include/qapi/qmp/json-streamer.h line 17
Quoted 4 lines
include/qapi/qmp/json-streamer.h +1
-0@@ -14,6 +14,7 @@#ifndef QEMU_JSON_STREAMER_H #define QEMU_JSON_STREAMER_H +#include <stdint.h>
I guess you need this because json-lexer.h no longer includes it via qstring.h. The only use appars to be uint64_t token_size, where size_t would be more appropriate. Follow-up cleanup.
Quoted 22 lines
include/qapi/qmp/json-streamer.h +0
-0#include "qapi/qmp/qlist.h" #include "qapi/qmp/json-lexer.h" qobject/json-lexer.c +1
-4@@ -11,12 +11,9 @@* */ -#include "qapi/qmp/qstring.h" -#include "qapi/qmp/qlist.h" -#include "qapi/qmp/qdict.h" -#include "qapi/qmp/qint.h" #include "qemu-common.h" #include "qapi/qmp/json-lexer.h" +#include <stdint.h> #define MAX_TOKEN_SIZE (64ULL << 20)
qobject/json-lexer.c void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func) · line 272
Quoted 5 lines
qobject/json-lexer.c +1
-1@@ -272,7 +269,7 @@ void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func) { lexer->emit = func; lexer->state =IN_START ; - lexer->token =qstring_new (); + lexer->token =g_string_sized_new (3 );
Where does the 3 come from?
As long as g_string_truncate() doesn't reallocate, lexer->token will grow to hold the largest token seen so far, and stay there.
Why not MAX_TOKEN_SIZE and be done with it? Oh, it's 64 MiB. Ookay, I'd call that... generous.
Quoted 13 lines
qobject/json-lexer.c +1
-1lexer->x = lexer->y =0 ; }@@ -290,7 +287,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush) new_state = json_lexer[lexer->state][(uint8_t )ch]; char_consumed = !TERMINAL_NEEDED_LOOKAHEAD (lexer->state, new_state);if (char_consumed) { -qstring_append_chr (lexer->token, ch); +g_string_append_c (lexer->token, ch); }switch (new_state) {
qobject/json-lexer.c static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush) · line 305
Quoted 8 lines
qobject/json-lexer.c +1
-2@@ -303,8 +300,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush) lexer->emit (lexer, lexer->token, new_state, lexer->x, lexer->y);/* fall through */ case JSON_SKIP : -QDECREF (lexer->token); - lexer->token =qstring_new (); +g_string_truncate (lexer->token,0 ); new_state =IN_START ;break ;
Before your patch, we effectively hand QString * lexer->token off to whatever emit() is.
Now, it's a GString, and we want it back.
Okay, because below the obfuscating abstraction, emit() is json_message_process_token(), and you update it accordingly, see last patch hunk.
qobject/json-lexer.c static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush) · line 324
Quoted 11 lines
qobject/json-lexer.c +1
-2case IN_ERROR :@@ -322,8 +318,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)* induce an error/flush state. */ lexer->emit (lexer, lexer->token,JSON_ERROR , lexer->x, lexer->y); -QDECREF (lexer->token); - lexer->token =qstring_new (); +g_string_truncate (lexer->token,0 ); new_state =IN_START ; lexer->state = new_state;return 0 ;
Likewise.
qobject/json-lexer.c static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush) · line 339
Quoted 13 lines
qobject/json-lexer.c +2
-3@@ -336,10 +331,9 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)/* Do not let a single token grow to an arbitrarily large size, * this is a security consideration. */ -if (lexer->token->length >MAX_TOKEN_SIZE ) { +if (lexer->token->len >MAX_TOKEN_SIZE ) { lexer->emit (lexer, lexer->token, lexer->state, lexer->x, lexer->y); -QDECREF (lexer->token); - lexer->token =qstring_new (); +g_string_truncate (lexer->token,0 ); lexer->state =IN_START ; }
Likewise.
Preexisting: splitting long tokens with an axe like that is wrong. If the token is too long, we should error out. Outside this patch's scope.
Quoted 39 lines
qobject/json-lexer.c +1
-1@@ -369,5 +363,5 @@ int json_lexer_flush(JSONLexer *lexer)void json_lexer_destroy (JSONLexer *lexer) { -QDECREF (lexer->token); +g_string_free (lexer->token, true); }qobject/json-streamer.c +3
-2@@ -12,6 +12,7 @@*/ #include "qapi/qmp/qlist.h" +#include "qapi/qmp/qstring.h" #include "qapi/qmp/qint.h" #include "qapi/qmp/qdict.h" #include "qemu-common.h" @@ -21,13 +22,13 @@#define MAX_TOKEN_SIZE (64ULL << 20)#define MAX_NESTING (1ULL << 10) -static void json_message_process_token (JSONLexer *lexer,QString *token,JSONTokenType type,int x,int y) +static void json_message_process_token (JSONLexer *lexer,GString *input,JSONTokenType type,int x,int y) {JSONMessageParser *parser =container_of (lexer, JSONMessageParser, lexer);QDict *dict;if (type ==JSON_OPERATOR ) { -switch (qstring_get_str (token)[0 ]) { +switch (input->str[0 ]) {case '{' : parser->brace_count++;break ;@@ -47,12 +48,11 @@ static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTok dict =qdict_new ();qdict_put (dict,"type" ,qint_from_int (type));
qobject/json-streamer.c static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTok · line 57
Quoted 10 lines
qobject/json-streamer.c +2
-3-QINCREF (token); -qdict_put (dict,"token" , token); +qdict_put (dict,"token" ,qstring_from_str (input->str));qdict_put (dict,"x" ,qint_from_int (x));qdict_put (dict,"y" ,qint_from_int (y)); - parser->token_size += token->length; + parser->token_size += input->len;qlist_append (parser->tokens, dict);
Before your patch: take a reference to token.
Now: copy it out. Good.
Preexisting: storing the token's textual representation is necessary for strings, defensible for numbers, and a total waste for everything else. But we can tolerate some waste. Just not the *egregious* waste this series fixes.
Re: [Qemu-devel] [PATCH v2 1/4] qjson: replace QString in JSONLexer with GString
Quoted 1 line · Markus Armbruster
Where does the 3 come from?
My hat. :) I don't know---it was in the old patches, and I just brought it on.
Oh, found it. GString adds 1 to the size (for the \0); allocating 4 bytes probably seemed like a good idea at the time.
However, it also computes the power of two that is >= size + 1, and g_string_new uses 2 as the default length so it _also_ results in allocating four bytes (ceil_pow2(2+1) == 4).
Paolo
Quoted 6 lines
As long as
g_string_truncate()doesn't reallocate,lexer->tokenwill grow to hold the largest token seen so far, and stay there.Why not
MAX_TOKEN_SIZEand be done with it? Oh, it's 64 MiB. Ookay, I'd call that... generous.
PATCH v2 2/4 qjson: do not save/restore contexts
In reply to Paolo BonziniJSON is LL(1) and our parser indeed needs only 1 token lookahead. Saving the parser context is mostly unnecessary; we can replace it with peeking at the next token, or remove it altogether when the restore only happens on errors. The token list is destroyed anyway on errors.
The only interesting thing is that parse_keyword always eats a TOKEN_KEYWORD, even if it is invalid, so it must come last in parse_value (otherwise, NULL is returned, parse_literal is invoked and it tries to peek beyond end of input). This is caught by /errors/unterminated/literal, which actually checks for an unterminated keyword. ಠ_ಠ
--- qobject/json-parser.c | 59 ++++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-)
qobject/json-parser.c +21 -38
@@ -296,23 +296,6 @@ static QObject *parser_context_peek_token(JSONParserContext *ctxt)return token; } -static JSONParserContext parser_context_save (JSONParserContext *ctxt) -{ -JSONParserContext saved_ctxt = {0 }; - saved_ctxt.tokens.pos = ctxt->tokens.pos; - saved_ctxt.tokens.count = ctxt->tokens.count; - saved_ctxt.tokens.buf = ctxt->tokens.buf; -return saved_ctxt; -} - -static void parser_context_restore (JSONParserContext *ctxt, -JSONParserContext saved_ctxt) -{ - ctxt->tokens.pos = saved_ctxt.tokens.pos; - ctxt->tokens.count = saved_ctxt.tokens.count; - ctxt->tokens.buf = saved_ctxt.tokens.buf; -} -static void tokens_append_from_iter (QObject *obj,void *opaque) {JSONParserContext *ctxt = opaque;
@@ -364,7 +347,6 @@ static void parser_context_free(JSONParserContext *ctxt)static int parse_pair (JSONParserContext *ctxt,QDict *dict,va_list *ap) {QObject *key =NULL , *token =NULL , *value, *peek; -JSONParserContext saved_ctxt =parser_context_save (ctxt); peek =parser_context_peek_token (ctxt);if (peek ==NULL ) {
@@ -402,7 +384,6 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)return 0 ; out: -parser_context_restore (ctxt, saved_ctxt);qobject_decref (key);return -1 ;
@@ -412,9 +393,8 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) {QDict *dict =NULL ;QObject *token, *peek; -JSONParserContext saved_ctxt =parser_context_save (ctxt); - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; }
@@ -425,6 +405,7 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) dict =qdict_new (); +parser_context_pop_token (ctxt); peek =parser_context_peek_token (ctxt);if (peek ==NULL ) {parse_error (ctxt,NULL ,"premature EOI" );
@@ -465,7 +446,6 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)return QOBJECT (dict); out: -parser_context_restore (ctxt, saved_ctxt);QDECREF (dict);return NULL ; }
@@ -474,9 +454,8 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap) {QList *list =NULL ;QObject *token, *peek; -JSONParserContext saved_ctxt =parser_context_save (ctxt); - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; }
@@ -487,6 +466,7 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap) list =qlist_new (); +parser_context_pop_token (ctxt); peek =parser_context_peek_token (ctxt);if (peek ==NULL ) {parse_error (ctxt,NULL ,"premature EOI" );
@@ -537,7 +517,6 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)return QOBJECT (list); out: -parser_context_restore (ctxt, saved_ctxt);QDECREF (list);return NULL ; }
@@ -545,9 +524,8 @@ out:static QObject *parse_keyword (JSONParserContext *ctxt) {QObject *token, *ret; -JSONParserContext saved_ctxt =parser_context_save (ctxt); - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; }
@@ -556,6 +534,7 @@ static QObject *parse_keyword(JSONParserContext *ctxt) goto out; } +parser_context_pop_token (ctxt);if (token_is_keyword (token,"true" )) { ret =QOBJECT (qbool_from_bool (true)); }else if (token_is_keyword (token,"false" )) {
@@ -570,7 +549,6 @@ static QObject *parse_keyword(JSONParserContext *ctxt)return ret; out: -parser_context_restore (ctxt, saved_ctxt);return NULL ; }
@@ -578,17 +556,21 @@ out:static QObject *parse_escape (JSONParserContext *ctxt,va_list *ap) {QObject *token =NULL , *obj; -JSONParserContext saved_ctxt =parser_context_save (ctxt);if (ap ==NULL ) { goto out; } - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; } +if (token_get_type (token) !=JSON_ESCAPE ) { + goto out; + } + +parser_context_pop_token (ctxt);if (token_is_escape (token,"%p" )) { obj = va_arg(*ap, QObject *); }else if (token_is_escape (token,"%i" )) {
@@ -611,7 +593,6 @@ static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)return obj; out: -parser_context_restore (ctxt, saved_ctxt);return NULL ; }
@@ -619,15 +600,15 @@ out:static QObject *parse_literal (JSONParserContext *ctxt) {QObject *token, *obj; -JSONParserContext saved_ctxt =parser_context_save (ctxt); - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; }switch (token_get_type (token)) {case JSON_STRING : +parser_context_pop_token (ctxt); obj =QOBJECT (qstring_from_escaped_str (ctxt, token));break ;case JSON_INTEGER : {
@@ -645,15 +626,18 @@ static QObject *parse_literal(JSONParserContext *ctxt)*/ int64_t value; +parser_context_pop_token (ctxt); errno =0 ;/* strtoll doesn't set errno on success */ value =strtoll (token_get_value (token),NULL ,10 );if (errno !=ERANGE ) { obj =QOBJECT (qint_from_int (value));break ; } -/* fall through to JSON_FLOAT */ + goto parse_float; }case JSON_FLOAT : +parser_context_pop_token (ctxt); + parse_float:/* FIXME dependent on locale */ obj =QOBJECT (qfloat_from_double (strtod (token_get_value (token),NULL )));break ;
@@ -664,7 +648,6 @@ static QObject *parse_literal(JSONParserContext *ctxt)return obj; out: -parser_context_restore (ctxt, saved_ctxt);return NULL ; }
@@ -681,11 +664,11 @@ static QObject *parse_value(JSONParserContext *ctxt, va_list *ap) obj =parse_escape (ctxt, ap); }if (obj ==NULL ) { - obj =parse_keyword (ctxt); - } -if (obj ==NULL ) { obj =parse_literal (ctxt); } +if (obj ==NULL ) { + obj =parse_keyword (ctxt); + }return obj; }
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
Quoted 6 lines · Paolo Bonzini
JSON is LL(1) and our parser indeed needs only 1 token lookahead. Saving the parser context is mostly unnecessary; we can replace it with peeking at the next token, or remove it altogether when the restore only happens on errors. The token list is destroyed anyway on errors.
Quoted 6 lines
The only interesting thing is that
parse_keywordalways eats aTOKEN_KEYWORD, even if it is invalid, so it must come last inparse_value(otherwise, NULL is returned,parse_literalis invoked and it tries to peek beyond end of input). This is caught by /errors/unterminated/literal, which actually checks for an unterminated keyword. ಠ_ಠ
Is it accepted practice to put UTF-8 in commit messages? (Or, actually, anywhere in patches, except maybe the notes section?)
I'd recommend o_O.
Thanks
Laszlo
Quoted 225 lines
Signed-off-by Paolo Bonzini <[email protected]>--- qobject/json-parser.c | 59 ++++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-)qobject/json-parser.c +21
-38@@ -296,23 +296,6 @@ static QObject *parser_context_peek_token(JSONParserContext *ctxt)return token; } -static JSONParserContext parser_context_save (JSONParserContext *ctxt) -{ -JSONParserContext saved_ctxt = {0 }; - saved_ctxt.tokens.pos = ctxt->tokens.pos; - saved_ctxt.tokens.count = ctxt->tokens.count; - saved_ctxt.tokens.buf = ctxt->tokens.buf; -return saved_ctxt; -} - -static void parser_context_restore (JSONParserContext *ctxt, -JSONParserContext saved_ctxt) -{ - ctxt->tokens.pos = saved_ctxt.tokens.pos; - ctxt->tokens.count = saved_ctxt.tokens.count; - ctxt->tokens.buf = saved_ctxt.tokens.buf; -} -static void tokens_append_from_iter (QObject *obj,void *opaque) {JSONParserContext *ctxt = opaque;@@ -364,7 +347,6 @@ static void parser_context_free(JSONParserContext *ctxt)static int parse_pair (JSONParserContext *ctxt,QDict *dict,va_list *ap) {QObject *key =NULL , *token =NULL , *value, *peek; -JSONParserContext saved_ctxt =parser_context_save (ctxt); peek =parser_context_peek_token (ctxt);if (peek ==NULL ) {@@ -402,7 +384,6 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)return 0 ; out: -parser_context_restore (ctxt, saved_ctxt);qobject_decref (key);return -1 ;@@ -412,9 +393,8 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) {QDict *dict =NULL ;QObject *token, *peek; -JSONParserContext saved_ctxt =parser_context_save (ctxt); - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; }@@ -425,6 +405,7 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) dict =qdict_new (); +parser_context_pop_token (ctxt); peek =parser_context_peek_token (ctxt);if (peek ==NULL ) {parse_error (ctxt,NULL ,"premature EOI" );@@ -465,7 +446,6 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)return QOBJECT (dict); out: -parser_context_restore (ctxt, saved_ctxt);QDECREF (dict);return NULL ; }@@ -474,9 +454,8 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap) {QList *list =NULL ;QObject *token, *peek; -JSONParserContext saved_ctxt =parser_context_save (ctxt); - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; }@@ -487,6 +466,7 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap) list =qlist_new (); +parser_context_pop_token (ctxt); peek =parser_context_peek_token (ctxt);if (peek ==NULL ) {parse_error (ctxt,NULL ,"premature EOI" );@@ -537,7 +517,6 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)return QOBJECT (list); out: -parser_context_restore (ctxt, saved_ctxt);QDECREF (list);return NULL ; }@@ -545,9 +524,8 @@ out:static QObject *parse_keyword (JSONParserContext *ctxt) {QObject *token, *ret; -JSONParserContext saved_ctxt =parser_context_save (ctxt); - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; }@@ -556,6 +534,7 @@ static QObject *parse_keyword(JSONParserContext *ctxt) goto out; } +parser_context_pop_token (ctxt);if (token_is_keyword (token,"true" )) { ret =QOBJECT (qbool_from_bool (true)); }else if (token_is_keyword (token,"false" )) {@@ -570,7 +549,6 @@ static QObject *parse_keyword(JSONParserContext *ctxt)return ret; out: -parser_context_restore (ctxt, saved_ctxt);return NULL ; }@@ -578,17 +556,21 @@ out:static QObject *parse_escape (JSONParserContext *ctxt,va_list *ap) {QObject *token =NULL , *obj; -JSONParserContext saved_ctxt =parser_context_save (ctxt);if (ap ==NULL ) { goto out; } - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; } +if (token_get_type (token) !=JSON_ESCAPE ) { + goto out; + } + +parser_context_pop_token (ctxt);if (token_is_escape (token,"%p" )) { obj = va_arg(*ap, QObject *); }else if (token_is_escape (token,"%i" )) {@@ -611,7 +593,6 @@ static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)return obj; out: -parser_context_restore (ctxt, saved_ctxt);return NULL ; }@@ -619,15 +600,15 @@ out:static QObject *parse_literal (JSONParserContext *ctxt) {QObject *token, *obj; -JSONParserContext saved_ctxt =parser_context_save (ctxt); - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; }switch (token_get_type (token)) {case JSON_STRING : +parser_context_pop_token (ctxt); obj =QOBJECT (qstring_from_escaped_str (ctxt, token));break ;case JSON_INTEGER : {@@ -645,15 +626,18 @@ static QObject *parse_literal(JSONParserContext *ctxt)*/ int64_t value; +parser_context_pop_token (ctxt); errno =0 ;/* strtoll doesn't set errno on success */ value =strtoll (token_get_value (token),NULL ,10 );if (errno !=ERANGE ) { obj =QOBJECT (qint_from_int (value));break ; } -/* fall through to JSON_FLOAT */ + goto parse_float; }case JSON_FLOAT : +parser_context_pop_token (ctxt); + parse_float:/* FIXME dependent on locale */ obj =QOBJECT (qfloat_from_double (strtod (token_get_value (token),NULL )));break ;@@ -664,7 +648,6 @@ static QObject *parse_literal(JSONParserContext *ctxt)return obj; out: -parser_context_restore (ctxt, saved_ctxt);return NULL ; }@@ -681,11 +664,11 @@ static QObject *parse_value(JSONParserContext *ctxt, va_list *ap) obj =parse_escape (ctxt, ap); }if (obj ==NULL ) { - obj =parse_keyword (ctxt); - } -if (obj ==NULL ) { obj =parse_literal (ctxt); } +if (obj ==NULL ) { + obj =parse_keyword (ctxt); + }return obj; }
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
Quoted 10 lines · Laszlo Ersek
Quoted 6 lines
Quoted 6 lines
The only interesting thing is that
parse_keywordalways eats aTOKEN_KEYWORD, even if it is invalid, so it must come last inparse_value(otherwise, NULL is returned,parse_literalis invoked and it tries to peek beyond end of input). This is caught by /errors/unterminated/literal, which actually checks for an unterminated keyword. ಠ_ಠIs it accepted practice to put UTF-8 in commit messages? (Or, actually, anywhere in patches, except maybe the notes section?)
I'd recommend
o_O.
Perhaps not Kannada, but Unicode 0xA1 to 0x1FF is pretty common in commit messages.
Paolo
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Laszlo Ersek
Quoted 6 lines
Quoted 6 lines
The only interesting thing is that
parse_keywordalways eats aTOKEN_KEYWORD, even if it is invalid, so it must come last inparse_value(otherwise, NULL is returned,parse_literalis invoked and it tries to peek beyond end of input). This is caught by /errors/unterminated/literal, which actually checks for an unterminated keyword. ಠ_ಠ
Quoted 7 lines
Quoted 4 lines
Is it accepted practice to put UTF-8 in commit messages? (Or, actually, anywhere in patches, except maybe the notes section?)
I'd recommend
o_O.Perhaps not Kannada, but Unicode
0xA1to0x1FFis pretty common in commit messages.
That seems to be a subset of the union of the following three:
https://en.wikipedia.org/wiki/Latin-1_Supplement_%28Unicode_block%29
https://en.wikipedia.org/wiki/Latin_Extended-A
https://en.wikipedia.org/wiki/Latin_Extended-B
Thanks for the info,
Laszlo
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
In reply to Laszlo ErsekQuoted 14 lines · Laszlo Ersek
Quoted 6 lines · Paolo Bonzini
JSON is LL(1) and our parser indeed needs only 1 token lookahead. Saving the parser context is mostly unnecessary; we can replace it with peeking at the next token, or remove it altogether when the restore only happens on errors. The token list is destroyed anyway on errors.
Quoted 6 lines
The only interesting thing is that
parse_keywordalways eats aTOKEN_KEYWORD, even if it is invalid, so it must come last inparse_value(otherwise, NULL is returned,parse_literalis invoked and it tries to peek beyond end of input). This is caught by /errors/unterminated/literal, which actually checks for an unterminated keyword. ಠ_ಠ
Quoted 3 lines
Is it accepted practice to put UTF-8 in commit messages? (Or, actually, anywhere in patches, except maybe the notes section?)
Git handles UTF-8 just fine (and for any other encoding, properly transmitted in the email, git transcodes to UTF-8 before writing it into the repository).
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
Quoted 15 lines · Eric Blake
Quoted 14 lines · Laszlo Ersek
Quoted 12 lines · Paolo Bonzini
JSON is LL(1) and our parser indeed needs only 1 token lookahead. Saving the parser context is mostly unnecessary; we can replace it with peeking at the next token, or remove it altogether when the restore only happens on errors. The token list is destroyed anyway on errors.
The only interesting thing is that
parse_keywordalways eats aTOKEN_KEYWORD, even if it is invalid, so it must come last inparse_value(otherwise, NULL is returned,parse_literalis invoked and it tries to peek beyond end of input). This is caught by /errors/unterminated/literal, which actually checks for an unterminated keyword. ಠ_ಠ
Quoted 8 lines
Quoted 3 lines
Is it accepted practice to put UTF-8 in commit messages? (Or, actually, anywhere in patches, except maybe the notes section?)
Git handles UTF-8 just fine (and for any other encoding, properly transmitted in the email, git transcodes to UTF-8 before writing it into the repository).
Yes, I know. I use latin2:
$ locale
LANG=
LC_CTYPE=hu_HU.ISO8859-2
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
and from my git config:
[i18n]
logOutputEncoding = latin2
commitencoding = latin2
This works very well -- as long as it doesn't choke on something outside of latin2 --, both the glibc locale support and git are doing their jobs perfectly fine; my question concerned any other users who decided to stay with single-byte encodings (with an ASCII subset).
(I believe that RFCs stick with ASCII to this day, and I also think that our source code and docs/ should stick with ASCII; but I know I can't plausibly argue for the same in commit messages, assuming I'm alone with that anyway.
BTW I should have written "non-ASCII Unicode code points" in my original question, rather than "UTF-8".)
Thanks!
Laszlo
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
In reply to Laszlo ErsekHi,
Quoted 2 lines
Is it accepted practice to put UTF-8 in commit messages? (Or, actually, anywhere in patches, except maybe the notes section?)
Sure. We don't want limit people names (in signed-off etc) to us-ascii.
See "eb8934b vnc: fix memory corruption (CVE-2015-5225)" for a name written in kanji.
Quoted 1 line
I'd recommend
o_O.
Heh, it's 2015, not 1995 ...
cheers,
Gerd
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
Quoted 9 lines · Gerd Hoffmann
Hi,
Quoted 2 lines
Is it accepted practice to put UTF-8 in commit messages? (Or, actually, anywhere in patches, except maybe the notes section?)
Sure. We don't want limit people names (in signed-off etc) to us-ascii.
See "eb8934b vnc: fix memory corruption (CVE-2015-5225)" for a name written in kanji.
I'm very sorry, but this is something I expressly disagree with. (I didn't want to bring this up on my own, but since you did...)
International engineering / science / research etc. are being done in English. We use English because we expect people to learn one common language (native English speakers have it easy, but that's a side point), so that everyone not have to learn every possible language.
The same point applies *much more* to writing systems / alphabets. You (the generic you) can't expect me (the generic me) to read Kanji, Sanskrit, Thai script, Cyrillic script, and so on, even if your name is written in that language natively. You come up with an approximation in Latin script, and use that.
Is your purpose to feel pleased about the faithful representation of your name in the commit message (that the international community is unable to read, not even approximately), or is your goal to allow the community to read your (approximate) name?
I bet everyone who is involved in international development, and travels occasionally, has business cards in Latin script *too*. I bet whoever does research and publishes papers in English puts their name (or at least an official approximation of it) on the front page in Latin script *too*.
Specifically about the commit you mention, the email of the reporter is:
I'm absolutely sure that "zuozhi" is the official Pinyin transliteration of the reporter's name (or a part of it). Now, while Pinyin has its own separate pronunciation rules, I *can* (and occasionally do) look up those rules. So Pinyin allows me to *work* with the name with relative safety, and it even gives me a fleeting chance at getting the pronunciation right, should we meet.
My name is László Érsek. I've dropped the accents for the purpose of international exchange in advance; I just write Laszlo Ersek, even when I sign physical documents that are in English.
Even that way, I've seen the larger community abuse my name endlessly; in particular I've seen all permutations (= reordering) and variations (= missing characters) of the substring "szl" in "Laszlo".
If the larger community fails to get such a simple ASCII name right -- and yes I'm at fault too, I have occasionally left off the second "n" of your last name --, then why am I (or anyone else) expected to struggle with names written in non-Latin script? They are much harder, and have exactly zero value, as far as international collaboration is concerned.
The development is being done in English, and the script of English is Latin. Stick with it.
Quoted 3 lines
Quoted 1 line
I'd recommend
o_O.Heh, it's 2015, not 1995 ...
Sure, and the Internet Standards are still being written in pure ASCII.
https://en.wikipedia.org/wiki/Request_for_Comments#Obtaining_RFCs
Laszlo
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
Quoted 13 lines · Laszlo Ersek
You
(the generic you) can't expect me (the generic me) to read Kanji, Sanskrit, Thai script, Cyrillic script, and so on, even if your name is written in that language natively. You come up with an approximation in Latin script, and use that.Is your purpose to feel pleased about the faithful representation of your name in the commit message (that the international community is unable to read, not even approximately), or is your goal to allow the community to read your (approximate) name?
Specifically about the commit you mention, the email of the reporter is:
Quoted 8 lines
I'm absolutely sure that "zuozhi" is the official Pinyin transliteration of the reporter's name (or a part of it). Now, while Pinyin has its own separate pronunciation rules, I *can* (and occasionally do) look up those rules. So Pinyin allows me to *work* with the name with relative safety, and it even gives me a fleeting chance at getting the pronunciation right, should we meet.
I think this is getting into dangerous territory. :)
Chinese language and names are very different from ours and it's possible that a person for some reason is very attached to the particular hanzi (kanji is Japanese :)) that are used for their name. (Fam, correct me if I'm wrong).
I've also seen people who have adopted an alternative English name and still want to include the Chinese name somewhere, at which point it's understandable that they write the latter with hanzi.
Certainly it's better if you get something like these:
but if the email is understandable I have no problem with
or in the case of an English name any of
where in the last case the email doesn't give the full Chinese name, but there is an alternative for people who cannot read Chinese characters.
For some reason this almost never happens with Japanese developers, but
https://en.wikipedia.org/wiki/Japanese_name#Difficulty_of_reading_names
suggests that they may also appreciate using kanji for their names in commit messages.
Thanks,
Paolo
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
Quoted 14 lines · Paolo Bonzini
Quoted 11 lines · Laszlo Ersek
You
(the generic you) can't expect me (the generic me) to read Kanji, Sanskrit, Thai script, Cyrillic script, and so on, even if your name is written in that language natively. You come up with an approximation in Latin script, and use that.Is your purpose to feel pleased about the faithful representation of your name in the commit message (that the international community is unable to read, not even approximately), or is your goal to allow the community to read your (approximate) name?
Quoted 6 lines
Quoted 6 lines
Specifically about the commit you mention, the email of the reporter is:
I'm absolutely sure that "zuozhi" is the official Pinyin transliteration of the reporter's name (or a part of it). Now, while Pinyin has its own
Yes, it's the Pinyin of 祚至, his given name. It's a peculiar and literary choice of name. :)
Quoted 31 lines
Quoted 4 lines
separate pronunciation rules, I *can* (and occasionally do) look up those rules. So Pinyin allows me to *work* with the name with relative safety, and it even gives me a fleeting chance at getting the pronunciation right, should we meet.
I think this is getting into dangerous territory. :)
Chinese language and names are very different from ours and it's possible that a person for some reason is very attached to the particular hanzi (kanji is Japanese :)) that are used for their name. (Fam, correct me if I'm wrong).
I've also seen people who have adopted an alternative English name and still want to include the Chinese name somewhere, at which point it's understandable that they write the latter with hanzi.
Certainly it's better if you get something like these:
Signed-off-by Gong Li (巩俐) <[email protected]>Signed-off-by 巩俐 (Gong Li) <[email protected]>but if the email is understandable I have no problem with
Signed-off-by 巩俐 <[email protected]>or in the case of an English name any of
Signed-off-by Jane Li (巩俐) <[email protected]>Signed-off-by 巩俐 (Jane Li) <[email protected]>Signed-off-by 巩俐 (Jane Li) <[email protected]>
Quoted 7 lines
where in the last case the email doesn't give the full Chinese name, but there is an alternative for people who cannot read Chinese characters.
For some reason this almost never happens with Japanese developers, but
https://en.wikipedia.org/wiki/Japanese_name#Difficulty_of_reading_names
suggests that they may also appreciate using kanji for their names in commit messages.
The problem for using Pinyin is, even I as a Chinese can't tell which is the last name out of a two-syllable name such as "Gong Li", because of obvious reasons.
Having hanzi together with Pinyin helps a lot, in that it tells me how to call it, should we meet. Not just in "which is the family name", but also which tone to use. In Chinese, you're not calling one's name if not using the right tone. For example "zuozhi" can be "左之" or "祚至" or one of other 100 combinations, which vary in tones. Personally I don't feel very comfortable when a Mandarin speaker calls me with a wrong tone. And I know that will happen from time to time if I stick to Pinyin rather than Fam. Thinking from the other way round, that may be a reason why they use hanzi.
All in all, in my opinion, only something like
, from which you can't easily infer the pronunciation, is apparently inconsiderate in an English context. Otherwise I think we should tolerate the usage of non-latin characters even if it means we have to copy&paste.
Fam
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
Quoted 7 lines · Fam Zheng
All in all, in my opinion, only something like
Signed-off-by 巩俐 <[email protected]>, from which you can't easily infer the pronunciation, is apparently inconsiderate in an English context. Otherwise I think we should tolerate the usage of non-latin characters even if it means we have to copy&paste.
FWIW, I totally agree.
Paolo
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
Quoted 10 lines · Paolo Bonzini
Quoted 7 lines · Fam Zheng
All in all, in my opinion, only something like
Signed-off-by 巩俐 <[email protected]>, from which you can't easily infer the pronunciation, is apparently inconsiderate in an English context. Otherwise I think we should tolerate the usage of non-latin characters even if it means we have to copy&paste.
FWIW, I totally agree.
Me too.
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
In reply to Laszlo ErsekHi,
Quoted 10 lines
The same point applies *much more* to writing systems / alphabets. You (the generic you) can't expect me (the generic me) to read Kanji, Sanskrit, Thai script, Cyrillic script, and so on, even if your name is written in that language natively. You come up with an approximation in Latin script, and use that.
Is your purpose to feel pleased about the faithful representation of your name in the commit message (that the international community is unable to read, not even approximately), or is your goal to allow the community to read your (approximate) name?
IMO that is for the people in question to decide. Usually I just cut +paste (or let stefans great patches script collect) what they are using them-self and are apparently comfortable with.
Having Kanji only as in that specific case is unusual indeed, in most cases there is latin transcript, either in place of or additionally to the native version, like this: "latin (native) <email>". The latter looks a bit silly for latin-family of alphabets, for others (kanji/cyrillic/...) it makes more sense and seems to be more common.
But even when they use native only I still think their name and not only the email address should appear in the commit message when giving them the credit they deserve.
cheers,
Gerd
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
In reply to Laszlo ErsekQuoted 1 line · Laszlo Ersek
My name is László Érsek.
OMG, my native name is actually "Érsek László". 95% of my written communication is in English, and this very discussion is occurring in English. So in this context I forgot that in Hungarian the family name goes first.
(Just goes on to show that I'm willing to mangle my name for collaboration's sake. Others should be too.)
Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts
In reply to Paolo BonziniQuoted 2 lines · Paolo Bonzini
JSON is LL(1) and our parser indeed needs only 1 token lookahead. Saving the parser context is mostly unnecessary;
"Mit Kanonen auf Spatzen" (shooting cannons at sparrows).
Quoted 32 lines
we can replace it
with peeking at the next token, or remove it altogether when the restore only happens on errors. The token list is destroyed anyway on errors.The only interesting thing is that
parse_keywordalways eats aTOKEN_KEYWORD, even if it is invalid, so it must come last inparse_value(otherwise, NULL is returned,parse_literalis invoked and it tries to peek beyond end of input). This is caught by /errors/unterminated/literal, which actually checks for an unterminated keyword. ಠ_ಠSigned-off-by Paolo Bonzini <[email protected]>--- qobject/json-parser.c | 59 ++++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-)qobject/json-parser.c +0
-7@@ -296,23 +296,6 @@ static QObject *parser_context_peek_token(JSONParserContext *ctxt)return token; } -static JSONParserContext parser_context_save (JSONParserContext *ctxt) -{ -JSONParserContext saved_ctxt = {0 }; - saved_ctxt.tokens.pos = ctxt->tokens.pos; - saved_ctxt.tokens.count = ctxt->tokens.count; - saved_ctxt.tokens.buf = ctxt->tokens.buf; -return saved_ctxt;
qobject/json-parser.c static QObject *parser_context_peek_token(JSONParserContext *ctxt) · line 315
Quoted 10 lines
qobject/json-parser.c +0
-10-} - -static void parser_context_restore (JSONParserContext *ctxt, -JSONParserContext saved_ctxt) -{ - ctxt->tokens.pos = saved_ctxt.tokens.pos; - ctxt->tokens.count = saved_ctxt.tokens.count; - ctxt->tokens.buf = saved_ctxt.tokens.buf; -} -
This saves and restores tokens, which is an array @buf of @count tokens with a cursor @pos.
@buf and count only ever change in parser_context_new(). Saving and restoring them has always been pointless.
What this actually does is saving and restoring the cursor.
Quoted 12 lines
qobject/json-parser.c +0
-1static void tokens_append_from_iter (QObject *obj,void *opaque) {JSONParserContext *ctxt = opaque;@@ -364,7 +347,6 @@ static void parser_context_free(JSONParserContext *ctxt)static int parse_pair (JSONParserContext *ctxt,QDict *dict,va_list *ap) {QObject *key =NULL , *token =NULL , *value, *peek; -JSONParserContext saved_ctxt =parser_context_save (ctxt); peek =parser_context_peek_token (ctxt);if (peek ==NULL ) {
qobject/json-parser.c static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap) · line 389
Quoted 7 lines
qobject/json-parser.c +0
-1@@ -402,7 +384,6 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)return 0 ; out: -parser_context_restore (ctxt, saved_ctxt);qobject_decref (key);return -1 ;
Before your patch: on error, we rewind the cursor to where it was on entry. Useful in a backtracking parser, but this shouldn't be one.
Now: we leave it wherever we error out.
Fine, because the sole caller parse_object() won't actually use it on error.
Quoted 20 lines
qobject/json-parser.c +2
-2@@ -412,9 +393,8 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) {QDict *dict =NULL ;QObject *token, *peek; -JSONParserContext saved_ctxt =parser_context_save (ctxt); - token =parser_context_pop_token (ctxt); + token =parser_context_peek_token (ctxt);if (token ==NULL ) { goto out; }@@ -425,6 +405,7 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) dict =qdict_new (); +parser_context_pop_token (ctxt); peek =parser_context_peek_token (ctxt);if (peek ==NULL ) {parse_error (ctxt,NULL ,"premature EOI" );
qobject/json-parser.c static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) · line 451
Quoted 7 lines
qobject/json-parser.c +0
-1@@ -465,7 +446,6 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)return QOBJECT (dict); out: -parser_context_restore (ctxt, saved_ctxt);QDECREF (dict);return NULL ; }
I'm not 100% when exactly parser_context_peek_token() returns null, but I think can see what your patch does anyway.
Before: if we can parse an object, advance cursor behind the object and return it, else leave the cursor alone and return null. The sole caller parse_value() relies on this behavior to try alternatives until one succeeds.
After: if we can parse an object, same as before, else advance the cursor to right before the offending token and return null.
Consider QMP input { 1 [ 2 ] }.
json_parser_parse_err():
parse_value() @ pos=0:
triy parse_object() @ pos=0:
consume '{'
parse_pair() @ pos=1:
parse_value() @ pos=1:
try ..., consume 1, now pos=2
fail with "key is not a string in object"
propagate failure
try parse_array() @ pos=2:
consume [ 2 ], return the QList, now pos=5
return the QList
throw away the error (caller json_parser_parse() passed errp=NULL) return the QList
Fortunately, that's not valid QMP, and we get
{"error": {"class": "GenericError", "desc": "Expected 'object' in QMP input"}}Shouldn't be hard to fix. we just have to complete the job of turning this thing into a recursive descent parser. I'll give it a shot.
[...]
PATCH v2 3/4 qjson: store tokens in a GQueue
In reply to Paolo BonziniEven though we still have the "streamer" concept, the tokens can now be deleted as they are read. While doing so convert from QList to GQueue, since the next step will make tokens not a QObject and we will have to do the conversion anyway.
--- include/qapi/qmp/json-parser.h | 4 +-- include/qapi/qmp/json-streamer.h | 8 ++--- monitor.c | 2 +- qga/main.c | 2 +- qobject/json-parser.c | 65 +++++++++++++--------------------------- qobject/json-streamer.c | 25 +++++++++------- qobject/qjson.c | 2 +- tests/libqtest.c | 2 +- 8 files changed, 45 insertions(+), 65 deletions(-)
include/qapi/qmp/json-parser.h +2 -2
@@ -18,7 +18,7 @@#include "qapi/qmp/qlist.h" #include "qapi/error.h" -QObject *json_parser_parse (QList *tokens,va_list *ap); -QObject *json_parser_parse_err (QList *tokens,va_list *ap,Error **errp); +QObject *json_parser_parse (GQueue *tokens,va_list *ap); +QObject *json_parser_parse_err (GQueue *tokens,va_list *ap,Error **errp);#endif
include/qapi/qmp/json-streamer.h +4 -4
@@ -15,21 +15,21 @@#define QEMU_JSON_STREAMER_H #include <stdint.h> -#include "qapi/qmp/qlist.h" +#include "glib-compat.h" #include "qapi/qmp/json-lexer.h" typedef struct JSONMessageParser { -void (*emit)(struct JSONMessageParser *parser,QList *tokens); +void (*emit)(struct JSONMessageParser *parser,GQueue *tokens);JSONLexer lexer;int brace_count;int bracket_count; -QList *tokens; +GQueue *tokens;uint64_t token_size; }JSONMessageParser ;void json_message_parser_init (JSONMessageParser *parser, -void (*func)(JSONMessageParser *,QList *)); +void (*func)(JSONMessageParser *,GQueue *));int json_message_parser_feed (JSONMessageParser *parser,const char *buffer,size_t size);
monitor.c +1 -1
@@ -3849,7 +3849,7 @@ static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)return input_dict; } -static void handle_qmp_command (JSONMessageParser *parser,QList *tokens) +static void handle_qmp_command (JSONMessageParser *parser,GQueue *tokens) {Error *local_err =NULL ;QObject *obj, *data;
qga/main.c +1 -1
@@ -570,7 +570,7 @@ static void process_command(GAState *s, QDict *req) }/* handle requests/control events coming in over the channel */ -static void process_event (JSONMessageParser *parser,QList *tokens) +static void process_event (JSONMessageParser *parser,GQueue *tokens) {GAState *s =container_of (parser, GAState, parser);QDict *qdict;
qobject/json-parser.c +20 -45
111 lines not on this page · raw message
qobject/json-streamer.c +15 -10
@@ -22,6 +22,14 @@#define MAX_TOKEN_SIZE (64ULL << 20)#define MAX_NESTING (1ULL << 10) +static void json_message_free_tokens (JSONMessageParser *parser) +{ +if (parser->tokens) { +g_queue_free (parser->tokens); + parser->tokens =NULL ; + } +} +static void json_message_process_token (JSONLexer *lexer,GString *input,JSONTokenType type,int x,int y) {JSONMessageParser *parser =container_of (lexer, JSONMessageParser, lexer);
@@ -54,7 +62,7 @@ static void json_message_process_token(JSONLexer *lexer, GString *input, JSONTok parser->token_size += input->len; -qlist_append (parser->tokens, dict); +g_queue_push_tail (parser->tokens, dict);if (type ==JSON_ERROR ) { goto out_emit_bad;
@@ -78,27 +86,24 @@ out_emit_bad:/* clear out token list and tell the parser to emit and error * indication by passing it a NULL list */ -QDECREF (parser->tokens); - parser->tokens =NULL ; +json_message_free_tokens (parser); out_emit:/* send current list of tokens to parser and reset tokenizer */ parser->brace_count =0 ; parser->bracket_count =0 ; +/* parser->emit takes ownership of parser->tokens. */ parser->emit (parser, parser->tokens); -if (parser->tokens) { -QDECREF (parser->tokens); - } - parser->tokens =qlist_new (); + parser->tokens =g_queue_new (); parser->token_size =0 ; }void json_message_parser_init (JSONMessageParser *parser, -void (*func)(JSONMessageParser *,QList *)) +void (*func)(JSONMessageParser *,GQueue *)) { parser->emit = func; parser->brace_count =0 ; parser->bracket_count =0 ; - parser->tokens =qlist_new (); + parser->tokens =g_queue_new (); parser->token_size =0 ;json_lexer_init (&parser->lexer, json_message_process_token);
@@ -118,5 +123,5 @@ int json_message_parser_flush(JSONMessageParser *parser)void json_message_parser_destroy (JSONMessageParser *parser) {json_lexer_destroy (&parser->lexer); -QDECREF (parser->tokens); +json_message_free_tokens (parser); }
qobject/qjson.c +1 -1
@@ -28,7 +28,7 @@ typedef struct JSONParsingStateQObject *result; } JSONParsingState; -static void parse_json (JSONMessageParser *parser,QList *tokens) +static void parse_json (JSONMessageParser *parser,GQueue *tokens) {JSONParsingState *s =container_of (parser, JSONParsingState, parser); s->result =json_parser_parse (tokens, s->ap);
tests/libqtest.c +1 -1
@@ -351,7 +351,7 @@ typedef struct {QDict *response; } QMPResponseParser; -static void qmp_response (JSONMessageParser *parser,QList *tokens) +static void qmp_response (JSONMessageParser *parser,GQueue *tokens) {QMPResponseParser *qmp =container_of (parser, QMPResponseParser, parser);QObject *obj;
Signature · 1 line
2.5.0
PATCH v2 4/4 qjson: surprise, allocating 6 QObjects per token is expensive
In reply to Paolo BonziniReplace the contents of the tokens GQueue with a simple struct. This cuts the amount of memory allocated by tests/check-qjson from ~500MB to ~20MB, and the execution time from 600ms to 80ms on my laptop. Still a lot (some could be saved by using an intrusive list, such as QSIMPLEQ, instead of the GQueue), but the savings are already massive and the right thing to do would probably be to get rid of json-streamer completely.
---
v1->v2: fix memcpy off-by-one [Eric]
include/qapi/qmp/json-streamer.h | 7 ++++ qobject/json-parser.c | 81 +++++++++++++++++++--------------------- qobject/json-streamer.c | 19 ++++------ 3 files changed, 53 insertions(+), 54 deletions(-)
include/qapi/qmp/json-streamer.h +7 -0
@@ -18,6 +18,13 @@#include "glib-compat.h" #include "qapi/qmp/json-lexer.h" +typedef struct JSONToken { +int type; +int x; +int y; +char str[]; +}JSONToken ; + typedef struct JSONMessageParser { void (*emit)(struct JSONMessageParser *parser, GQueue *tokens);
qobject/json-parser.c +38 -43
224 lines not on this page · raw message
qobject/json-streamer.c +8 -11
@@ -11,10 +11,6 @@* */ -#include "qapi/qmp/qlist.h" -#include "qapi/qmp/qstring.h" -#include "qapi/qmp/qint.h" -#include "qapi/qmp/qdict.h" #include "qemu-common.h" #include "qapi/qmp/json-lexer.h" #include "qapi/qmp/json-streamer.h"
@@ -33,7 +29,7 @@ static void json_message_free_tokens(JSONMessageParser *parser)static void json_message_process_token (JSONLexer *lexer,GString *input,JSONTokenType type,int x,int y) {JSONMessageParser *parser =container_of (lexer, JSONMessageParser, lexer); -QDict *dict; +JSONToken *token;if (type ==JSON_OPERATOR ) {switch (input->str[0 ]) {
@@ -54,15 +50,16 @@ static void json_message_process_token(JSONLexer *lexer, GString *input, JSONTok } } - dict =qdict_new (); -qdict_put (dict,"type" ,qint_from_int (type)); -qdict_put (dict,"token" ,qstring_from_str (input->str)); -qdict_put (dict,"x" ,qint_from_int (x)); -qdict_put (dict,"y" ,qint_from_int (y)); + token =g_malloc (sizeof (JSONToken) + input->len +1 ); + token->type = type; +memcpy (token->str, input->str, input->len); + token->str[input->len] =0 ; + token->x = x; + token->y = y; parser->token_size += input->len; -g_queue_push_tail (parser->tokens, dict); +g_queue_push_tail (parser->tokens, token);if (type ==JSON_ERROR ) { goto out_emit_bad;
Signature · 1 line
2.5.0
Quoted 10 lines · Paolo Bonzini
This patch from 2011 (!) saves about 96% of the allocation cost (down from 500 MiB to 20 MiB) for check-qjson.
Paolo
Paolo Bonzini (4):
qjson: replace QString in JSONLexer with GString
qjson: do not save/restore contexts
qjson: store tokens in a GQueue
qjson: surprise, allocating 6 QObjects per token is expensive
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 2 lines · Paolo Bonzini
This patch from 2011 (!) saves about 96% of the allocation cost (down from 500 MiB to 20 MiB) for check-qjson.
Looks good to me apart from the incomplete transition to recursive descent in PATCH 2.
Quoted 5 lines · Markus Armbruster
Quoted 2 lines
This patch from 2011 (!) saves about 96% of the allocation cost (down from 500 MiB to 20 MiB) for check-qjson.
Looks good to me apart from the incomplete transition to recursive descent in PATCH 2.
I do not understand whether that is a problem. Is the remark from the commit message incorrect ("Saving the parser context is mostly unnecessary; we can replace it with peeking at the next token, or remove it altogether when the restore only happens on errors. The token list is destroyed anyway on errors.")?
Paolo
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Markus Armbruster
Quoted 2 lines
This patch from 2011 (!) saves about 96% of the allocation cost (down from 500 MiB to 20 MiB) for check-qjson.
Quoted 8 lines
Quoted 2 lines
Looks good to me apart from the incomplete transition to recursive descent in PATCH 2.
I do not understand whether that is a problem. Is the remark from the commit message incorrect ("Saving the parser context is mostly unnecessary; we can replace it with peeking at the next token, or remove it altogether when the restore only happens on errors. The token list is destroyed anyway on errors.")?
The token sequence
{ 1 [ 2 ] }gets parsed as [ 2 ]. See my reply to PATCH 2 for a more detailed explanation.
I didn't bother to construct an example where QMP executes the incorrect result, and I'm not sure it's possible.
Regardless, I'm fixing it, by simplifying the parser some more.