Skip to content

Refactor bcmath 2 #14110

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

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 9 additions & 3 deletions ext/bcmath/libbcmath/src/num2str.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale)
{
zend_string *str;
char *sptr;
size_t index;
bool signch;
size_t min_scale = MIN(num->n_scale, scale);

Expand All @@ -64,8 +63,15 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale)
if (scale > 0) {
*sptr++ = '.';
sptr = bc_copy_bcd_val(sptr, nptr, nptr + min_scale);
for (index = num->n_scale; index < scale; index++) {
*sptr++ = BCD_CHAR(0);

size_t scale_diff;
if (scale > num->n_scale && (scale_diff = scale - num->n_scale) > 8) {
memset(sptr, BCD_CHAR(0), scale_diff);
sptr += scale_diff;
} else {
for (size_t index = num->n_scale; index < scale; index++) {
*sptr++ = BCD_CHAR(0);
}
Comment on lines +67 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe to always compute scale_diff, the compiler will do the optimization for you, and it will also be cleaner code.
Anyway, this part didn't make a real difference on my system, the UNEXPECTED part did however make an improvement.

To have more stable measurements, I recommend using hyperfine which is what I typically use. It runs the code multiple times and also computes a confidence interval. It can also do warmup rounds to reduce noise. I found the results from this tool to be more stable than only using the benchmark script. To use it here I would recommend to reduce the number of iterations in your benchmark script (to make sure the total runtime is below a second to avoid CPU throttling), and to remove the timing from the benchmark script and only rely on hyperfine instead.

}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/libbcmath/src/str2num.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
if (decimal_point) {
/* search */
fractional_ptr = fractional_end = decimal_point + 1;
if (*fractional_ptr == '\0') {
if (UNEXPECTED(*fractional_ptr == '\0')) {
goto after_fractional;
}

Expand Down
Loading