From faf6b3809544f40043b82552742572b6fa9bb339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 13 Dec 2018 14:21:16 +0100 Subject: Do not allocate new bitstring/binary on empty append --- erts/emulator/beam/erl_bits.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'erts/emulator/beam/erl_bits.c') diff --git a/erts/emulator/beam/erl_bits.c b/erts/emulator/beam/erl_bits.c index e82c776e70..051feb5977 100644 --- a/erts/emulator/beam/erl_bits.c +++ b/erts/emulator/beam/erl_bits.c @@ -1331,6 +1331,10 @@ erts_bs_append(Process* c_p, Eterm* reg, Uint live, Eterm build_size_term, } } + if (build_size_in_bits == 0) { + goto return_bin; + } + if((ERTS_UINT_MAX - build_size_in_bits) < erts_bin_offset) { c_p->freason = SYSTEM_LIMIT; return THE_NON_VALUE; @@ -1388,16 +1392,6 @@ erts_bs_append(Process* c_p, Eterm* reg, Uint live, Eterm build_size_term, Uint bitsize; Eterm* hp; - /* - * Allocate heap space. - */ - heap_need = PROC_BIN_SIZE + ERL_SUB_BIN_SIZE + extra_words; - if (c_p->stop - c_p->htop < heap_need) { - (void) erts_garbage_collect(c_p, heap_need, reg, live+1); - bin = reg[live]; - } - hp = c_p->htop; - /* * Calculate sizes. The size of the new binary, is the sum of the * build size and the size of the old binary. Allow some room @@ -1412,6 +1406,19 @@ erts_bs_append(Process* c_p, Eterm* reg, Uint live, Eterm build_size_term, } } + if (build_size_in_bits == 0) { + goto return_bin; + } + + /* + * Allocate heap space. + */ + heap_need = PROC_BIN_SIZE + ERL_SUB_BIN_SIZE + extra_words; + if (c_p->stop - c_p->htop < heap_need) { + (void) erts_garbage_collect(c_p, heap_need, reg, live+1); + } + hp = c_p->htop; + if((ERTS_UINT_MAX - build_size_in_bits) < erts_bin_offset) { c_p->freason = SYSTEM_LIMIT; return THE_NON_VALUE; @@ -1471,6 +1478,16 @@ erts_bs_append(Process* c_p, Eterm* reg, Uint live, Eterm build_size_term, return make_binary(sb); } + + return_bin: + { + if (c_p->stop - c_p->htop < extra_words) { + reg[live] = bin; + (void) erts_garbage_collect(c_p, extra_words, reg, live+1); + bin = reg[live]; + } + return bin; + } } Eterm -- cgit v1.2.3