Skip to content

Performance optimization - #49

Open
mzueva wants to merge 1 commit into
mainfrom
mzueva/performance-fix
Open

mzueva wants to merge 1 commit into
mainfrom
mzueva/performance-fix

Conversation

@mzueva

@mzueva mzueva commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces significant performance optimizations by refactoring the liability calculation and risk classification logic to leverage Polars' vectorized operations. By replacing iterative, row-wise Python processing with highly optimized Polars expressions and pre-compiling regular expressions, the script's execution speed for large datasets is substantially improved. This change enhances efficiency without altering the core functionality or output of the liability analysis.

Highlights

  • Vectorized Polars Operations: Replaced inefficient row-wise Python functions with native Polars vectorized expressions for liability identification, risk classification, and summary generation, significantly boosting processing speed.
  • Regex Pre-compilation: Implemented pre-compilation of regular expressions to optimize pattern matching operations, reducing overhead during repeated use, especially within the annotation loop.
  • Code Simplification: Removed several debug print statements and deprecated row-wise processing functions, leading to cleaner and more maintainable code.
Changelog
  • .changeset/upset-sites-eat.md
    • Added a new changeset file to document the performance optimization patch.
  • liabilities-calc-script/src/main.py
    • Introduced global variables POLARS_W_CDR3_PATTERN and COMPILED_LIABILITY_REGEX to support vectorized regex operations and address Polars Rust regex limitations.
    • Modified get_active_liability_definitions to populate COMPILED_LIABILITY_REGEX with pre-compiled patterns.
    • Removed the identify_liabilities, classify_risk, overall_risk_func, and _create_sequence_liabilities_summary_str functions.
    • Added new vectorized Polars expression builder functions: _build_vectorized_liability_expr, _build_vectorized_risk_expr, _build_vectorized_overall_risk_expr, and _build_vectorized_summary_expr.
    • Updated the main function to utilize the new vectorized Polars expressions for liability calculation, risk classification, and summary generation, replacing map_elements calls.
    • Modified the annotation loop in main to use pre-compiled regex patterns for improved performance.
    • Removed several debug print statements from the main function.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a significant performance optimization by refactoring the liability and risk calculation logic to use vectorized Polars expressions instead of row-wise Python functions with map_elements. This is an excellent improvement that leverages the power of Polars for better performance. The new expression-building functions are well-structured. I've added a few suggestions to make the expression-building code even more concise and idiomatic by using polars.any_horizontal. The removal of leftover debug print statements is also a good cleanup.

Comment on lines +177 to +182
any_in_bounds = any_in_bounds_parts[0]
for e in any_in_bounds_parts[1:]:
any_in_bounds = any_in_bounds | e
any_has_c = any_has_c_parts[0]
for e in any_has_c_parts[1:]:
any_has_c = any_has_c | e

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The manual reduction of boolean expressions using loops can be simplified. Using polars.any_horizontal is more concise and idiomatic for this purpose, and it directly operates on a list of expressions.

            any_in_bounds = pl.any_horizontal(any_in_bounds_parts)
            any_has_c = pl.any_horizontal(any_has_c_parts)

Comment on lines +234 to +236
result = conditions[0]
for c in conditions[1:]:
result = result | c

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This manual reduction of a list of boolean conditions can be simplified by using polars.any_horizontal(conditions). This is more idiomatic and improves readability.

        result = pl.any_horizontal(conditions)

Comment on lines +250 to +257
has_high = pl.lit(False)
has_medium = pl.lit(False)
has_low = pl.lit(False)
for col_name in risk_col_names:
c = pl.col(col_name).cast(pl.Utf8)
has_high = has_high | (c == "High")
has_medium = has_medium | (c == "Medium")
has_low = has_low | (c == "Low")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This loop can be refactored to be more declarative and potentially more performant by using polars.any_horizontal for each risk level check. This avoids iterating in Python and builds the expression tree more directly.

    has_high = pl.any_horizontal((pl.col(c).cast(pl.Utf8) == "High") for c in risk_col_names)
    has_medium = pl.any_horizontal((pl.col(c).cast(pl.Utf8) == "Medium") for c in risk_col_names)
    has_low = pl.any_horizontal((pl.col(c).cast(pl.Utf8) == "Low") for c in risk_col_names)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant