Skip to content

⚡ Optimize get_tool with O(1) cached lookup#66

Open
badMade wants to merge 3 commits into
mainfrom
perf-optimize-get-tool-16068724223843316924
Open

⚡ Optimize get_tool with O(1) cached lookup#66
badMade wants to merge 3 commits into
mainfrom
perf-optimize-get-tool-16068724223843316924

Conversation

@badMade
Copy link
Copy Markdown
Owner

@badMade badMade commented Apr 30, 2026

💡 What: Replaced the O(N) linear loop inside get_tool with an O(1) lazily initialized dictionary lookup (_get_tool_lookup). We use MappingProxyType and @lru_cache to ensure the lookup dict is immutable, cached, and guarantees first-match priority as was done originally.
🎯 Why: Looking up a tool by name inside get_tool is a high-frequency operation and linear scanning of all tools for every tool invocation wastes CPU time. A hashed lookup eliminates this overhead entirely.
📊 Measured Improvement:
We built a benchmark using timeit across 10,000 executions iterating through a mix of first, middle, last, and non-existent tools in the tools list:

  • Baseline: 0.5346 seconds (0.0535 ms per execution of all test cases)
  • Improvement: 0.0169 seconds (0.0017 ms per execution of all test cases)
  • Change over baseline: ~31x faster.

PR created automatically by Jules for task 16068724223843316924 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 April 30, 2026 01:28
@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.

@gemini-code-assist
Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

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 get_tool() in the Python porting workspace by replacing the per-call linear scan over PORTED_TOOLS with a lazily initialized, cached lookup map for O(1) access while preserving case-insensitive matching and first-match behavior.

Changes:

  • Added _get_tool_lookup() using @lru_cache(maxsize=1) and MappingProxyType to build an immutable, cached lowercased-name → PortingModule mapping.
  • Updated get_tool() to perform constant-time lookup via the cached mapping.
  • Applied consistent formatting/quoting updates in src/tools.py.

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

Comment thread src/tools.py Outdated
@badMade
Copy link
Copy Markdown
Owner Author

badMade commented Apr 30, 2026

@claude code review

@badMade
Copy link
Copy Markdown
Owner Author

badMade commented Apr 30, 2026

@codex code review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@badMade
Copy link
Copy Markdown
Owner Author

badMade commented Apr 30, 2026

@copilot, resolve the merge conflicts in this pull request.

1 similar comment
@badMade
Copy link
Copy Markdown
Owner Author

badMade commented Apr 30, 2026

@copilot, resolve the merge conflicts in this pull request.

@badMade badMade marked this pull request as ready for review April 30, 2026 01:50
@gemini-code-assist
Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Copilot AI and others added 2 commits April 30, 2026 02:01
… in _get_tool_lookup

Co-authored-by: badMade <106821302+badMade@users.noreply.github.com>
…l-16068724223843316924

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

Copilot AI commented Apr 30, 2026

@copilot, resolve the merge conflicts in this pull request.

Resolved. I also fixed the _get_tool_lookup return annotation (using Mapping[str, PortingModule] from collections.abc and lookup: dict[str, PortingModule] = {}) to match the pattern in src/commands.py as suggested by the code review. Commit: 7c1d823.

Copy link
Copy Markdown

Copilot AI commented Apr 30, 2026

@copilot, resolve the merge conflicts in this pull request.

The merge conflicts were already resolved in commit 7c1d823 — the branch is fully up to date with main. No further action needed.

@badMade
Copy link
Copy Markdown
Owner Author

badMade commented Apr 30, 2026

@gemini-code-assist, what are the steps and line numbers to make this fix?

@gemini-code-assist
Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@badMade
Copy link
Copy Markdown
Owner Author

badMade commented May 3, 2026

@gemini-code-assist, what are the steps and line numbers to make this fix?

@gemini-code-assist
Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

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