-
Notifications
You must be signed in to change notification settings - Fork 1k
[Variant] rescale_decimal followup
#8655
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
Conversation
0603e21 to
594cf52
Compare
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.
Looks like a nice improvement to me.
@scovich let me know whey you think this is ready to go and I'll merge it in
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.
Nice! Couple small nits.
| let scaled = if is_infallible_cast { | ||
| Some(O::Native::from_decimal(value).unwrap().mul_wrapping(mul)) | ||
| } else { | ||
| O::Native::from_decimal(value).and_then(|x| x.mul_checked(mul).ok()) | ||
| }; |
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.
| let scaled = if is_infallible_cast { | |
| Some(O::Native::from_decimal(value).unwrap().mul_wrapping(mul)) | |
| } else { | |
| O::Native::from_decimal(value).and_then(|x| x.mul_checked(mul).ok()) | |
| }; | |
| let value = O::Native::from_decimal(value); | |
| let scaled = if is_infallible_cast { | |
| Some(value.unwrap().mul_wrapping(mul)) | |
| } else { | |
| value.and_then(|x| x.mul_checked(mul).ok()) | |
| }; |
| (O::Native::from_decimal(adjusted), is_infallible_cast) | ||
| }; | ||
|
|
||
| scaled.filter(|v| is_infallible_cast || O::is_valid_decimal_precision(*v, output_precision)) |
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.
aside: I believe compiler "jump threading" optimizations should bypass the filter call when is_infallible_cast=true. Not sure how to verify that, tho.
Alternatively, we can drastically increase the probability of successful jump threading by defining mut scale and then:
| scaled.filter(|v| is_infallible_cast || O::is_valid_decimal_precision(*v, output_precision)) | |
| if !is_infallible_cast { | |
| scaled = scaled.filter(|v| O::is_valid_decimal_precision(*v, output_precision)); | |
| } | |
| scaled |
(if we pursue that, we may want to change the logic to is_fallible_cast to avoid error-prone double negative logic)
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.
To make it simple, how about this
if is_infallible_cast {
scaled
} else {
scaled.filter(|v| O::is_valid_decimal_precision(*v, output_precision))
}|
Thanks @liamzwbao and @scovich |
Which issue does this PR close?
Decimal32/64/128/256#8552.Rationale for this change
Code cleanup and optimization
What changes are included in this PR?
Addressed the post-comments in #8552 and refactor/optimize the method
rescale_decimalAre these changes tested?
Covered by existing tests
Are there any user-facing changes?
No