Skip to content

Commit

Permalink
Allocate less heap space for band of positive numbers
Browse files Browse the repository at this point in the history
When doing a `band` on two positive numbers, the result buffer buffer
can be allocated based on the minimum of the sizes of the number.
  • Loading branch information
bjorng committed Aug 25, 2023
1 parent 8d6cb95 commit a246d74
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion erts/emulator/beam/erl_arith.c
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,7 @@ Eterm erts_band(Process* p, Eterm arg1, Eterm arg2)
{
Eterm tmp_big1[2], tmp_big2[2];
Eterm* hp;
dsize_t sz1, sz2, sz;
int need;

switch (NUMBER_CODE(arg1, arg2)) {
Expand All @@ -1352,9 +1353,25 @@ Eterm erts_band(Process* p, Eterm arg1, Eterm arg2)
p->freason = BADARITH;
return THE_NON_VALUE;
}
need = BIG_NEED_SIZE(MAX(big_size(arg1), big_size(arg2)) + 1);
sz1 = big_size(arg1);
sz2 = big_size(arg2);
if (big_sign(arg1) == 0 && sz1 < sz2) {
sz = sz1 + 1;
} else if (big_sign(arg2) == 0 && sz2 < sz1) {
sz = sz2 + 1;
} else {
sz = MAX(sz1, sz2) + 1;
}
need = BIG_NEED_SIZE(sz);
#ifdef DEBUG
need++;
#endif
hp = HeapFragOnlyAlloc(p, need);
#ifdef DEBUG
hp[need-1] = ERTS_HOLE_MARKER;
#endif
arg1 = big_band(arg1, arg2, hp);
ASSERT(hp[need-1] == ERTS_HOLE_MARKER);
ASSERT(is_not_nil(arg1));
maybe_shrink(p, hp, arg1, need);
return arg1;
Expand Down

0 comments on commit a246d74

Please sign in to comment.