You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
⚡ Bolt: Optimize Context lookup and reduce Mutex lock contention
💡 What:
- Eliminated a double map lookup (`.is_none()` followed by `.unwrap()`) by using `.cloned()` inside `Context::value`.
- Modified `Context::value` so the lock is dropped *before* executing a user-provided closure.
- Streamlined `Context::get_func` and `Context::get_variable` to consume `ContextValue` directly instead of unnecessarily cloning inner properties of the already cloned value.
🎯 Why:
The `Context::value` method held the `Mutex` lock continuously while executing function closures. This severely reduced concurrency and was a major lock contention point since functions can be arbitrary long-running user code. Additionally, using double map lookups in high-frequency access patterns is wasteful.
📊 Impact:
- Improves expression execution time (benchmark improved from ~5.3µs to ~5.0µs)
- Halves map lookup cost for fetching context values.
- Substantially reduces the risk of long-held locks or deadlocks during closure execution.
🔬 Measurement:
Run `cargo bench execute_expression` to observe the improvement.
Co-authored-by: ashyanSpada <22587148+ashyanSpada@users.noreply.github.com>
**Learning:** Found an edge case where map access and function execution was holding a Mutex lock which leads to high lock contention, especially since Context instances store closures that might be arbitrarily expensive. Double map lookups in `.value()` (`is_none()` then `unwrap()`) combined with lock-holding was a bottleneck for expression execution.
3
+
**Action:** Always fetch the target value into an owned variable outside the lock block (using scope bounds or `cloned()`) in high-frequency map lookups. Never execute user closures inside a lock guard.
0 commit comments