From 31c67f1392019fb2970308629bdf58033fa257f9 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 18 Jul 2020 11:12:16 +0200 Subject: [PATCH] bcli: Avoid tal_fmt when handling a raw hex block We were using `tal_fmt` to truncate off the last byte of the response (newline), which required an allocation, a call to `vsnprintf` and a copy of the block contents. This being >2MB in size on mainnet was rather expensive. We now just signal the end of the string by overwriting the trailing newline, and stealing directly onto the result. --- plugins/bcli.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/bcli.c b/plugins/bcli.c index c595f8426..ffb66fdeb 100644 --- a/plugins/bcli.c +++ b/plugins/bcli.c @@ -618,9 +618,9 @@ static struct command_result *process_getrawblock(struct bitcoin_cli *bcli) struct json_stream *response; struct getrawblock_stash *stash = bcli->stash; - /* -1 to strip \n. */ - stash->block_hex = tal_fmt(stash, "%.*s", - (int)bcli->output_bytes-1, bcli->output); + /* -1 to strip \n and steal onto the stash. */ + bcli->output[bcli->output_bytes-1] = 0x00; + stash->block_hex = tal_steal(stash, bcli->output); response = jsonrpc_stream_success(bcli->cmd); json_add_string(response, "blockhash", stash->block_hash);