Skip to content

Commit bf3c487

Browse files
authored
Avoid needless memsets by creating a variant of bc_new_num that doesn't memset (#14133)
Also avoid some memsets where we do call bc_new_num. After: ``` 1.2066178321838 1.5389559268951 1.6050860881805 ``` Before: ``` 1.3858470916748 1.6806011199951 1.9091980457306 ```
1 parent 1ae58d4 commit bf3c487

File tree

7 files changed

+21
-12
lines changed

7 files changed

+21
-12
lines changed

ext/bcmath/libbcmath/src/add.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ bc_num bc_add(bc_num n1, bc_num n2, size_t scale_min)
5858
case 0:
5959
/* They are equal! return zero with the correct scale! */
6060
sum = bc_new_num (1, MAX(scale_min, MAX(n1->n_scale, n2->n_scale)));
61-
memset(sum->n_value, 0, sum->n_scale + 1);
6261
break;
6362
case 1:
6463
/* n2 is less than n1, subtract n2 from n1. */

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ void bc_init_numbers(void);
8686

8787
bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent);
8888

89+
bc_num _bc_new_num_nonzeroed_ex(size_t length, size_t scale, bool persistent);
90+
8991
void _bc_free_num_ex(bc_num *num, bool persistent);
9092

9193
/* Make a copy of a number! Just increments the reference count! */
@@ -167,8 +169,9 @@ void bc_raise_bc_exponent(bc_num base, bc_num exponent, bc_num *resul, size_t sc
167169
bool bc_sqrt(bc_num *num, size_t scale);
168170

169171
/* Prototypes needed for external utility routines. */
170-
#define bc_new_num(length, scale) _bc_new_num_ex((length), (scale), 0)
171-
#define bc_free_num(num) _bc_free_num_ex((num), 0)
172-
#define bc_num2str(num) bc_num2str_ex((num), (num->n_scale))
172+
#define bc_new_num(length, scale) _bc_new_num_ex((length), (scale), 0)
173+
#define bc_new_num_nonzeroed(length, scale) _bc_new_num_nonzeroed_ex((length), (scale), 0)
174+
#define bc_free_num(num) _bc_free_num_ex((num), 0)
175+
#define bc_num2str(num) bc_num2str_ex((num), (num->n_scale))
173176

174177
#endif

ext/bcmath/libbcmath/src/div.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
9797
if (n2->n_scale == 0 && n2->n_len == 1 && *n2->n_value == 1) {
9898
qval = bc_new_num (n1->n_len, scale);
9999
qval->n_sign = (n1->n_sign == n2->n_sign ? PLUS : MINUS);
100-
memset(&qval->n_value[n1->n_len], 0, scale);
101100
memcpy(qval->n_value, n1->n_value, n1->n_len + MIN(n1->n_scale, scale));
102101
bc_free_num (quot);
103102
*quot = qval;
@@ -146,7 +145,6 @@ bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
146145

147146
/* Allocate and zero the storage for the quotient. */
148147
qval = bc_new_num (qdigits - scale, scale);
149-
memset(qval->n_value, 0, qdigits);
150148

151149
/* Allocate storage for the temporary storage mval. */
152150
mval = (unsigned char *) safe_emalloc(1, len2, 1);

ext/bcmath/libbcmath/src/init.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
#include <string.h>
3636
#include "zend_alloc.h"
3737

38-
/* new_num allocates a number and sets fields to known values. */
39-
bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent)
38+
static zend_always_inline bc_num _bc_new_num_nonzeroed_ex_internal(size_t length, size_t scale, bool persistent)
4039
{
4140
/* PHP Change: malloc() -> pemalloc(), removed free_list code, merged n_ptr and n_value */
4241
bc_num temp = safe_pemalloc(1, sizeof(bc_struct) + length, scale, persistent);
@@ -45,10 +44,21 @@ bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent)
4544
temp->n_scale = scale;
4645
temp->n_refs = 1;
4746
temp->n_value = (char *) temp + sizeof(bc_struct);
47+
return temp;
48+
}
49+
50+
/* new_num allocates a number and sets fields to known values. */
51+
bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent)
52+
{
53+
bc_num temp = _bc_new_num_nonzeroed_ex_internal(length, scale, persistent);
4854
memset(temp->n_value, 0, length + scale);
4955
return temp;
5056
}
5157

58+
bc_num _bc_new_num_nonzeroed_ex(size_t length, size_t scale, bool persistent)
59+
{
60+
return _bc_new_num_nonzeroed_ex_internal(length, scale, persistent);
61+
}
5262

5363
/* "Frees" a bc_num NUM. Actually decreases reference count and only
5464
frees the storage if reference count is zero. */

ext/bcmath/libbcmath/src/recmul.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static void _bc_simp_mul(bc_num n1, size_t n1len, bc_num n2, int n2len, bc_num *
6868

6969
int prodlen = n1len + n2len + 1;
7070

71-
*prod = bc_new_num (prodlen, 0);
71+
*prod = bc_new_num_nonzeroed(prodlen, 0);
7272

7373
n1end = (char *) (n1->n_value + n1len - 1);
7474
n2end = (char *) (n2->n_value + n2len - 1);

ext/bcmath/libbcmath/src/str2num.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ bool bc_str2num(bc_num *num, const char *str, const char *end, size_t scale, boo
154154
zero_int = true;
155155
digits = 1;
156156
}
157-
*num = bc_new_num (digits, str_scale);
157+
*num = bc_new_num_nonzeroed(digits, str_scale);
158158
(*num)->n_sign = *str == '-' ? MINUS : PLUS;
159159
char *nptr = (*num)->n_value;
160160

161161
if (zero_int) {
162-
nptr++;
162+
*nptr++ = 0;
163163
/*
164164
* If zero_int is true and the str_scale is 0, there is an early return,
165165
* so here str_scale is always greater than 0.

ext/bcmath/libbcmath/src/sub.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ bc_num bc_sub(bc_num n1, bc_num n2, size_t scale_min)
5959
/* They are equal! return zero! */
6060
size_t res_scale = MAX (scale_min, MAX(n1->n_scale, n2->n_scale));
6161
diff = bc_new_num (1, res_scale);
62-
memset(diff->n_value, 0, res_scale + 1);
6362
break;
6463
}
6564
case 1:

0 commit comments

Comments
 (0)