Skip to content
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

chore(rust): use unwrap_or_else and get_unchecked_release in rolling kernels #12405

Merged
merged 1 commit into from
Nov 13, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/polars-arrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ foreign_vec = { version = "0.1" }
hashbrown = { workspace = true }
num-traits = { workspace = true }
polars-error = { workspace = true }
polars-utils = { workspace = true }
serde = { workspace = true, features = ["derive"], optional = true }
simdutf8 = { workspace = true }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::Debug;

use num_traits::ToPrimitive;
use polars_error::polars_ensure;
use polars_utils::slice::GetSaferUnchecked;

use super::QuantileInterpolOptions::*;
use super::*;
Expand Down Expand Up @@ -60,12 +61,16 @@ impl<
if top_idx == idx {
// safety
// we are in bounds
unsafe { *vals.get_unchecked(idx) }
unsafe { *vals.get_unchecked_release(idx) }
} else {
// safety
// we are in bounds
let (mid, mid_plus_1) =
unsafe { (*vals.get_unchecked(idx), *vals.get_unchecked(idx + 1)) };
let (mid, mid_plus_1) = unsafe {
(
*vals.get_unchecked_release(idx),
*vals.get_unchecked_release(idx + 1),
)
};

(mid + mid_plus_1) / T::from::<f64>(2.0f64).unwrap()
}
Expand All @@ -77,16 +82,18 @@ impl<
if top_idx == idx {
// safety
// we are in bounds
unsafe { *vals.get_unchecked(idx) }
unsafe { *vals.get_unchecked_release(idx) }
} else {
let proportion = T::from(float_idx - idx as f64).unwrap();
proportion * (vals[top_idx] - vals[idx]) + vals[idx]
proportion
* (*vals.get_unchecked_release(top_idx) - *vals.get_unchecked_release(idx))
+ *vals.get_unchecked_release(idx)
}
},
_ => {
// safety
// we are in bounds
unsafe { *vals.get_unchecked(idx) }
unsafe { *vals.get_unchecked_release(idx) }
},
}
}
Expand Down
8 changes: 3 additions & 5 deletions crates/polars-arrow/src/legacy/kernels/rolling/nulls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ where
// Safety; we are in bounds
let mut agg_window = unsafe { Agg::new(values, validity, start, end, params) };

let mut validity = match create_validity(min_periods, len, window_size, det_offsets_fn) {
Some(v) => v,
None => {
let mut validity = create_validity(min_periods, len, window_size, det_offsets_fn)
.unwrap_or_else(|| {
let mut validity = MutableBitmap::with_capacity(len);
validity.extend_constant(len, true);
validity
},
};
});

let out = (0..len)
.map(|idx| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use polars_utils::slice::GetSaferUnchecked;

use super::*;

pub struct QuantileWindow<'a, T: NativeType + IsFloat + PartialOrd> {
Expand Down Expand Up @@ -65,7 +67,8 @@ impl<
QuantileInterpolOptions::Midpoint => {
let top_idx = ((length as f64 - 1.0) * self.prob).ceil() as usize;
Some(
(values[idx].unwrap() + values[top_idx].unwrap())
(values.get_unchecked_release(idx).unwrap()
+ values.get_unchecked_release(top_idx).unwrap())
/ T::from::<f64>(2.0f64).unwrap(),
)
},
Expand All @@ -74,16 +77,18 @@ impl<
let top_idx = f64::ceil(float_idx) as usize;

if top_idx == idx {
Some(values[idx].unwrap())
Some(values.get_unchecked_release(idx).unwrap())
} else {
let proportion = T::from(float_idx - idx as f64).unwrap();
Some(
proportion * (values[top_idx].unwrap() - values[idx].unwrap())
+ values[idx].unwrap(),
proportion
* (values.get_unchecked_release(top_idx).unwrap()
- values.get_unchecked_release(idx).unwrap())
+ values.get_unchecked_release(idx).unwrap(),
)
}
},
_ => Some(values[idx].unwrap()),
_ => Some(values.get_unchecked_release(idx).unwrap()),
}
}

Expand Down