Skip to content

⚡ Bolt: O(1) constant-time lookups for execution registry#180

Open
badMade wants to merge 3 commits into
mainfrom
bolt/execution-registry-lookup-optimization-11857276324186609613
Open

⚡ Bolt: O(1) constant-time lookups for execution registry#180
badMade wants to merge 3 commits into
mainfrom
bolt/execution-registry-lookup-optimization-11857276324186609613

Conversation

@badMade
Copy link
Copy Markdown
Owner

@badMade badMade commented May 13, 2026

💡 What: Replaced O(N) linear iteration over command and tool tuples with lazily-evaluated O(1) dictionary lookups (@functools.cached_property) in the ExecutionRegistry class. Preserved first-match-wins logic for duplicate keys by using reversed().
🎯 Why: Prevents repetitive string allocations and list traversals during frequent routing operations when searching for specific tools or commands by name.
📊 Measured Improvement: Reduces average benchmarked time for repeated lookups of unknown strings from ~3.69s down to ~0.06s (~98% speedup).


PR created automatically by Jules for task 11857276324186609613 started by @badMade

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 13, 2026 15:56
@google-labs-jules
Copy link
Copy Markdown

👋 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.

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

This pull request optimizes the ExecutionRegistry class by replacing O(N) linear list traversals with O(1) dictionary lookups using functools.cached_property. Additionally, it updates the project documentation to include this optimization pattern. The reviewer suggested wrapping the cached dictionaries in MappingProxyType to improve encapsulation and ensure immutability, which aligns with existing patterns in the codebase.

Comment thread src/execution_registry.py Outdated
Comment on lines +37 to +38
def _command_map(self) -> dict[str, MirroredCommand]:
return {cmd.name.lower(): cmd for cmd in reversed(self.commands)}
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 better encapsulation and to prevent accidental modification of the cached data, it's a good practice to return an immutable mapping. This aligns with the frozen=True nature of the ExecutionRegistry dataclass and is consistent with the pattern used in _get_command_lookup in src/commands.py.

You'll need to add the following imports at the top of the file:

from collections.abc import Mapping
from types import MappingProxyType
Suggested change
def _command_map(self) -> dict[str, MirroredCommand]:
return {cmd.name.lower(): cmd for cmd in reversed(self.commands)}
def _command_map(self) -> Mapping[str, MirroredCommand]:
return MappingProxyType({cmd.name.lower(): cmd for cmd in reversed(self.commands)})

Comment thread src/execution_registry.py Outdated
Comment on lines +41 to +42
def _tool_map(self) -> dict[str, MirroredTool]:
return {tool.name.lower(): tool for tool in reversed(self.tools)}
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

Similar to the _command_map, returning an immutable MappingProxyType here would make the implementation more robust and consistent with existing patterns in the codebase (e.g., _get_tool_lookup in src/tools.py).

This change requires the following imports:

from collections.abc import Mapping
from types import MappingProxyType
Suggested change
def _tool_map(self) -> dict[str, MirroredTool]:
return {tool.name.lower(): tool for tool in reversed(self.tools)}
def _tool_map(self) -> Mapping[str, MirroredTool]:
return MappingProxyType({tool.name.lower(): tool for tool in reversed(self.tools)})

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 ExecutionRegistry command/tool name lookups by replacing linear scans over tuples with cached, dictionary-backed maps to achieve O(1) average lookup time during routing.

Changes:

  • Added lazily computed @functools.cached_property maps for commands/tools to speed up name-based lookup.
  • Updated command() / tool() to use cached dict .get() lookups instead of iterating the tuples.
  • Added an optimization log entry in .jules/bolt.md describing the change and measured impact.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/execution_registry.py Introduces cached dict-backed lookup maps and switches registry lookups to O(1).
.jules/bolt.md Documents the optimization and its benchmark impact.
Comments suppressed due to low confidence (1)

src/execution_registry.py:42

  • _tool_map has the same mutability/immutability concern as _command_map: it returns a mutable dict cached on a frozen instance. Consider returning an immutable Mapping (e.g., MappingProxyType) and aligning duplicate-handling with the repo’s existing lookup builders (if key not in lookup:) for clarity.
    @functools.cached_property
    def _tool_map(self) -> dict[str, MirroredTool]:
        return {tool.name.lower(): tool for tool in reversed(self.tools)}

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

Comment thread src/execution_registry.py
Comment on lines +36 to +39
@functools.cached_property
def _command_map(self) -> dict[str, MirroredCommand]:
return {cmd.name.lower(): cmd for cmd in reversed(self.commands)}

Comment thread src/execution_registry.py Outdated
Comment on lines +38 to +42
return {cmd.name.lower(): cmd for cmd in reversed(self.commands)}

@functools.cached_property
def _tool_map(self) -> dict[str, MirroredTool]:
return {tool.name.lower(): tool for tool in reversed(self.tools)}
Comment thread .jules/bolt.md Outdated
## 2026-04-29 - Python Dictionary Lookup Optimization for Lists
**Learning:** O(N) linear scans on static lists inside frequently called functions (e.g., `get_tool`) are a performance hazard. A `lru_cache(maxsize=1)` dictionary lookup wrapped in `MappingProxyType` to prevent modification is a simple and extremely effective optimization technique that reduces complexity from O(N) to O(1) and eliminates repeated loop and string allocations. We must be mindful when mapping non-unique elements. Preserving first-match behavior requires `if key not in lookup:` during the dictionary's initial lazy-loading map phase.
**Action:** Always favor lazily initialized cached dictionary lookups over list iteration when retrieving elements from static datasets by string keys.
## 2024-05-13 - Python Lazy Map Lookups Optimization
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@badMade
Copy link
Copy Markdown
Owner Author

badMade commented May 15, 2026

@claude code review
@copilot code review

@badMade badMade marked this pull request as ready for review May 15, 2026 17:22
Copilot AI review requested due to automatic review settings May 15, 2026 17:22
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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/execution_registry.py Outdated
Comment on lines +36 to +38
@functools.cached_property
def _command_map(self) -> dict[str, MirroredCommand]:
return {cmd.name.lower(): cmd for cmd in reversed(self.commands)}
Agent-Logs-Url: https://github.com/badMade/claw-code/sessions/d3feae09-3114-4c34-870d-bdbcaee89121

Co-authored-by: badMade <106821302+badMade@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI commented May 15, 2026

@claude code review
@copilot code review

Addressed the requested review feedback in commit 1d05494: ExecutionRegistry cached maps are now immutable MappingProxyType mappings, added duplicate-name + case-insensitive lookup coverage, and corrected the optimization log date.

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.

3 participants