[php-src] master: Avoid unnecessary destruction in bc_add()

From: Date: Thu, 02 May 2024 18:04:15 +0000
Subject: [php-src] master: Avoid unnecessary destruction in bc_add()
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-128309@lists.php.net to get a copy of this message
Author: Niels Dossche (nielsdos) Date: 2024-05-02T20:03:59+02:00 Commit: https://github.com/php/php-src/commit/959e11544c82f2b91933aef65280abe7227a4a91 Raw diff: https://github.com/php/php-src/commit/959e11544c82f2b91933aef65280abe7227a4a91.diff Avoid unnecessary destruction in bc_add() Changed paths: M ext/bcmath/bcmath.c M ext/bcmath/libbcmath/src/add.c M ext/bcmath/libbcmath/src/bcmath.h M ext/bcmath/libbcmath/src/sqrt.c Diff: diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 277d956d3fd6..874a430559dc 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -148,7 +148,7 @@ PHP_FUNCTION(bcadd) zend_string *left, *right; zend_long scale_param; bool scale_param_is_null = 1; - bc_num first = NULL, second = NULL, result; + bc_num first = NULL, second = NULL, result = NULL; int scale; ZEND_PARSE_PARAMETERS_START(2, 3) @@ -167,8 +167,6 @@ PHP_FUNCTION(bcadd) scale = (int) scale_param; } - bc_init_num(&result); - if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) { zend_argument_value_error(1, "is not well-formed"); goto cleanup; @@ -179,7 +177,7 @@ PHP_FUNCTION(bcadd) goto cleanup; } - bc_add (first, second, &result, scale); + result = bc_add (first, second, scale); RETVAL_NEW_STR(bc_num2str_ex(result, scale)); diff --git a/ext/bcmath/libbcmath/src/add.c b/ext/bcmath/libbcmath/src/add.c index 1e6285a0df19..23f922b7eb6d 100644 --- a/ext/bcmath/libbcmath/src/add.c +++ b/ext/bcmath/libbcmath/src/add.c @@ -39,7 +39,7 @@ N1 is added to N2 and the result placed into RESULT. SCALE_MIN is the minimum scale for the result. */ -void bc_add(bc_num n1, bc_num n2, bc_num *result, size_t scale_min) +bc_num bc_add(bc_num n1, bc_num n2, size_t scale_min) { bc_num sum = NULL; @@ -67,7 +67,5 @@ void bc_add(bc_num n1, bc_num n2, bc_num *result, size_t scale_min) } } - /* Clean up and return. */ - bc_free_num (result); - *result = sum; + return sum; } diff --git a/ext/bcmath/libbcmath/src/bcmath.h b/ext/bcmath/libbcmath/src/bcmath.h index 6d2163b023d2..d046fdb7b9e0 100644 --- a/ext/bcmath/libbcmath/src/bcmath.h +++ b/ext/bcmath/libbcmath/src/bcmath.h @@ -115,7 +115,13 @@ bool bc_is_near_zero(bc_num num, size_t scale); bool bc_is_neg(bc_num num); -void bc_add(bc_num n1, bc_num n2, bc_num *result, size_t scale_min); +bc_num bc_add(bc_num n1, bc_num n2, size_t scale_min); + +#define bc_add_ex(n1, n2, result, scale_min) do { \ + bc_num add_ex = bc_add(n1, n2, scale_min); \ + bc_free_num (result); \ + *(result) = add_ex; \ +} while (0) void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min); diff --git a/ext/bcmath/libbcmath/src/sqrt.c b/ext/bcmath/libbcmath/src/sqrt.c index f29f9ce484ba..8c88b0baeef2 100644 --- a/ext/bcmath/libbcmath/src/sqrt.c +++ b/ext/bcmath/libbcmath/src/sqrt.c @@ -92,7 +92,7 @@ bool bc_sqrt(bc_num *num, size_t scale) bc_free_num (&guess1); guess1 = bc_copy_num(guess); bc_divide(*num, guess, &guess, cscale); - bc_add(guess, guess1, &guess, 0); + bc_add_ex(guess, guess1, &guess, 0); bc_multiply(guess, point5, &guess, cscale); bc_sub(guess, guess1, &diff, cscale + 1); if (bc_is_near_zero(diff, cscale)) {

« previous php.cvs (#128309) next »