-
Notifications
You must be signed in to change notification settings - Fork 444
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
Optimize result representation for BcMathCalculator #748
Optimize result representation for BcMathCalculator #748
Conversation
c1ac815
to
d5389be
Compare
d5389be
to
0bddd72
Compare
@Ocramius You implemented Both are currently failing with my changes. From my understanding |
@bcremer changes seem to indicate that removing the scale broke everything in the test suite? |
It's only the explicit |
@bcremer I cannot recall or see why I would have done that. Maybe it was some preliminary work for PreciseMoney, a project that never made the finish line. If no other fails, I think it is fine to change the scale to zero for add and subtract operations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm saying that your patch should NOT touch src/Calculator/BcMathCalculator.php
unless there's a strong reason to do it.
The fact that tests cover that is because there's a BC boundary there.
Hmm, maybe @Ocramius is right. In the end of the day this is a performance patch. It should deal with all features in the library. |
4e8ea5e
to
2ad0ec7
Compare
I see your point. I pushed a solution that will speedup the "real world path" as well as maintain the public api. This will still result in a 50% improvement for certain operations:
|
2ad0ec7
to
c44e629
Compare
I think this good to go, @Ocramius approved? |
src/Calculator/BcMathCalculator.php
Outdated
@@ -31,13 +32,17 @@ public static function compare(string $a, string $b): int | |||
/** @psalm-pure */ | |||
public static function add(string $amount, string $addend): string | |||
{ | |||
return bcadd($amount, $addend, self::SCALE); | |||
$scale = str_contains($amount, '.') ? self::SCALE : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is checking $amount
, but not $addend
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd really just leave these methods alone :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, why not patch bcmath itself to ignore the scale directly, when there's no decimal stuff going on? :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could submit a PR for that too, agreed. Let's do both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be a BC break in the bcmath extension wouldn't it?
It's not about ignoring the scale during the bcmath operation but about the return value of bcmath.
See previous PR for detailed explanation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a breakage, if there's no behavioral change.
If there's noticeable performance overhead, a large chunk of the PHP community could benefit from it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need beauviour change for the performance improvement in the MoneyPHP ctor.
See #747 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, lemme rephrase: I was saying that patching https://github.com/php/php-src/ is not a BC break if there's no behavioral change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we talk past each other.
bcadd('5', '15', 0)
and bcadd('5', '15', 14)
are already comparable performance wise.
What is different is the returned string value:
bcadd('5', '15', 0); // string(2) "20"
bcadd('5', '15', 14); // string(2) "20.00000000000000"
I'd need to change in the php-core.
- bcadd('5', '15', 14); // string(2) "20.00000000000000"
+ bcadd('5', '15', 14); // string(2) "20"
I consider this a major BC break.
The actual performance improvement comes from passing the calculatores return value into Money::__construct
return new Money("20", $currenty); // fast
return new Money("20.00000000000000", $currency); // slow
This is due to additional number parsing if the passed string is valid for FILTER_VALIDATE_INT
:
Line 80 in 8953b87
if (filter_var($amount, FILTER_VALIDATE_INT) === false) { |
That given maybe it's would be a better fix to improve that validation for numeric strings containing int values.
So a fast coercion from 20.00000000000000
to 20
.
I hope that clears things up. Sorry for splitting my PR into two, that whas not helpful to preserve context.
…lculator::substract
c44e629
to
e3d208a
Compare
* Optimize result representation for BcMathCalculator::add and BcMathCalculator::substract
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything is good
Optimize the result representation of some
BcMathCalculator
operations for faster usage in\Money\Money::__construct
.This will provide a 50% performance boost for some operations.
For full history: #747