⚡️ Speed up function _get_mode_intro_message by 32%
#615
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 32% (0.32x) speedup for
_get_mode_intro_messageinmarimo/_server/ai/prompts.py⏱️ Runtime :
735 microseconds→558 microseconds(best of126runs)📝 Explanation and details
The optimization achieves a 31% speedup by eliminating redundant string construction and concatenation operations on each function call.
Key Changes:
_MANUAL_MODE_INTROand_ASK_MODE_INTROinstead of constructing them dynamicallybase_introvariable creation, f-string formatting (f"{base_intro}"), and multi-line string concatenation that occurred on every function callWhy This Speeds Up Performance:
The original code performed expensive string operations every time the function was called:
base_introstring (806ns + 719ns per call from profiler)base_introwith additional content (480ns + 493ns per call)The optimized version simply returns pre-built string constants, requiring only a conditional check and direct return - no string construction overhead.
Performance Impact:
Test Case Performance:
The optimization is particularly effective for:
This optimization trades a small amount of module initialization time and memory for significantly faster runtime performance, making it ideal for functions called frequently during application execution.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
from typing import Any, Literal
imports
import pytest
from marimo._server.ai.prompts import _get_mode_intro_message
Simulate CopilotMode typing for tests
CopilotMode = Literal["manual", "ask"]
from marimo._server.ai.prompts import _get_mode_intro_message
unit tests
------------------------
Basic Test Cases
------------------------
def test_manual_mode_message_basic():
"""Test that 'manual' mode returns the expected message."""
codeflash_output = _get_mode_intro_message("manual"); result = codeflash_output # 534ns -> 311ns (71.7% faster)
def test_ask_mode_message_basic():
"""Test that 'ask' mode returns the expected message."""
codeflash_output = _get_mode_intro_message("ask"); result = codeflash_output # 559ns -> 342ns (63.5% faster)
def test_manual_and_ask_modes_are_distinct():
"""Test that manual and ask modes produce different outputs."""
codeflash_output = _get_mode_intro_message("manual"); manual_msg = codeflash_output # 550ns -> 333ns (65.2% faster)
codeflash_output = _get_mode_intro_message("ask"); ask_msg = codeflash_output # 301ns -> 190ns (58.4% faster)
------------------------
Edge Test Cases
------------------------
def test_large_batch_of_manual_modes():
"""Test that multiple calls with 'manual' mode are consistent and efficient."""
codeflash_output = _get_mode_intro_message("manual"); expected = codeflash_output # 775ns -> 442ns (75.3% faster)
for _ in range(500): # reasonable batch size
codeflash_output = _get_mode_intro_message("manual") # 81.7μs -> 62.7μs (30.2% faster)
def test_large_batch_of_ask_modes():
"""Test that multiple calls with 'ask' mode are consistent and efficient."""
codeflash_output = _get_mode_intro_message("ask"); expected = codeflash_output # 629ns -> 383ns (64.2% faster)
for _ in range(500):
codeflash_output = _get_mode_intro_message("ask") # 88.2μs -> 67.7μs (30.3% faster)
def test_stress_with_alternating_modes():
"""Test alternating valid modes for consistency."""
codeflash_output = _get_mode_intro_message("manual"); manual_msg = codeflash_output # 704ns -> 439ns (60.4% faster)
codeflash_output = _get_mode_intro_message("ask"); ask_msg = codeflash_output # 364ns -> 228ns (59.6% faster)
for i in range(1000):
if i % 2 == 0:
codeflash_output = _get_mode_intro_message("manual")
else:
codeflash_output = _get_mode_intro_message("ask")
------------------------
Additional Robustness Tests
------------------------
def test_message_formatting_is_consistent():
"""Test that the message always starts with the base intro."""
for mode in ["manual", "ask"]:
codeflash_output = _get_mode_intro_message(mode); msg = codeflash_output # 1.01μs -> 526ns (92.0% faster)
def test_message_contains_sections():
"""Test that both modes contain 'Capabilities' and 'Limitations' sections."""
for mode in ["manual", "ask"]:
codeflash_output = _get_mode_intro_message(mode); msg = codeflash_output # 816ns -> 502ns (62.5% faster)
#------------------------------------------------
from typing import Literal
imports
import pytest # used for our unit tests
from marimo._server.ai.prompts import _get_mode_intro_message
Simulate the CopilotMode type used in the function
CopilotMode = Literal["manual", "ask"]
from marimo._server.ai.prompts import _get_mode_intro_message
unit tests
---- Basic Test Cases ----
def test_manual_mode_returns_expected_message():
# Test that "manual" mode returns the correct message
expected = (
"You are Marimo Copilot, an AI assistant integrated into the marimo notebook code editor.\n"
"Your primary function is to help users create, analyze, and improve data science notebooks using marimo's reactive programming model.\n"
"## Capabilities\n"
"- Answer questions and provide guidance using only your internal knowledge and the notebook context provided by the user.\n"
"\n"
"## Limitations\n"
"- You do NOT have access to any external tools, plugins, or APIs.\n"
"- You may not perform any actions beyond generating text and code suggestions.\n"
)
codeflash_output = _get_mode_intro_message("manual"); result = codeflash_output # 712ns -> 406ns (75.4% faster)
def test_ask_mode_returns_expected_message():
# Test that "ask" mode returns the correct message
expected = (
"You are Marimo Copilot, an AI assistant integrated into the marimo notebook code editor.\n"
"Your primary function is to help users create, analyze, and improve data science notebooks using marimo's reactive programming model.\n"
"## Capabilities\n"
"- You can use a set of read-only tools to gather additional context from the notebook or environment (e.g., searching code, summarizing data, or reading documentation).\n"
"- You may use these tools ONLY to gather information, not to modify code or state.\n"
"\n"
"## Limitations\n"
"- All tool use is strictly read-only. You may not perform write, edit, or execution actions.\n"
"- You must always explain to the user why you are using a tool before invoking it.\n"
)
codeflash_output = _get_mode_intro_message("ask"); result = codeflash_output # 652ns -> 375ns (73.9% faster)
def test_manual_and_ask_modes_are_distinct():
# Ensure the outputs for "manual" and "ask" are different
codeflash_output = _get_mode_intro_message("manual"); manual_msg = codeflash_output # 591ns -> 372ns (58.9% faster)
codeflash_output = _get_mode_intro_message("ask"); ask_msg = codeflash_output # 290ns -> 204ns (42.2% faster)
---- Edge Test Cases ----
def test_message_starts_with_base_intro():
# Ensure both modes start with the base_intro string
base_intro = (
"You are Marimo Copilot, an AI assistant integrated into the marimo notebook code editor.\n"
"Your primary function is to help users create, analyze, and improve data science notebooks using marimo's reactive programming model.\n"
)
def test_manual_mode_contains_expected_keywords():
# Check that specific keywords are present in manual mode
codeflash_output = _get_mode_intro_message("manual"); msg = codeflash_output # 697ns -> 421ns (65.6% faster)
def test_ask_mode_contains_expected_keywords():
# Check that specific keywords are present in ask mode
codeflash_output = _get_mode_intro_message("ask"); msg = codeflash_output # 635ns -> 361ns (75.9% faster)
---- Large Scale Test Cases ----
def test_large_batch_of_manual_modes():
# Test calling the function with "manual" mode 1000 times
codeflash_output = _get_mode_intro_message("manual"); expected = codeflash_output # 615ns -> 308ns (99.7% faster)
for _ in range(1000):
codeflash_output = _get_mode_intro_message("manual") # 163μs -> 123μs (32.2% faster)
def test_large_batch_of_ask_modes():
# Test calling the function with "ask" mode 1000 times
codeflash_output = _get_mode_intro_message("ask"); expected = codeflash_output # 632ns -> 383ns (65.0% faster)
for _ in range(1000):
codeflash_output = _get_mode_intro_message("ask") # 177μs -> 134μs (31.7% faster)
def test_all_modes_in_list():
# Test the function over a mixed list of valid and invalid modes
modes = ["manual", "ask", "Manual", "ASK", "", None, 123, "invalid"]
for mode in modes:
if mode in ("manual", "ask"):
# Should not raise
codeflash_output = _get_mode_intro_message(mode); msg = codeflash_output
else:
with pytest.raises(Exception):
_get_mode_intro_message(mode) # type: ignore
def test_message_length_consistency():
# Ensure that the message length for manual and ask is consistent across calls
manual_len = len(_get_mode_intro_message("manual")) # 714ns -> 407ns (75.4% faster)
ask_len = len(_get_mode_intro_message("ask")) # 366ns -> 222ns (64.9% faster)
for _ in range(100):
codeflash_output = len(_get_mode_intro_message("manual")) # 17.5μs -> 13.0μs (35.1% faster)
codeflash_output = len(_get_mode_intro_message("ask"))
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-_get_mode_intro_message-mhvjmtanand push.