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

perf: Speed up list operations that use amortized_iter() #20964

Merged
merged 9 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 6 additions & 2 deletions crates/polars-core/src/chunked_array/list/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::marker::PhantomData;
use std::ptr::NonNull;
use std::rc::Rc;

use crate::chunked_array::flags::StatisticsFlags;
use crate::prelude::*;
use crate::series::amortized_iter::{unstable_series_container_and_ptr, AmortSeries, ArrayBox};

Expand Down Expand Up @@ -81,10 +82,13 @@ impl<I: Iterator<Item = Option<ArrayBox>>> Iterator for AmortizedListIter<'_, I>
// update the inner state
unsafe { *self.inner.as_mut() = array_ref };

// As an optimization, we try to minimize how many calls to
// _get_inner_mut() we do.
let series_mut_inner = series_mut._get_inner_mut();
// last iteration could have set the sorted flag (e.g. in compute_len)
series_mut.clear_flags();
series_mut_inner._set_flags(StatisticsFlags::empty());
// make sure that the length is correct
series_mut._get_inner_mut().compute_len();
series_mut_inner.compute_len();
}

// SAFETY:
Expand Down
1 change: 1 addition & 0 deletions crates/polars-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![cfg_attr(feature = "simd", feature(portable_simd))]
#![allow(ambiguous_glob_reexports)]
#![cfg_attr(feature = "nightly", allow(clippy::non_canonical_partial_ord_impl))] // remove once stable

itamarst marked this conversation as resolved.
Show resolved Hide resolved
extern crate core;

#[macro_use]
Expand Down
23 changes: 23 additions & 0 deletions py-polars/tests/benchmark/test_list_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Callable
itamarst marked this conversation as resolved.
Show resolved Hide resolved

import pytest

import polars as pl

pytestmark = pytest.mark.benchmark()


@pytest.fixture(scope="module")
def lists_and_values() -> pl.DataFrame:
return pl.DataFrame(
{"lists": [[0, 2, 1, 3, 5]] * 10_000, "values": [3, 1, 4, 5, 0] * 2000}
)


def test_list_contains(
benchmark: Callable[[Callable[[], None]], object], lists_and_values: pl.DataFrame
) -> None:
def go() -> None:
lists_and_values.select(pl.col("lists").list.contains(pl.col("values")))

benchmark(go)
Loading