From 465065691ffe6d09e915a1db0d3be6e261a45d71 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 8 Apr 2019 19:28:44 +0930 Subject: [PATCH] json_add_hex: wire in at a lower level. This is one of the more significant fields we print, but there's no need to allocate a temp buffer or escape the resulting string. MCP results from 5 runs, min-max(mean +/- stddev): store_load_msec:34048-36002(35070.4+/-8.5e+02) vsz_kb:2637488 store_rewrite_sec:35.110000-38.120000(36.604+/-1.2) listnodes_sec:0.830000-1.020000(0.95+/-0.065) listchannels_sec:48.560000-55.680000(52.642+/-2.7) routing_sec:29.800000-33.170000(30.536+/-1.3) peer_write_all_sec:49.260000-52.490000(50.316+/-1.1) MCP notable changes from previous patch (>1 stddev): -listchannels_sec:55.390000-58.110000(56.998+/-0.99) +listchannels_sec:48.560000-55.680000(52.642+/-2.7) Signed-off-by: Rusty Russell --- lightningd/json.c | 10 ---------- lightningd/json_stream.c | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lightningd/json.c b/lightningd/json.c index 5171f75d3..f11069e53 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -330,16 +330,6 @@ void json_add_null(struct json_stream *stream, const char *fieldname) json_add_member(stream, fieldname, "null"); } -void json_add_hex(struct json_stream *result, const char *fieldname, - const void *data, size_t len) -{ - char *hex = tal_arr(NULL, char, hex_str_size(len)); - - hex_encode(data, len, hex, hex_str_size(len)); - json_add_string(result, fieldname, hex); - tal_free(hex); -} - void json_add_hex_talarr(struct json_stream *result, const char *fieldname, const tal_t *data) diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c index 80695244b..6c65173f2 100644 --- a/lightningd/json_stream.c +++ b/lightningd/json_stream.c @@ -1,6 +1,7 @@ #include /* To reach into io_plan: not a public header! */ #include +#include #include #include #include @@ -271,6 +272,23 @@ json_add_member(struct json_stream *js, const char *fieldname, va_end(ap); } +void json_add_hex(struct json_stream *js, const char *fieldname, + const void *data, size_t len) +{ + /* Size without NUL term */ + size_t hexlen = hex_str_size(len) - 1; + char *dest; + + json_start_member(js, fieldname); + mkroom(js, 1 + hexlen + 1); + dest = membuf_add(&js->outbuf, 1 + hexlen + 1); + dest[0] = '"'; + if (!hex_encode(data, len, dest + 1, hexlen + 1)) + abort(); + dest[1+hexlen] = '"'; + js_written_some(js); +} + /* This is where we read the json_stream and write it to conn */ static struct io_plan *json_stream_output_write(struct io_conn *conn, struct json_stream *js)