From 54d4bf117fc482de2ef064225442d46e629807a8 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 20 Nov 2025 12:07:14 +1030 Subject: [PATCH] common: optimize json parsing. We would keep parsing if we were out of tokens, even if we had actually finished one object! These are comparison against the "xpay: use filtering on rpc_command so we only get called on "pay"." not the disasterous previous one! tests/test_coinmoves.py::test_generate_coinmoves (2,000,000, sqlite3): Time (from start to end of l2 node): 126 seconds (was 135) Worst latency: 5.1 seconds **WAS 12.1** Signed-off-by: Rusty Russell --- common/json_parse_simple.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/common/json_parse_simple.c b/common/json_parse_simple.c index f3b3da808..348be9ef6 100644 --- a/common/json_parse_simple.c +++ b/common/json_parse_simple.c @@ -486,24 +486,24 @@ bool json_parse_input(jsmn_parser *parser, again: ret = jsmn_parse(parser, input, len, *toks, tal_count(*toks) - 1); - - switch (ret) { - case JSMN_ERROR_INVAL: + if (ret == JSMN_ERROR_INVAL) return false; - case JSMN_ERROR_NOMEM: - tal_resize(toks, tal_count(*toks) * 2); - goto again; - } /* Check whether we read at least one full root element, i.e., root * element has its end set. */ if ((*toks)[0].type == JSMN_UNDEFINED || (*toks)[0].end == -1) { + /* If it ran out of tokens, provide more. */ + if (ret == JSMN_ERROR_NOMEM) { + tal_resize(toks, tal_count(*toks) * 2); + goto again; + } + /* Otherwise, must be incomplete */ *complete = false; return true; } /* If we read a partial element at the end of the stream we'll get a - * ret=JSMN_ERROR_PART, but due to the previous check we know we read at + * errro, but due to the previous check we know we read at * least one full element, so count tokens that are part of this root * element. */ ret = json_next(*toks) - *toks;