Skip to content

Commit

Permalink
fix accidental quadratic behavior in rolling_groupby (#3510)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed May 26, 2022
1 parent 487379a commit df22b1b
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions polars/polars-time/src/windows/groupby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,8 @@ pub fn groupby_windows(
(groups, lower_bound, upper_bound)
}

fn find_offset(time: &[i64], b: Bounds, closed: ClosedWindow) -> Option<usize> {
time.iter()
.enumerate()
.find_map(|(i, t)| match b.is_member(*t, closed) {
true => None,
false => Some(i),
})
fn find_offset(time: &[i64], b: Bounds, closed: ClosedWindow) -> usize {
time.partition_point(|v| b.is_member(*v, closed))
}

/// Different from `groupby_windows`, where define window buckets and search which values fit that
Expand Down Expand Up @@ -172,8 +167,10 @@ pub fn groupby_values(
lagging_offset += 1;
}

let slice = &time[lagging_offset..];
let len = find_offset(slice, b, closed_window).unwrap_or(slice.len());
// Safety
// we just iterated over value i.
let slice = unsafe { time.get_unchecked(lagging_offset..) };
let len = find_offset(slice, b, closed_window);

[lagging_offset as IdxSize, len as IdxSize]
})
Expand Down

0 comments on commit df22b1b

Please sign in to comment.