Skip to content

Commit

Permalink
perf: Memoize duplicates in rolling-gb-dyn (#19939)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Nov 23, 2024
1 parent 05f2abb commit cbd7857
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions crates/polars-time/src/windows/group_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,14 @@ pub(crate) fn group_by_values_iter_lookbehind(
.iter()
.enumerate()
.map(move |(mut i, t)| {
i += start_offset;

// Fast path for duplicates.
if *t == last && i > start_offset {
if *t == last && i > 0 {
let len = end - start;
let offset = start as IdxSize;
return Ok((offset, len as IdxSize));
}
last = *t;
i += start_offset;

let lower = add(&offset, *t, tz.as_ref())?;
let upper = *t;
Expand Down Expand Up @@ -327,7 +326,17 @@ pub(crate) fn group_by_values_iter_window_behind_t(

let mut start = 0;
let mut end = start;
let mut last = time[0];
let mut started = false;
time.iter().map(move |lower| {
// Fast path for duplicates.
if *lower == last && started {
let len = end - start;
let offset = start as IdxSize;
return Ok((offset, len as IdxSize));
}
last = *lower;
started = true;
let lower = add(&offset, *lower, tz.as_ref())?;
let upper = add(&period, lower, tz.as_ref())?;

Expand Down Expand Up @@ -377,7 +386,16 @@ pub(crate) fn group_by_values_iter_partial_lookbehind(

let mut start = 0;
let mut end = start;
let mut last = time[0];
time.iter().enumerate().map(move |(i, lower)| {
// Fast path for duplicates.
if *lower == last && i > 0 {
let len = end - start;
let offset = start as IdxSize;
return Ok((offset, len as IdxSize));
}
last = *lower;

let lower = add(&offset, *lower, tz.as_ref())?;
let upper = add(&period, lower, tz.as_ref())?;

Expand Down Expand Up @@ -429,7 +447,18 @@ pub(crate) fn group_by_values_iter_lookahead(
let mut start = start_offset;
let mut end = start;

let mut last = time[start_offset];
let mut started = false;
time[start_offset..upper_bound].iter().map(move |lower| {
// Fast path for duplicates.
if *lower == last && started {
let len = end - start;
let offset = start as IdxSize;
return Ok((offset, len as IdxSize));
}
started = true;
last = *lower;

let lower = add(&offset, *lower, tz.as_ref())?;
let upper = add(&period, lower, tz.as_ref())?;

Expand Down

0 comments on commit cbd7857

Please sign in to comment.