bc: code shrink
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 21 Dec 2018 22:13:48 +0000 (23:13 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 21 Dec 2018 22:13:48 +0000 (23:13 +0100)
function                                             old     new   delta
bc_result_pop_and_push                                 -      73     +73
zbc_program_exec                                    4068    4064      -4
bc_program_binOpRetire                                46      32     -14
zdc_program_assignStr                                146     126     -20
zdc_program_asciify                                  395     370     -25
bc_program_retire                                     35       7     -28
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/5 up/down: 73/-91)            Total: -18 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/bc.c

index 2ba530d43780d0e487796c87e406b747e889dd34..cac3cb734136671909cb3a843b02223c10c40b96 100644 (file)
@@ -1106,6 +1106,21 @@ static size_t bc_vec_push(BcVec *v, const void *data)
        return len;
 }
 
+// G.prog.results often needs "pop old operand, push result" idiom.
+// Can do this without a few extra ops
+static size_t bc_result_pop_and_push(const void *data)
+{
+       BcVec *v = &G.prog.results;
+       char *last;
+       size_t len = v->len - 1;
+
+       last = v->v + (v->size * len);
+       if (v->dtor)
+               v->dtor(last);
+       memmove(last, data, v->size);
+       return len;
+}
+
 static size_t bc_vec_pushByte(BcVec *v, char data)
 {
        return bc_vec_push(v, &data);
@@ -5165,8 +5180,7 @@ static void bc_program_binOpRetire(BcResult *r)
 {
        r->t = BC_RESULT_TEMP;
        bc_vec_pop(&G.prog.results);
-       bc_vec_pop(&G.prog.results);
-       bc_vec_push(&G.prog.results, r);
+       bc_result_pop_and_push(r);
 }
 
 static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
@@ -5190,8 +5204,7 @@ static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
 static void bc_program_retire(BcResult *r, BcResultType t)
 {
        r->t = t;
-       bc_vec_pop(&G.prog.results);
-       bc_vec_push(&G.prog.results, r);
+       bc_result_pop_and_push(r);
 }
 
 static BC_STATUS zbc_program_op(char inst)
@@ -5684,9 +5697,7 @@ static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
                bc_vec_pop(&G.prog.results);
        }
 
-       bc_vec_pop(&G.prog.results);
-
-       bc_vec_push(&G.prog.results, &res);
+       bc_result_pop_and_push(&res);
        bc_vec_push(v, &n2);
 
        RETURN_STATUS(BC_STATUS_SUCCESS);
@@ -5928,8 +5939,7 @@ static BC_STATUS zbc_program_incdec(char inst)
        if (s) RETURN_STATUS(s);
 
        if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
-               bc_vec_pop(&G.prog.results);
-               bc_vec_push(&G.prog.results, &copy);
+               bc_result_pop_and_push(&copy);
        }
 
        RETURN_STATUS(s);
@@ -6244,8 +6254,7 @@ static BC_STATUS zdc_program_asciify(void)
  dup:
        res.t = BC_RESULT_STR;
        res.d.id.idx = idx;
-       bc_vec_pop(&G.prog.results);
-       bc_vec_push(&G.prog.results, &res);
+       bc_result_pop_and_push(&res);
 
        RETURN_STATUS(BC_STATUS_SUCCESS);
  num_err: