Skip to content

Improve BCMath performance and reduce memory usage #14101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions ext/bcmath/libbcmath/src/bcmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,11 @@ typedef enum {PLUS, MINUS} sign;
typedef struct bc_struct *bc_num;

typedef struct bc_struct {
size_t n_len; /* The number of digits before the decimal point. */
size_t n_scale; /* The number of digits after the decimal point. */
char *n_ptr; /* The pointer to the actual storage.
If NULL, n_value points to the inside of another number
(bc_multiply...) and should not be "freed." */
char *n_value; /* The number. Not zero char terminated.
May not point to the same place as n_ptr as
in the case of leading zeros generated. */
int n_refs; /* The number of pointers to this number. */
sign n_sign;
size_t n_len; /* The number of digits before the decimal point. */
size_t n_scale; /* The number of digits after the decimal point. */
char *n_value; /* The number. Not zero char terminated. */
unsigned int n_refs; /* The number of pointers to this number. */
sign n_sign;
} bc_struct;

#ifdef HAVE_CONFIG_H
Expand Down
14 changes: 4 additions & 10 deletions ext/bcmath/libbcmath/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@
/* new_num allocates a number and sets fields to known values. */
bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent)
{
/* PHP Change: malloc() -> pemalloc(), removed free_list code */
bc_num temp = (bc_num) safe_pemalloc(1, sizeof(bc_struct) + length, scale, persistent);
/* PHP Change: malloc() -> pemalloc(), removed free_list code, merged n_ptr and n_value */
bc_num temp = safe_pemalloc(1, sizeof(bc_struct) + length, scale, persistent);
temp->n_sign = PLUS;
temp->n_len = length;
temp->n_scale = scale;
temp->n_refs = 1;
/* PHP Change: malloc() -> pemalloc() */
temp->n_ptr = (char *) safe_pemalloc(1, length, scale, persistent);
temp->n_value = temp->n_ptr;
memset(temp->n_ptr, 0, length + scale);
temp->n_value = (char *) temp + sizeof(bc_struct);
memset(temp->n_value, 0, length + scale);
return temp;
}

Expand All @@ -61,10 +59,6 @@ void _bc_free_num_ex(bc_num *num, bool persistent)
}
(*num)->n_refs--;
if ((*num)->n_refs == 0) {
if ((*num)->n_ptr) {
/* PHP Change: free() -> pefree(), removed free_list code */
pefree((*num)->n_ptr, persistent);
}
pefree(*num, persistent);
}
*num = NULL;
Expand Down
2 changes: 0 additions & 2 deletions ext/bcmath/libbcmath/src/recmul.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ static bc_num new_sub_num(size_t length, size_t scale, char *value)
temp->n_len = length;
temp->n_scale = scale;
temp->n_refs = 1;
temp->n_ptr = NULL;
temp->n_value = value;
return temp;
}
Expand Down Expand Up @@ -273,7 +272,6 @@ void bc_multiply(bc_num n1, bc_num n2, bc_num *prod, size_t scale)

/* Assign to prod and clean up the number. */
pval->n_sign = (n1->n_sign == n2->n_sign ? PLUS : MINUS);
pval->n_value = pval->n_ptr;
pval->n_len = len2 + len1 + 1 - full_scale;
pval->n_scale = prod_scale;
_bc_rm_leading_zeros(pval);
Expand Down
Loading