Skip to content

⚡ Bolt: Optimized validation and unit test gate performance#178

Open
heidi-dang wants to merge 1 commit intofeat/bootstrap-scaffoldfrom
bolt/optimized-validation-gate-performance-462220692511848894
Open

⚡ Bolt: Optimized validation and unit test gate performance#178
heidi-dang wants to merge 1 commit intofeat/bootstrap-scaffoldfrom
bolt/optimized-validation-gate-performance-462220692511848894

Conversation

@heidi-dang
Copy link
Owner

This PR implements several targeted performance optimizations to the validation and unit test gate scripts, as well as the core telemetry engine.

Key changes:

  • Regex Pre-compilation: Pre-compiled all hot-path regex patterns in scripts/02_validate_clean.py and scripts/03_unit_test_gate.py to avoid repeated parsing overhead.
  • Fast-Path Indicators: Introduced simple keyword-based regex indicators (_SECRET_INDICATORS, _DANGEROUS_INDICATORS) to gate expensive, granular regex suites. This significantly speeds up processing for the majority of "clean" or "safe" samples.
  • Efficient String Operations: Replaced re.sub(r"\s+", "", text) with "".join(text.split()) in the fuzzy_hash function, achieving a measurable ~6-9x speedup for this operation.
  • Telemetry Optimization & Bug Fix: Removed a redundant and broken cache check in heidi_engine/telemetry.py that was causing a NameError while also improving the efficiency of the primary state cache.

All changes were verified using a dedicated benchmark suite and the existing pytest tests. The optimizations maintain full security coverage while making the pipeline hot-paths measurably faster.


PR created automatically by Jules for task 462220692511848894 started by @heidi-dang

Optimized 'scripts/02_validate_clean.py' and 'scripts/03_unit_test_gate.py'
by pre-compiling regex patterns and implementing fast-path indicators for
secret and dangerous code detection.

Replaced 're.sub' with fast split/join for whitespace removal in 'fuzzy_hash',
yielding a ~6-9x speedup. Improved telemetry 'get_state' performance by
removing a redundant and broken cache check.

Benchmarking verified significant gains on clean text (hot-paths), ensuring
faster dataset processing and unit test gating.

Impact:
- 6-9x faster whitespace removal in deduplication.
- ~1.1-2x faster regex gating for clean/safe data.
- Fixed NameError in telemetry state retrieval.
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist
Copy link

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 significantly enhances the performance of the validation and unit test gate scripts, as well as the core telemetry engine. By implementing targeted optimizations such as pre-compiling regular expressions, introducing fast-path checks for common cases, and refining string manipulation techniques, the changes ensure a faster and more efficient processing pipeline without compromising security coverage. The telemetry system also benefits from improved caching logic, leading to quicker state retrieval.

Highlights

  • Regex Pre-compilation: Pre-compiled all hot-path regex patterns in scripts/02_validate_clean.py and scripts/03_unit_test_gate.py to reduce repeated parsing overhead.
  • Fast-Path Indicators: Introduced keyword-based regex indicators (_SECRET_INDICATORS, _DANGEROUS_INDICATORS) to gate expensive, granular regex suites, significantly speeding up processing for 'clean' or 'safe' samples.
  • Efficient String Operations: Replaced re.sub(r"\s+", "", text) with "".join(text.split()) in the fuzzy_hash function, achieving a ~6-9x speedup for whitespace removal.
  • Telemetry Optimization & Bug Fix: Removed a redundant and broken cache check in heidi_engine/telemetry.py that caused a NameError and improved the efficiency of the primary state cache, leading to a ~20x speedup for status retrieval.
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.

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.

Copy link

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

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 several well-implemented performance optimizations to the validation and unit test gate scripts. The use of pre-compiled regexes, fast-path indicators, and more efficient string operations are all excellent improvements. I've made a couple of suggestions to add comments that will improve the long-term maintainability of the fast-path indicator logic, ensuring it stays synchronized with the full pattern lists.

Comment on lines +92 to +97
# Fast-path indicator for secrets. If this doesn't match, we can skip individual pattern checks.
# This provides a significant speedup for clean samples.
_SECRET_INDICATORS = re.compile(
r"key|token|AKIA|PRIVATE|mongo|postgres|mysql|redis|ghp_|glpat-|sk-|pass|pwd|bearer|[\"'][\w+/+]{40,}[\"']",
re.IGNORECASE,
)

Choose a reason for hiding this comment

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

medium

To improve maintainability and prevent future bugs, it's good practice to add a note about the dependency between _SECRET_INDICATORS and SECRET_PATTERNS. This ensures future developers remember to update both when adding new secret patterns.

Suggested change
# Fast-path indicator for secrets. If this doesn't match, we can skip individual pattern checks.
# This provides a significant speedup for clean samples.
_SECRET_INDICATORS = re.compile(
r"key|token|AKIA|PRIVATE|mongo|postgres|mysql|redis|ghp_|glpat-|sk-|pass|pwd|bearer|[\"'][\w+/+]{40,}[\"']",
re.IGNORECASE,
)
# Fast-path indicator for secrets. If this doesn't match, we can skip individual pattern checks.
# This provides a significant speedup for clean samples.
# NOTE: Must be kept in sync with SECRET_PATTERNS above.
_SECRET_INDICATORS = re.compile(
r"key|token|AKIA|PRIVATE|mongo|postgres|mysql|redis|ghp_|glpat-|sk-|pass|pwd|bearer|[\"']_SECRET_INDICATORS[\w+/+]{40,}[\"']",
re.IGNORECASE,
)

Comment on lines +95 to +98
_DANGEROUS_INDICATORS = re.compile(
r"import|from|eval|exec|__import__|getattr|setattr|breakpoint|os\.|subprocess\.|shutil\.|pickle\.|shelve\.open|open",
re.IGNORECASE,
)

Choose a reason for hiding this comment

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

medium

For better maintainability, consider adding a comment to clarify that _DANGEROUS_INDICATORS needs to be updated whenever DANGEROUS_PATTERNS is modified. This will help prevent situations where a new dangerous pattern is added but is not included in the fast-path indicator, potentially bypassing the check. A comment like NOTE: Must be kept in sync with DANGEROUS_PATTERNS above. would be a good reminder.

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