Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Removed expensive compute in hot loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Nov 14, 2021
1 parent 12a2a5b commit 41e98d4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
22 changes: 13 additions & 9 deletions src/compute/arithmetics/decimal/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,23 @@ pub fn adaptive_mul(
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(*lhs_p, *lhs_s, *rhs_p, *rhs_s);

let mut result = Vec::new();
for (l, r) in lhs.values().iter().zip(rhs.values().iter()) {
let shift = 10i128.pow(diff as u32);
let shift_1 = 10i128.pow(res_s as u32);
let mut max = max_value(res_p);

let iter = lhs.values().iter().zip(rhs.values().iter()).map(|(l, r)| {
// Based on the array's scales one of the arguments in the sum has to be shifted
// to the left to match the final scale
let res = if lhs_s > rhs_s {
l.checked_mul(r * 10i128.pow(diff as u32))
l.checked_mul(r * shift)
.expect("Mayor overflow for multiplication")
} else {
(l * 10i128.pow(diff as u32))
(l * shift)
.checked_mul(*r)
.expect("Mayor overflow for multiplication")
};

let res = res / 10i128.pow(res_s as u32);
let res = res / shift_1;

// The precision of the resulting array will change if one of the
// multiplications during the iteration produces a value bigger
Expand All @@ -247,15 +250,16 @@ pub fn adaptive_mul(
// 10.0000 -> 6, 4
// -----------------
// 100.0000 -> 7, 4
if res.abs() > max_value(res_p) {
if res.abs() > max {
res_p = number_digits(res);
max = max_value(res_p);
}

result.push(res);
}
res
});
let values = Buffer::from_trusted_len_iter(iter);

let validity = combine_validities(lhs.validity(), rhs.validity());
let values = Buffer::from(result);

Ok(PrimitiveArray::<i128>::from_data(
DataType::Decimal(res_p, res_s),
Expand Down
19 changes: 11 additions & 8 deletions src/compute/arithmetics/decimal/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,16 @@ pub fn adaptive_sub(
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(*lhs_p, *lhs_s, *rhs_p, *rhs_s);

let mut result = Vec::new();
for (l, r) in lhs.values().iter().zip(rhs.values().iter()) {
let shift = 10i128.pow(diff as u32);
let mut max = max_value(res_p);

let iter = lhs.values().iter().zip(rhs.values().iter()).map(|(l, r)| {
// Based on the array's scales one of the arguments in the sum has to be shifted
// to the left to match the final scale
let res: i128 = if lhs_s > rhs_s {
l - r * 10i128.pow(diff as u32)
l - r * shift
} else {
l * 10i128.pow(diff as u32) - r
l * shift - r
};

// The precision of the resulting array will change if one of the
Expand All @@ -216,15 +218,16 @@ pub fn adaptive_sub(
// 00.0001 -> 6, 4
// -----------------
// -100.0000 -> 7, 4
if res.abs() > max_value(res_p) {
if res.abs() > max {
res_p = number_digits(res);
max = max_value(res_p);
}

result.push(res);
}
res
});
let values = Buffer::from_trusted_len_iter(iter);

let validity = combine_validities(lhs.validity(), rhs.validity());
let values = Buffer::from(result);

Ok(PrimitiveArray::<i128>::from_data(
DataType::Decimal(res_p, res_s),
Expand Down

0 comments on commit 41e98d4

Please sign in to comment.