Skip to content

Commit

Permalink
bcli: Avoid tal_fmt when handling a raw hex block
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cdecker committed Aug 27, 2020
1 parent 4a9c162 commit 6e1641a
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions plugins/bcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6e1641a

Please sign in to comment.