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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2025-11-20 12:07:14 +10:30
parent d2a6091149
commit 54d4bf117f

View File

@@ -486,24 +486,24 @@ bool json_parse_input(jsmn_parser *parser,
again: again:
ret = jsmn_parse(parser, input, len, *toks, tal_count(*toks) - 1); ret = jsmn_parse(parser, input, len, *toks, tal_count(*toks) - 1);
if (ret == JSMN_ERROR_INVAL)
switch (ret) {
case JSMN_ERROR_INVAL:
return false; 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 /* Check whether we read at least one full root element, i.e., root
* element has its end set. */ * element has its end set. */
if ((*toks)[0].type == JSMN_UNDEFINED || (*toks)[0].end == -1) { 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; *complete = false;
return true; return true;
} }
/* If we read a partial element at the end of the stream we'll get a /* 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 * least one full element, so count tokens that are part of this root
* element. */ * element. */
ret = json_next(*toks) - *toks; ret = json_next(*toks) - *toks;