# Using Polars Plugins for a 14x Speed Boost with Rust | by Nelson Griffiths | Nov, 2023

## Achieving high speed outside the native Polars library

Polars is taking the world by storm thanks to it’s speed, memory efficiency, and beautiful API. If you want to know how powerful it is, look no further than the DuckDB Benchmarks. And these aren’t even using the most recent version of Polars.

For all the amazing things Polars can do though, it has not traditionally been a better solution than Pandas to do ALL the calculations you might want to do. There are a few exceptions where Polars has not outperformed. With the recent release of the Polars plugin system for Rust though, that may no longer be the case.

What exactly is a polars plugin? It is simply a way to create your own Polars Expressions using native Rust and exposing those to expressions using a custom namespace. It allows you to take the speed of Rust, and apply it to your Polars DataFrame to perform calculations in a way that takes advantage of the speed and built-in tooling Polars provides.

Let’s take a look at some concrete examples.

## Sequential Calculations

One area that Polars seems to lack some functionality is operations that require a knowledge of the previous value of a DataFrame. Calculations that are sequential in nature are not always super easy or efficient to write in native Polars expressions. Let’s take a look at one specific example.

We have the following algorithm to calculate the cumulative value of an array of numbers for a given run, defined as a set of numbers that have the same sign. For example:

`┌───────┬───────────┐│ value ┆ run_value ││ ---   ┆ ---       ││ i64   ┆ i64       │╞═══════╪═══════════╡│ 1     ┆ 1         │   # First run starts here│ 2     ┆ 3         ││ 3     ┆ 6         ││ -1    ┆ -1        │   # Run resets here│ -2    ┆ -3        ││ 1     ┆ 1         │   # Run resets here└───────┴───────────┘`

So we want to have a cumulative sum of a column which resets every time the sign of the value switches from either positive to negative or negative to positive.

`def calculate_runs_pd(s: pd.Series) -> pd.Series:out = []is_positive = Truecurrent_value = 0.0for value in s:if value > 0:if is_positive:current_value += valueelse:current_value = valueis_positive = Trueelse:if is_positive:current_value = valueis_positive = Falseelse:current_value += valueout.append(current_value)return pd.Series(out)`