Skip to content

⚡ Bolt: optimize Context map lookups and lock contention#42

Open
ashyanSpada wants to merge 2 commits intomasterfrom
bolt/optimize-context-lookups-3557716546625986088
Open

⚡ Bolt: optimize Context map lookups and lock contention#42
ashyanSpada wants to merge 2 commits intomasterfrom
bolt/optimize-context-lookups-3557716546625986088

Conversation

@ashyanSpada
Copy link
Copy Markdown
Owner

💡 What: Refactored get_func, get_variable, and value methods in src/context.rs to avoid cloning the whole ContextValue enum, performing a single .get(name) map lookup, and dropping the MutexGuard explicitly before invoking functions. Also formatted code with cargo fmt.
🎯 Why: The previous implementation performed a double map lookup (.is_none() followed by .unwrap()) and cloned the full ContextValue containing large data types unnecessarily. Furthermore, the MutexGuard was held while func(Vec::new()) was executed, keeping the lock active for the duration of the function execution which creates lock contention.
📊 Impact: Reduced memory allocation and mutex contention. The execute_expression benchmark execution time dropped from ~5.60 µs to ~5.38 µs (a ~4% improvement).
🔬 Measurement: Verify the improvement by running cargo bench.


PR created automatically by Jules for task 3557716546625986088 started by @ashyanSpada

💡 What: Refactored `get_func`, `get_variable`, and `value` methods in `src/context.rs` to avoid cloning the whole `ContextValue` enum, performing a single `.get(name)` map lookup, and dropping the `MutexGuard` explicitly before invoking functions. Also formatted code with `cargo fmt`.
🎯 Why: The previous implementation performed a double map lookup (`.is_none()` followed by `.unwrap()`) and cloned the full `ContextValue` containing large data types unnecessarily. Furthermore, the `MutexGuard` was held while `func(Vec::new())` was executed, keeping the lock active for the duration of the function execution which creates lock contention.
📊 Impact: Reduced memory allocation and mutex contention. The `execute_expression` benchmark execution time dropped from ~5.60 µs to ~5.38 µs (a ~4% improvement).
🔬 Measurement: Verify the improvement by running `cargo bench`.

Co-authored-by: ashyanSpada <22587148+ashyanSpada@users.noreply.github.com>
@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.

Copilot AI review requested due to automatic review settings March 20, 2026 21:23
@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 focuses on optimizing the Context map lookups and reducing lock contention within the Bolt expression engine. By refactoring key methods to perform single map lookups, avoid full enum cloning, and manage mutex guards more efficiently, the changes aim to enhance overall performance and reduce memory allocation. The improvements are validated by a measurable reduction in benchmark execution time.

Highlights

  • Performance Optimization: Refactored get_func, get_variable, and value methods in src/context.rs to improve performance by avoiding redundant map lookups and unnecessary cloning of ContextValue enums.
  • Reduced Lock Contention: Modified the value method to explicitly drop the MutexGuard before invoking external functions, significantly reducing lock contention and preventing potential deadlocks.
  • Code Formatting: Applied cargo fmt to ensure consistent code style across relevant files.
  • Documentation: Added a new markdown document (.jules/bolt.md) detailing the learning and action points related to context map lookups and locking contention optimizations.
  • Benchmark Improvement: Achieved a ~4% improvement in the execute_expression benchmark execution time, dropping from ~5.60 µs to ~5.38 µs.
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
Copy Markdown

@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

The pull request primarily focuses on optimizing context map lookups and reducing lock contention within the Context struct. Specifically, the get_func, get_variable, and value methods in src/context.rs were refactored to acquire the mutex lock explicitly and perform single map lookups, eliminating redundant is_none() and unwrap() calls. Crucially, the value method was updated to ensure the MutexGuard is dropped before invoking external function closures, preventing potential deadlocks and improving concurrency. A new markdown file, .jules/bolt.md, was added to document these performance learnings and actions. Minor stylistic changes were also made to import statements in benches/display_expression.rs and formatting in src/parser.rs. A review comment suggests an if let pattern for improved readability and idiomatic Rust in the get_func method.

Comment on lines +34 to 39
let binding = self.0.lock().unwrap();
let value = binding.get(name)?;
match value {
ContextValue::Function(func) => Some(func.clone()),
ContextValue::Variable(_) => None,
}
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

For improved readability and to make the code more idiomatic, you could consider using if let to handle the Some case directly. This avoids the need for ? followed by a match and makes the intent clearer.

Suggested change
let binding = self.0.lock().unwrap();
let value = binding.get(name)?;
match value {
ContextValue::Function(func) => Some(func.clone()),
ContextValue::Variable(_) => None,
}
let binding = self.0.lock().unwrap();
if let Some(ContextValue::Function(func)) = binding.get(name) {
Some(func.clone())
} else {
None
}

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes Context lookups and reduces mutex lock contention in the expression engine by avoiding redundant HashMap::get calls, avoiding cloning full ContextValue enums when only the inner value is needed, and ensuring the context lock is not held while invoking stored functions.

Changes:

  • Refactored Context::get_func, Context::get_variable, and Context::value to do a single map lookup and clone only the needed inner value.
  • Updated Context::value to drop the MutexGuard before calling InnerFunction closures to reduce lock contention / reentrancy risk.
  • Applied cargo fmt formatting adjustments (parser display formatting and bench imports) and added a Jules learning note.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/context.rs Single-lookup accessors; releases mutex before invoking stored functions to reduce contention.
src/parser.rs Formatting-only changes in fmt::Display implementation.
benches/display_expression.rs Formatting-only import reordering.
.jules/bolt.md Documents the optimization lesson/action regarding map lookups and mutex guard scope.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.22%. Comparing base (5576973) to head (8aa3d3a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #42      +/-   ##
==========================================
+ Coverage   88.74%   89.22%   +0.47%     
==========================================
  Files          11       11              
  Lines        1066     1067       +1     
==========================================
+ Hits          946      952       +6     
+ Misses        120      115       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…failure

💡 What: Added a `tests` module directly inside `src/context.rs` to comprehensively test `get`, `get_func`, `get_variable`, and `value` methods against existent and non-existent keys.
🎯 Why: The previous PR decreased test coverage below the CI requirement because the optimized `Context` lookups contained match arms that were not executed during the existing test suite, causing Codecov to fail the CI build.
📊 Impact: Increases Codecov diff hit to 100%, resolving the CI failure.
🔬 Measurement: Verify the improvement by running `cargo test`.

Co-authored-by: ashyanSpada <22587148+ashyanSpada@users.noreply.github.com>
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.

2 participants