Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Dec 17, 2025

📄 114% (1.14x) speedup for _is_essential_tool in backend/python/app/modules/agents/qna/tool_registry.py

⏱️ Runtime : 1.49 milliseconds 698 microseconds (best of 171 runs)

📝 Explanation and details

The optimized code achieves a 113% speedup through two key micro-optimizations:

1. Tuple vs List Allocation

  • Changed ["calculator.", "web_search", "get_current_datetime"] to a tuple literal ("calculator.", "web_search", "get_current_datetime")
  • Eliminates repeated list allocation overhead on every function call (saves ~10ns per call based on profiler data)
  • Tuples are immutable and slightly more memory efficient for constant data

2. Early-Exit Loop vs Generator Expression

  • Replaced any(pattern in full_tool_name for pattern in essential_patterns) with an explicit for loop that returns True immediately upon finding a match
  • For small fixed sequences like this (3 patterns), explicit loops outperform generator expressions by avoiding generator overhead
  • The profiler shows the original any() line consumed 86.3% of total time, while the optimized loop distributes more efficiently across multiple lines

Performance Context
Based on function_references, this function is called from _should_include_tool() as a fallback when user preferences don't explicitly include a tool. While not in the hottest path, the function is called frequently enough (3,076 hits in profiler) that these micro-optimizations compound meaningfully.

Test Case Performance
The optimization shows consistent 60-190% speedups across all test scenarios, with particularly strong gains for:

  • Early matches (calculator tools): ~180% faster due to early exit
  • Non-matches: ~140% faster due to avoiding generator overhead
  • Large-scale tests: ~110% faster, demonstrating the optimization scales well

The changes preserve all behavior while making the function more efficient for this specific use case of checking a small, fixed set of patterns.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 3076 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from app.modules.agents.qna.tool_registry import _is_essential_tool

# unit tests

# ---------------------------
# BASIC TEST CASES
# ---------------------------


def test_calculator_tool_is_essential():
    # Should return True for a tool with the 'calculator.' prefix
    codeflash_output = _is_essential_tool(
        "calculator.add"
    )  # 2.09μs -> 746ns (181% faster)


def test_web_search_tool_is_essential():
    # Should return True for a tool containing 'web_search'
    codeflash_output = _is_essential_tool("web_search")  # 1.94μs -> 680ns (186% faster)
    codeflash_output = _is_essential_tool(
        "tools.web_search"
    )  # 840ns -> 428ns (96.3% faster)


def test_get_current_datetime_tool_is_essential():
    # Should return True for a tool containing 'get_current_datetime'
    codeflash_output = _is_essential_tool(
        "get_current_datetime"
    )  # 1.78μs -> 734ns (142% faster)
    codeflash_output = _is_essential_tool(
        "tools.get_current_datetime"
    )  # 932ns -> 510ns (82.7% faster)


def test_non_essential_tool_returns_false():
    # Should return False for a tool not matching any essential pattern
    codeflash_output = _is_essential_tool(
        "notebook.save"
    )  # 1.58μs -> 671ns (135% faster)
    codeflash_output = _is_essential_tool(
        "calendar.create_event"
    )  # 810ns -> 451ns (79.6% faster)


def test_partial_match_does_not_trigger():
    # Should not return True if only a substring matches, but not the full pattern
    codeflash_output = _is_essential_tool(
        "calculators.add"
    )  # 1.48μs -> 647ns (128% faster)
    codeflash_output = _is_essential_tool("websearch")  # 718ns -> 370ns (94.1% faster)
    codeflash_output = _is_essential_tool(
        "get_current_date"
    )  # 577ns -> 312ns (84.9% faster)


# ---------------------------
# EDGE TEST CASES
# ---------------------------


def test_empty_string():
    # Should return False for empty input
    codeflash_output = _is_essential_tool("")  # 1.32μs -> 506ns (162% faster)


def test_case_sensitivity():
    # Should be case-sensitive and not match different cases
    codeflash_output = _is_essential_tool(
        "Calculator.add"
    )  # 1.40μs -> 628ns (122% faster)
    codeflash_output = _is_essential_tool("WEB_SEARCH")  # 740ns -> 392ns (88.8% faster)
    codeflash_output = _is_essential_tool(
        "Get_Current_Datetime"
    )  # 670ns -> 399ns (67.9% faster)


def test_tool_name_is_pattern_prefix():
    # Should not match if tool name is a prefix of the pattern
    codeflash_output = _is_essential_tool("calculator")  # 1.33μs -> 527ns (152% faster)
    codeflash_output = _is_essential_tool("web_")  # 665ns -> 371ns (79.2% faster)
    codeflash_output = _is_essential_tool(
        "get_current_"
    )  # 574ns -> 321ns (78.8% faster)


def test_tool_name_contains_pattern_as_suffix():
    # Should match if the pattern occurs as a suffix
    codeflash_output = _is_essential_tool(
        "mytool_calculator."
    )  # 1.66μs -> 635ns (161% faster)
    codeflash_output = _is_essential_tool(
        "mytool_web_search"
    )  # 757ns -> 398ns (90.2% faster)
    codeflash_output = _is_essential_tool(
        "mytool_get_current_datetime"
    )  # 803ns -> 499ns (60.9% faster)


def test_tool_name_contains_pattern_in_middle():
    # Should match if the pattern occurs in the middle
    codeflash_output = _is_essential_tool(
        "foo.calculator.bar"
    )  # 1.40μs -> 505ns (177% faster)
    codeflash_output = _is_essential_tool(
        "foo.web_search.bar"
    )  # 816ns -> 435ns (87.6% faster)
    codeflash_output = _is_essential_tool(
        "foo.get_current_datetime.bar"
    )  # 765ns -> 471ns (62.4% faster)


def test_tool_name_with_similar_but_not_matching_patterns():
    # Should not match for similar but not matching patterns
    codeflash_output = _is_essential_tool("calculator")  # 1.44μs -> 623ns (131% faster)
    codeflash_output = _is_essential_tool(
        "websearcher"
    )  # 720ns -> 428ns (68.2% faster)
    codeflash_output = _is_essential_tool(
        "get_current_datetimex"
    )  # 883ns -> 464ns (90.3% faster)


def test_tool_name_with_multiple_patterns():
    # Should match if any pattern is present, even if multiple are present
    codeflash_output = _is_essential_tool(
        "calculator.web_search.get_current_datetime"
    )  # 1.34μs -> 535ns (151% faster)
    codeflash_output = _is_essential_tool(
        "foo_calculator.web_search"
    )  # 647ns -> 357ns (81.2% faster)


def test_tool_name_is_only_a_dot():
    # Should return False for a single dot
    codeflash_output = _is_essential_tool(".")  # 1.40μs -> 512ns (173% faster)


def test_tool_name_is_only_a_pattern():
    # Should return True if tool name is exactly the pattern
    codeflash_output = _is_essential_tool("web_search")  # 1.51μs -> 571ns (164% faster)
    codeflash_output = _is_essential_tool(
        "get_current_datetime"
    )  # 909ns -> 526ns (72.8% faster)


def test_tool_name_with_spaces():
    # Should not match if spaces are present in the pattern
    codeflash_output = _is_essential_tool(
        "calculator. add"
    )  # 1.35μs -> 464ns (192% faster)
    codeflash_output = _is_essential_tool("web_search ")  # 780ns -> 372ns (110% faster)
    codeflash_output = _is_essential_tool(
        " get_current_datetime"
    )  # 698ns -> 402ns (73.6% faster)


# ---------------------------
# LARGE SCALE TEST CASES
# ---------------------------


def test_large_number_of_non_essential_tools():
    # Should return False for all non-essential tool names in a large list
    non_essentials = [f"tool_{i}" for i in range(1000)]
    for name in non_essentials:
        codeflash_output = _is_essential_tool(name)  # 434μs -> 207μs (110% faster)


def test_large_number_of_essential_tools():
    # Should return True for all essential tool names in a large list
    essentials = (
        [f"calculator.{i}" for i in range(500)]
        + [f"web_search_{i}" for i in range(250)]
        + [f"get_current_datetime_{i}" for i in range(250)]
    )
    for name in essentials:
        # Only those with 'calculator.' or 'web_search' or 'get_current_datetime' as substring should match
        codeflash_output = _is_essential_tool(name)  # 473μs -> 213μs (122% faster)


def test_mixed_large_list():
    # Mix essential and non-essential tool names, check correct detection
    names = []
    expected = []
    for i in range(250):
        names.append(f"calculator.{i}")
        expected.append(True)
        names.append(f"web_search_{i}")
        expected.append(True)
        names.append(f"get_current_datetime_{i}")
        expected.append(True)
        names.append(f"notebook.save_{i}")
        expected.append(False)
    for name, exp in zip(names, expected):
        codeflash_output = _is_essential_tool(name)  # 494μs -> 237μs (109% faster)


def test_performance_with_long_strings():
    # Test with very long tool names, only one contains the essential pattern
    long_prefix = "x" * 500
    long_suffix = "y" * 500
    essential_name = long_prefix + "calculator." + long_suffix
    non_essential_name = long_prefix + "calculators" + long_suffix
    codeflash_output = _is_essential_tool(
        essential_name
    )  # 1.83μs -> 715ns (155% faster)
    codeflash_output = _is_essential_tool(
        non_essential_name
    )  # 1.21μs -> 825ns (46.7% faster)


# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
from app.modules.agents.qna.tool_registry import _is_essential_tool

# unit tests

# -----------------------
# Basic Test Cases
# -----------------------


def test_exact_calculator_dot():
    # Should match if 'calculator.' is at the start
    codeflash_output = _is_essential_tool(
        "calculator.add"
    )  # 1.48μs -> 526ns (180% faster)


def test_calculator_dot_in_middle():
    # Should match if 'calculator.' is in the middle
    codeflash_output = _is_essential_tool(
        "foo.calculator.bar"
    )  # 1.49μs -> 591ns (151% faster)


def test_exact_web_search():
    # Should match exact 'web_search'
    codeflash_output = _is_essential_tool("web_search")  # 1.56μs -> 573ns (173% faster)


def test_web_search_in_longer_name():
    # Should match if 'web_search' is a substring
    codeflash_output = _is_essential_tool(
        "tools.web_search.v2"
    )  # 1.57μs -> 679ns (131% faster)


def test_exact_get_current_datetime():
    # Should match exact 'get_current_datetime'
    codeflash_output = _is_essential_tool(
        "get_current_datetime"
    )  # 1.65μs -> 745ns (121% faster)


def test_get_current_datetime_suffix():
    # Should match if 'get_current_datetime' is a substring
    codeflash_output = _is_essential_tool(
        "foo.get_current_datetime.bar"
    )  # 1.77μs -> 865ns (104% faster)


def test_non_essential_tool():
    # Should not match if not in essential patterns
    codeflash_output = _is_essential_tool("foo.bar")  # 1.44μs -> 597ns (141% faster)


def test_similar_but_not_essential():
    # Should not match if similar but not exact pattern
    codeflash_output = _is_essential_tool("calculator")  # 1.44μs -> 639ns (125% faster)
    codeflash_output = _is_essential_tool("websearch")  # 685ns -> 349ns (96.3% faster)
    codeflash_output = _is_essential_tool(
        "get_currentdate_time"
    )  # 701ns -> 425ns (64.9% faster)


# -----------------------
# Edge Test Cases
# -----------------------


def test_empty_string():
    # Empty string should not match anything
    codeflash_output = _is_essential_tool("")  # 1.33μs -> 504ns (163% faster)


def test_case_sensitivity():
    # Should be case sensitive
    codeflash_output = _is_essential_tool(
        "Calculator.add"
    )  # 1.40μs -> 633ns (122% faster)
    codeflash_output = _is_essential_tool("WEB_SEARCH")  # 790ns -> 389ns (103% faster)
    codeflash_output = _is_essential_tool(
        "GET_CURRENT_DATETIME"
    )  # 630ns -> 375ns (68.0% faster)


def test_substring_overlap():
    # Should not match if only part of a pattern is present
    codeflash_output = _is_essential_tool(
        "calc.ulator.add"
    )  # 1.42μs -> 640ns (122% faster)
    codeflash_output = _is_essential_tool(
        "web_searching"
    )  # 1.09μs -> 550ns (97.5% faster)
    codeflash_output = _is_essential_tool(
        "get_current_datetimez"
    )  # 708ns -> 404ns (75.2% faster)


def test_pattern_at_end():
    # Should match if pattern is at the end
    codeflash_output = _is_essential_tool(
        "foo.bar.calculator."
    )  # 1.42μs -> 544ns (160% faster)
    codeflash_output = _is_essential_tool(
        "foo.bar.web_search"
    )  # 815ns -> 425ns (91.8% faster)
    codeflash_output = _is_essential_tool(
        "foo.bar.get_current_datetime"
    )  # 756ns -> 437ns (73.0% faster)


def test_pattern_with_extra_spaces():
    # Spaces should matter
    codeflash_output = _is_essential_tool(
        " calculator.add"
    )  # 1.36μs -> 519ns (162% faster)
    codeflash_output = _is_essential_tool(
        "web_search "
    )  # 799ns -> 420ns (90.2% faster)
    codeflash_output = _is_essential_tool(
        "get_current_datetime "
    )  # 720ns -> 389ns (85.1% faster)


def test_pattern_with_newlines():
    # Newlines should matter
    codeflash_output = _is_essential_tool(
        "calculator.\nadd"
    )  # 1.33μs -> 518ns (157% faster)
    codeflash_output = _is_essential_tool(
        "web_search\n"
    )  # 740ns -> 382ns (93.7% faster)
    codeflash_output = _is_essential_tool(
        "\nget_current_datetime"
    )  # 702ns -> 410ns (71.2% faster)


def test_pattern_with_special_characters():
    # Special characters should not affect matching unless they break the pattern
    codeflash_output = _is_essential_tool(
        "foo.calculator.$add"
    )  # 1.31μs -> 519ns (153% faster)
    codeflash_output = _is_essential_tool(
        "foo.web_search@"
    )  # 813ns -> 402ns (102% faster)
    codeflash_output = _is_essential_tool(
        "foo.get_current_datetime#"
    )  # 756ns -> 454ns (66.5% faster)


def test_pattern_with_unicode():
    # Unicode should not affect matching unless it breaks the pattern
    codeflash_output = _is_essential_tool(
        "foo.calculator.计算"
    )  # 1.80μs -> 935ns (92.4% faster)
    codeflash_output = _is_essential_tool(
        "foo.web_search搜索"
    )  # 972ns -> 583ns (66.7% faster)
    codeflash_output = _is_essential_tool(
        "foo.get_current_datetime时间"
    )  # 891ns -> 611ns (45.8% faster)


# -----------------------
# Large Scale Test Cases
# -----------------------


def test_pattern_is_substring_of_tool():
    # Should not match if pattern is only a substring of a word
    codeflash_output = _is_essential_tool(
        "mycalculator.add"
    )  # 2.33μs -> 905ns (157% faster)
    codeflash_output = _is_essential_tool(
        "myweb_searching"
    )  # 902ns -> 426ns (112% faster)
    codeflash_output = _is_essential_tool(
        "get_current_datetimez"
    )  # 704ns -> 410ns (71.7% faster)


def test_tool_name_is_pattern_prefix():
    # Should not match if tool name is a prefix of pattern
    codeflash_output = _is_essential_tool("calculator")  # 1.58μs -> 639ns (148% faster)
    codeflash_output = _is_essential_tool(
        "web_searcher"
    )  # 1.06μs -> 464ns (128% faster)
    codeflash_output = _is_essential_tool(
        "get_current_datetimezone"
    )  # 688ns -> 391ns (76.0% faster)


def test_tool_name_with_multiple_patterns():
    # Should match if any pattern is present
    codeflash_output = _is_essential_tool(
        "foo.calculator.bar.web_search.get_current_datetime"
    )  # 1.43μs -> 594ns (141% faster)


def test_tool_name_with_no_patterns():
    # Should not match if no patterns are present
    codeflash_output = _is_essential_tool(
        "foo.bar.baz"
    )  # 1.48μs -> 658ns (124% faster)


# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-_is_essential_tool-mja4zi8p and push.

Codeflash Static Badge

The optimized code achieves a **113% speedup** through two key micro-optimizations:

**1. Tuple vs List Allocation**
- Changed `["calculator.", "web_search", "get_current_datetime"]` to a tuple literal `("calculator.", "web_search", "get_current_datetime")`
- Eliminates repeated list allocation overhead on every function call (saves ~10ns per call based on profiler data)
- Tuples are immutable and slightly more memory efficient for constant data

**2. Early-Exit Loop vs Generator Expression**
- Replaced `any(pattern in full_tool_name for pattern in essential_patterns)` with an explicit `for` loop that returns `True` immediately upon finding a match
- For small fixed sequences like this (3 patterns), explicit loops outperform generator expressions by avoiding generator overhead
- The profiler shows the original `any()` line consumed 86.3% of total time, while the optimized loop distributes more efficiently across multiple lines

**Performance Context**
Based on `function_references`, this function is called from `_should_include_tool()` as a fallback when user preferences don't explicitly include a tool. While not in the hottest path, the function is called frequently enough (3,076 hits in profiler) that these micro-optimizations compound meaningfully.

**Test Case Performance**
The optimization shows consistent 60-190% speedups across all test scenarios, with particularly strong gains for:
- Early matches (calculator tools): ~180% faster due to early exit
- Non-matches: ~140% faster due to avoiding generator overhead
- Large-scale tests: ~110% faster, demonstrating the optimization scales well

The changes preserve all behavior while making the function more efficient for this specific use case of checking a small, fixed set of patterns.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 December 17, 2025 14:58
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant