Skip to content

Commit a66439a

Browse files
authored
Avoid unnecessary number initializations in BCMath (#14104)
Since freeing can deal with NULL, we can avoid calling bc_init_num and avoid resetting the number during parsing. Using benchmark from #14076. Before: ``` 1.544440984726 2.0288550853729 2.092139005661 ``` After: ``` 1.5324399471283 1.9081380367279 2.065819978714 ```
1 parent b1bb9c3 commit a66439a

File tree

2 files changed

+13
-35
lines changed

2 files changed

+13
-35
lines changed

ext/bcmath/bcmath.c

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ PHP_FUNCTION(bcadd)
148148
zend_string *left, *right;
149149
zend_long scale_param;
150150
bool scale_param_is_null = 1;
151-
bc_num first, second, result;
151+
bc_num first = NULL, second = NULL, result;
152152
int scale;
153153

154154
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -167,8 +167,6 @@ PHP_FUNCTION(bcadd)
167167
scale = (int) scale_param;
168168
}
169169

170-
bc_init_num(&first);
171-
bc_init_num(&second);
172170
bc_init_num(&result);
173171

174172
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
@@ -199,7 +197,7 @@ PHP_FUNCTION(bcsub)
199197
zend_string *left, *right;
200198
zend_long scale_param;
201199
bool scale_param_is_null = 1;
202-
bc_num first, second, result;
200+
bc_num first = NULL, second = NULL, result;
203201
int scale;
204202

205203
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -218,8 +216,6 @@ PHP_FUNCTION(bcsub)
218216
scale = (int) scale_param;
219217
}
220218

221-
bc_init_num(&first);
222-
bc_init_num(&second);
223219
bc_init_num(&result);
224220

225221
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
@@ -250,7 +246,7 @@ PHP_FUNCTION(bcmul)
250246
zend_string *left, *right;
251247
zend_long scale_param;
252248
bool scale_param_is_null = 1;
253-
bc_num first, second, result;
249+
bc_num first = NULL, second = NULL, result;
254250
int scale;
255251

256252
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -269,8 +265,6 @@ PHP_FUNCTION(bcmul)
269265
scale = (int) scale_param;
270266
}
271267

272-
bc_init_num(&first);
273-
bc_init_num(&second);
274268
bc_init_num(&result);
275269

276270
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
@@ -301,7 +295,7 @@ PHP_FUNCTION(bcdiv)
301295
zend_string *left, *right;
302296
zend_long scale_param;
303297
bool scale_param_is_null = 1;
304-
bc_num first, second, result;
298+
bc_num first = NULL, second = NULL, result;
305299
int scale = BCG(bc_precision);
306300

307301
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -320,8 +314,6 @@ PHP_FUNCTION(bcdiv)
320314
scale = (int) scale_param;
321315
}
322316

323-
bc_init_num(&first);
324-
bc_init_num(&second);
325317
bc_init_num(&result);
326318

327319
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
@@ -355,7 +347,7 @@ PHP_FUNCTION(bcmod)
355347
zend_string *left, *right;
356348
zend_long scale_param;
357349
bool scale_param_is_null = 1;
358-
bc_num first, second, result;
350+
bc_num first = NULL, second = NULL, result;
359351
int scale = BCG(bc_precision);
360352

361353
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -374,8 +366,6 @@ PHP_FUNCTION(bcmod)
374366
scale = (int) scale_param;
375367
}
376368

377-
bc_init_num(&first);
378-
bc_init_num(&second);
379369
bc_init_num(&result);
380370

381371
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
@@ -409,7 +399,7 @@ PHP_FUNCTION(bcpowmod)
409399
zend_string *base_str, *exponent_str, *modulus_str;
410400
zend_long scale_param;
411401
bool scale_param_is_null = 1;
412-
bc_num bc_base, bc_expo, bc_modulus, result;
402+
bc_num bc_base = NULL, bc_expo = NULL, bc_modulus = NULL, result;
413403
int scale = BCG(bc_precision);
414404

415405
ZEND_PARSE_PARAMETERS_START(3, 4)
@@ -429,9 +419,6 @@ PHP_FUNCTION(bcpowmod)
429419
scale = (int) scale_param;
430420
}
431421

432-
bc_init_num(&bc_base);
433-
bc_init_num(&bc_expo);
434-
bc_init_num(&bc_modulus);
435422
bc_init_num(&result);
436423

437424
if (php_str2num(&bc_base, ZSTR_VAL(base_str)) == FAILURE) {
@@ -487,7 +474,7 @@ PHP_FUNCTION(bcpow)
487474
zend_string *base_str, *exponent_str;
488475
zend_long scale_param;
489476
bool scale_param_is_null = 1;
490-
bc_num first, bc_exponent, result;
477+
bc_num first = NULL, bc_exponent = NULL, result;
491478
int scale = BCG(bc_precision);
492479

493480
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -506,8 +493,6 @@ PHP_FUNCTION(bcpow)
506493
scale = (int) scale_param;
507494
}
508495

509-
bc_init_num(&first);
510-
bc_init_num(&bc_exponent);
511496
bc_init_num(&result);
512497

513498
if (php_str2num(&first, ZSTR_VAL(base_str)) == FAILURE) {
@@ -549,7 +534,7 @@ PHP_FUNCTION(bcsqrt)
549534
zend_string *left;
550535
zend_long scale_param;
551536
bool scale_param_is_null = 1;
552-
bc_num result;
537+
bc_num result = NULL;
553538
int scale = BCG(bc_precision);
554539

555540
ZEND_PARSE_PARAMETERS_START(1, 2)
@@ -567,8 +552,6 @@ PHP_FUNCTION(bcsqrt)
567552
scale = (int) scale_param;
568553
}
569554

570-
bc_init_num(&result);
571-
572555
if (php_str2num(&result, ZSTR_VAL(left)) == FAILURE) {
573556
zend_argument_value_error(1, "is not well-formed");
574557
goto cleanup;
@@ -592,7 +575,7 @@ PHP_FUNCTION(bccomp)
592575
zend_string *left, *right;
593576
zend_long scale_param;
594577
bool scale_param_is_null = 1;
595-
bc_num first, second;
578+
bc_num first = NULL, second = NULL;
596579
int scale = BCG(bc_precision);
597580

598581
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -611,9 +594,6 @@ PHP_FUNCTION(bccomp)
611594
scale = (int) scale_param;
612595
}
613596

614-
bc_init_num(&first);
615-
bc_init_num(&second);
616-
617597
if (!bc_str2num(&first, ZSTR_VAL(left), scale, false)) {
618598
zend_argument_value_error(1, "is not well-formed");
619599
goto cleanup;
@@ -637,13 +617,12 @@ PHP_FUNCTION(bccomp)
637617
static void bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor)
638618
{
639619
zend_string *numstr;
640-
bc_num num, result;
620+
bc_num num = NULL, result;
641621

642622
ZEND_PARSE_PARAMETERS_START(1, 1)
643623
Z_PARAM_STR(numstr)
644624
ZEND_PARSE_PARAMETERS_END();
645625

646-
bc_init_num(&num);
647626
bc_init_num(&result);
648627

649628
if (php_str2num(&num, ZSTR_VAL(numstr)) == FAILURE) {
@@ -681,7 +660,7 @@ PHP_FUNCTION(bcround)
681660
zend_string *numstr;
682661
zend_long precision = 0;
683662
zend_long mode = PHP_ROUND_HALF_UP;
684-
bc_num num, result;
663+
bc_num num = NULL, result;
685664

686665
ZEND_PARSE_PARAMETERS_START(1, 3)
687666
Z_PARAM_STR(numstr)
@@ -705,7 +684,6 @@ PHP_FUNCTION(bcround)
705684
return;
706685
}
707686

708-
bc_init_num(&num);
709687
bc_init_num(&result);
710688

711689
if (php_str2num(&num, ZSTR_VAL(numstr)) == FAILURE) {

ext/bcmath/libbcmath/src/str2num.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
/* Convert strings to bc numbers. Base 10 only.*/
3838

39+
/* Assumes `num` points to NULL, i.e. does yet not hold a number. */
3940
bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
4041
{
4142
size_t digits = 0;
@@ -45,8 +46,7 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
4546
char *fractional_end = NULL;
4647
bool zero_int = false;
4748

48-
/* Prepare num. */
49-
bc_free_num (num);
49+
ZEND_ASSERT(*num == NULL);
5050

5151
/* Check for valid number and count digits. */
5252
if ((*ptr == '+') || (*ptr == '-')) {

0 commit comments

Comments
 (0)