From dc6d53e7879914c2d20ae4f28860c14d721e5798 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 8 Apr 2019 19:28:44 +0930 Subject: [PATCH] lightningd: don't bother pretty-printing JSON. This doesn't result in a speedup for our benchmark, since we use the cli tool which does the formatting. MCP results from 5 runs, min-max(mean +/- stddev): store_load_msec:33422-36830(35196.2+/-1.2e+03) vsz_kb:2637488 store_rewrite_sec:36.030000-37.630000(36.794+/-0.52) listnodes_sec:0.720000-0.950000(0.86+/-0.077) listchannels_sec:40.300000-41.080000(40.668+/-0.29) routing_sec:30.440000-31.030000(30.69+/-0.2) peer_write_all_sec:50.060000-52.800000(51.416+/-0.91) MCP notable changes from 2 patches ago (>1 stddev): -listchannels_sec:48.560000-55.680000(52.642+/-2.7) +listchannels_sec:40.300000-41.080000(40.668+/-0.29) Signed-off-by: Rusty Russell --- lightningd/json_stream.c | 40 ++++++----------------------------- lightningd/test/run-jsonrpc.c | 8 +++---- tools/bench-gossipd.sh | 2 +- 3 files changed, 11 insertions(+), 39 deletions(-) diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c index 6c65173f2..f250b8f43 100644 --- a/lightningd/json_stream.c +++ b/lightningd/json_stream.c @@ -14,9 +14,6 @@ struct json_stream { /* tal_arr of types (JSMN_OBJECT/JSMN_ARRAY) we're enclosed in. */ jsmntype_t *wrapping; #endif - /* How far to indent. */ - size_t indent; - /* True if we haven't yet put an element in current wrapping */ bool empty; @@ -61,7 +58,6 @@ struct json_stream *new_json_stream(const tal_t *ctx, #if DEVELOPER js->wrapping = tal_arr(js, jsmntype_t, 0); #endif - js->indent = 0; js->empty = true; js->log = log; return js; @@ -179,33 +175,15 @@ static void check_fieldname(const struct json_stream *js, #endif } -static void js_append_indent(struct json_stream *js) -{ - static const char indent_buf[] = " "; - size_t len; - - for (size_t i = 0; i < js->indent * 2; i += len) { - len = js->indent * 2; - if (len > sizeof(indent_buf)-1) - len = sizeof(indent_buf)-1; - /* Use tail of indent_buf string. */ - json_stream_append(js, indent_buf + sizeof(indent_buf) - 1 - len); - } -} - static void json_start_member(struct json_stream *js, const char *fieldname) { /* Prepend comma if required. */ if (!js->empty) - json_stream_append(js, ", \n"); - else - json_stream_append(js, "\n"); - - js_append_indent(js); + json_stream_append(js, ","); check_fieldname(js, fieldname); if (fieldname) - json_stream_append_fmt(js, "\"%s\": ", fieldname); + json_stream_append_fmt(js, "\"%s\":", fieldname); js->empty = false; } @@ -215,19 +193,17 @@ static void js_indent(struct json_stream *js, jsmntype_t type) tal_arr_expand(&js->wrapping, type); #endif js->empty = true; - js->indent++; } static void js_unindent(struct json_stream *js, jsmntype_t type) { - assert(js->indent); #if DEVELOPER - assert(tal_count(js->wrapping) == js->indent); - assert(js->wrapping[js->indent-1] == type); - tal_resize(&js->wrapping, js->indent-1); + size_t indent = tal_count(js->wrapping); + assert(indent > 0); + assert(js->wrapping[indent-1] == type); + tal_resize(&js->wrapping, indent-1); #endif js->empty = false; - js->indent--; } void json_array_start(struct json_stream *js, const char *fieldname) @@ -239,9 +215,7 @@ void json_array_start(struct json_stream *js, const char *fieldname) void json_array_end(struct json_stream *js) { - json_stream_append(js, "\n"); js_unindent(js, JSMN_ARRAY); - js_append_indent(js); json_stream_append(js, "]"); } @@ -254,9 +228,7 @@ void json_object_start(struct json_stream *js, const char *fieldname) void json_object_end(struct json_stream *js) { - json_stream_append(js, "\n"); js_unindent(js, JSMN_OBJECT); - js_append_indent(js); json_stream_append(js, "}"); } diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 90778ae74..aca596b97 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -149,12 +149,12 @@ static void test_json_escape(void) if (i == '\\' || i == '"' || i == '\n' || i == '\r' || i == '\b' || i == '\t' || i == '\f') - assert(strstarts(str, "\n{\n \"x\": \"\\")); + assert(strstarts(str, "{\"x\":\"\\")); else if (i < 32 || i == 127) { - assert(strstarts(str, "\n{\n \"x\": \"\\u00")); + assert(strstarts(str, "{\"x\":\"\\u00")); } else { - char expect[] = "\n{\n \"x\": \"?\"\n}"; - expect[11] = i; + char expect[] = "{\"x\":\"?\"}"; + expect[6] = i; assert(streq(str, expect)); } tal_free(result); diff --git a/tools/bench-gossipd.sh b/tools/bench-gossipd.sh index eecc9ad05..241a2d056 100755 --- a/tools/bench-gossipd.sh +++ b/tools/bench-gossipd.sh @@ -130,7 +130,7 @@ if [ -z "${TARGETS##* routing_sec *}" ]; then echo "$DIV" | tr -d \\n; DIV="," # shellcheck disable=SC2046 # shellcheck disable=SC2005 - echo $(grep nodeid "$DIR"/listnodes.json | cut -d'"' -f4 | sort | head -n2) | while read -r from to; do + echo $(tr '{}' '\n' < "$DIR"/listnodes.json | grep nodeid | cut -d'"' -f4 | sort | head -n2) | while read -r from to; do # shellcheck disable=SC2086 /usr/bin/time --quiet --append -f %e $LCLI1 getroute $from 1 1 6 $to 2>&1 > /dev/null | print_stat routing_sec # FIXME: this shouldn't fail done