Skip to content

Commit

Permalink
prevent redundat allocation on rolling_groupby
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jan 22, 2022
1 parent 3e739cc commit f64a027
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions polars/polars-core/src/frame/groupby/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ impl DataFrame {
) -> Result<(Series, GroupsProxy)> {
let dt = dt.datetime().unwrap().clone();

let groups = dt
let mut groups = dt
.downcast_iter()
.flat_map(|vals| {
.map(|vals| {
let ts = vals.values().as_slice();
polars_time::groupby::groupby_values(
options.period,
Expand All @@ -315,8 +315,14 @@ impl DataFrame {
)
})
.collect::<Vec<_>>();
let groups = GroupsProxy::Slice(groups);

// we don't flatmap because in case of a single chunk we don't need to reallocate the inner vec,
// just pop it.
let groups = if groups.len() == 1 {
GroupsProxy::Slice(groups.pop().unwrap())
} else {
GroupsProxy::Slice(groups.into_iter().flatten().collect())
};
dt.cast(time_type).map(|s| (s, groups))
}
}
Expand Down

0 comments on commit f64a027

Please sign in to comment.