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(python): Improve performance of expr_to_lit_or_expr for arguments of type Expr by ~80% #6967

Merged
merged 1 commit into from
Feb 18, 2023
Merged

perf(python): Improve performance of expr_to_lit_or_expr for arguments of type Expr by ~80% #6967

merged 1 commit into from
Feb 18, 2023

Conversation

jakob-keller
Copy link
Contributor

expr_to_lit_or_expr is used extensively throughout the codebase. When providing arguments of type Expr in an attempt to improve performance, the current implementation can do better IMO.

Benchmark

python3 -m timeit -s "import polars; expr = polars.col('test')" "polars.internals.expr_to_lit_or_expr(expr)"

0.16.6: 351 nsec per loop
After PR: 74.1 nsec per loop

Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair addition. But please change the final elif not isinstance(expr, Expr) line to else. Then this can be merged.

I will probably revise this parsing logic in the near future, but this is a good thing to consider.

Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Good addition. Expression inputs should be the default, anything else could be considered an ergonomic shortcut. So I think it's good to give 'preferential treatment' to expressions here.

@jakob-keller
Copy link
Contributor Author

jakob-keller commented Feb 17, 2023

Thanks! Good addition. Expression inputs should be the default, anything else could be considered an ergnomic shortcut. So I think it's good to give 'preferential treatment' to expressions here.

Thanks for the speedy review!

If the polars community is interested, I might suggest a number of similar improvements. At the moment, I am mostly using the low-level Python bindings (in polars.polars), since the high-level Python wrappers are not performant enough for my particular use case (high-frequency, real-time feature building for use in ML inference). If I could remove the main performance issues, I would use the regular Python API and be happy.

@stinodego
Copy link
Member

I'd be very interested in your input. Feel free to create an issue for this to start a discussion and track progress on similar improvements, or just send some more PRs our way!

@stinodego stinodego changed the title Improve performance of expr_to_lit_or_expr for arguments of type Expr by ~80% perf(python): Improve performance of expr_to_lit_or_expr for arguments of type Expr by ~80% Feb 18, 2023
@github-actions github-actions bot added performance Performance issues or improvements python Related to Python Polars labels Feb 18, 2023
@stinodego stinodego merged commit 49f71a8 into pola-rs:master Feb 18, 2023
@jakob-keller jakob-keller deleted the expr_to_lit_or_expr-fastpath branch February 18, 2023 08:43
@jakob-keller
Copy link
Contributor Author

I'd be very interested in your input. Feel free to create an issue for this to start a discussion and track progress on similar improvements, or just send some more PRs our way!

There you go: #7019, #7020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance issues or improvements python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants