Skip to content

⚡ Bolt: [performance improvement] rule execution bottlenecks#199

Open
ruzaqiarkan-eng wants to merge 2 commits into
developfrom
bolt-performance-optimizations-v1-4014674705685834553
Open

⚡ Bolt: [performance improvement] rule execution bottlenecks#199
ruzaqiarkan-eng wants to merge 2 commits into
developfrom
bolt-performance-optimizations-v1-4014674705685834553

Conversation

@ruzaqiarkan-eng

Copy link
Copy Markdown
Collaborator

This PR implements two high-impact performance optimizations within the FlexiRule execution engine:

  1. Rule Version Hashing Optimization: Re-computing the SHA256 hash by iterating through all actions inside get_rule_version_hash on every action plan lookup created massive overhead ($O(N^2)$). This is now cached using Frappe's request-local cache (frappe.local.flexirule_rule_hashes), dropping the cost to $O(1)$ for subsequent lookups in the execution thread.

  2. Expression Context Refactoring: Static helper functions (_get_meta, _is_submittable, etc.) were previously re-defined inside _build_eval_locals for every condition or value evaluation. These have been moved to the module level, and the execution context is now built using a shallow copy of a BASE_EVAL_CONTEXT, significantly reducing function definition overhead and garbage collection pressure.

These changes target the hottest paths in the rule evaluation loop, making the engine measurably faster and more efficient for complex rules.


PR created automatically by Jules for task 4014674705685834553 started by @ruzaqiarkan-eng

- Implemented request-local caching for rule version hashes in `action_plan_cache.py` to reduce complexity from O(N^2) to O(1) for repeated lookups.
- Refactored `_build_eval_locals` in `engine.py` to use module-level helper functions and a pre-built base context, minimizing overhead in the expression evaluation hot path.
- Reduced CPU churn and memory allocations during rule execution.
@google-labs-jules

Copy link
Copy Markdown
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.

- Implemented request-local caching for rule version hashes in `action_plan_cache.py` to reduce complexity from O(N^2) to O(1) for repeated lookups.
- Added cache invalidation for rule hashes in `clear_rule_action_plan_cache`.
- Refactored `_build_eval_locals` in `engine.py` to use module-level helper functions and a pre-built base context, minimizing overhead in the expression evaluation hot path.
- Updated `test_rule_version_hash_changes_when_execution_payload_changes` to account for local caching.
- Reduced CPU churn and memory allocations during rule execution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

1 participant