⚡️ Speed up function overloads_from_env by 10%
#619
+29
−13
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.
📄 10% (0.10x) speedup for
overloads_from_envinmarimo/_ast/app_config.py⏱️ Runtime :
837 microseconds→758 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 10% speedup through several key micro-optimizations:
1. Faster attribute checking in
from_untrusted_dict:hasattr(config, key)withkey in _AppConfig.__dataclass_fields__- dictionary lookup is significantly faster than attribute introspectionupdates.items()to avoid repeatedupdates[key]lookups during iterationsetattr()instead ofconfig.__setattr__()for cleaner, marginally faster attribute setting2. Reduced overhead in
overloads_from_env:[key for key in os.environ if key.startswith(prefix)], avoiding repeated prefix checkslen(prefix)aslpto eliminate repeated function callsos.environ.__getitem__for direct dictionary accessvalue.lower()once aslc_valueinstead of calling it multiple times for boolean checksos.environ[key]lookups by reusing the already-fetchedvaluePerformance characteristics from tests:
The optimizations maintain identical behavior while reducing computational overhead, especially beneficial for applications that frequently process environment-based configuration.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
import os
from dataclasses import dataclass, field
from typing import Any, Optional
imports
import pytest
from marimo._ast.app_config import overloads_from_env
unit tests
@pytest.mark.usefixtures("monkeypatch")
class TestOverloadsFromEnv:
# ========== BASIC TEST CASES ==========
codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import os
from dataclasses import dataclass, field
from typing import Any, Optional
imports
import pytest
from marimo._ast.app_config import overloads_from_env
--- Basic Test Cases ---
def test_no_env_vars_returns_default():
"""No environment variables: should return default config."""
codeflash_output = overloads_from_env(); config = codeflash_output # 9.24μs -> 10.4μs (11.5% slower)
def test_simple_string_override(monkeypatch):
"""Set a string environment variable and check override."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_APP_TITLE", "My App")
codeflash_output = overloads_from_env(); config = codeflash_output # 12.4μs -> 12.5μs (0.858% slower)
def test_boolean_true_override(monkeypatch):
"""Set a boolean 'True' environment variable and check override."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_LAYOUT_FILE", "True")
codeflash_output = overloads_from_env(); config = codeflash_output # 11.5μs -> 12.3μs (6.69% slower)
def test_boolean_false_override(monkeypatch):
"""Set a boolean 'False' environment variable and check override."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_CSS_FILE", "false")
codeflash_output = overloads_from_env(); config = codeflash_output # 11.4μs -> 12.1μs (6.12% slower)
def test_list_override(monkeypatch):
"""Set a list environment variable and check override."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", "[html,markdown]")
codeflash_output = overloads_from_env(); config = codeflash_output # 12.6μs -> 12.8μs (1.38% slower)
def test_multiple_overrides(monkeypatch):
"""Set multiple environment variables and check all are overridden."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_APP_TITLE", "Multi")
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_WIDTH", "wide")
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_SQL_OUTPUT", "table")
codeflash_output = overloads_from_env(); config = codeflash_output # 15.4μs -> 14.8μs (4.01% faster)
--- Edge Test Cases ---
def test_empty_list(monkeypatch):
"""Set a list variable to empty list."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", "[]")
codeflash_output = overloads_from_env(); config = codeflash_output # 12.4μs -> 12.4μs (0.064% faster)
def test_list_with_spaces(monkeypatch):
"""List with spaces should not strip spaces."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", "[ html, markdown ]")
codeflash_output = overloads_from_env(); config = codeflash_output # 12.6μs -> 12.7μs (0.593% slower)
def test_list_with_one_element(monkeypatch):
"""List with one element should be parsed as list of one string."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", "[pdf]")
codeflash_output = overloads_from_env(); config = codeflash_output # 12.4μs -> 12.3μs (0.713% faster)
def test_list_with_trailing_comma(monkeypatch):
"""List with trailing comma should produce an empty string as last element."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", "[html,markdown,]")
codeflash_output = overloads_from_env(); config = codeflash_output # 12.4μs -> 12.7μs (2.32% slower)
def test_nonexistent_key(monkeypatch):
"""Set an env var that doesn't map to any config key (should be ignored)."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_NOT_A_CONFIG", "value")
codeflash_output = overloads_from_env(); config = codeflash_output # 11.6μs -> 11.6μs (0.268% faster)
def test_case_insensitivity(monkeypatch):
"""Boolean values should be case-insensitive."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_CSS_FILE", "TrUe")
codeflash_output = overloads_from_env(); config = codeflash_output # 11.3μs -> 11.9μs (4.79% slower)
def test_list_with_internal_commas(monkeypatch):
"""List with internal commas (should not split inside elements)."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", "[html,mark,down]")
codeflash_output = overloads_from_env(); config = codeflash_output # 12.6μs -> 12.4μs (1.39% faster)
def test_list_with_empty_elements(monkeypatch):
"""List with empty elements (e.g., [,,]) should produce empty strings."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", "[,,]")
codeflash_output = overloads_from_env(); config = codeflash_output # 12.6μs -> 12.4μs (1.70% faster)
def test_value_with_square_brackets_but_no_commas(monkeypatch):
"""List with brackets but no commas should produce a single-element list."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", "[single]")
codeflash_output = overloads_from_env(); config = codeflash_output # 12.3μs -> 12.5μs (1.00% slower)
def test_value_with_unmatched_brackets(monkeypatch):
"""If value starts with [ but doesn't end with ], treat as string."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_APP_TITLE", "[unmatched")
codeflash_output = overloads_from_env(); config = codeflash_output # 11.9μs -> 12.2μs (2.18% slower)
def test_value_with_extra_spaces(monkeypatch):
"""String value with leading/trailing spaces is preserved."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_APP_TITLE", " my app ")
codeflash_output = overloads_from_env(); config = codeflash_output # 11.7μs -> 12.0μs (2.33% slower)
def test_value_with_empty_string(monkeypatch):
"""Empty string value is preserved."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_APP_TITLE", "")
codeflash_output = overloads_from_env(); config = codeflash_output # 11.7μs -> 11.7μs (0.249% faster)
--- Large Scale Test Cases ---
def test_large_number_of_env_vars(monkeypatch):
"""Test function with many environment variables (up to 1000)."""
for i in range(100):
monkeypatch.setenv(f"MARIMO_APP_OVERLOAD_APP_TITLE{i}", f"title_{i}")
# Only keys matching _AppConfig fields are used, so all should be ignored
codeflash_output = overloads_from_env(); config = codeflash_output # 122μs -> 83.4μs (46.9% faster)
def test_large_list(monkeypatch):
"""Test function with a large list in the environment variable."""
large_list = ",".join(str(i) for i in range(500))
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", f"[{large_list}]")
codeflash_output = overloads_from_env(); config = codeflash_output # 20.6μs -> 19.6μs (5.09% faster)
def test_large_string_value(monkeypatch):
"""Test function with a very large string value."""
bigstr = "x" * 1000
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_APP_TITLE", bigstr)
codeflash_output = overloads_from_env(); config = codeflash_output # 13.3μs -> 13.1μs (1.83% faster)
def test_many_valid_overrides(monkeypatch):
"""Set all fields to non-defaults at once."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_WIDTH", "full")
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_APP_TITLE", "Big App")
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_LAYOUT_FILE", "layout.json")
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_CSS_FILE", "css.css")
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_HTML_HEAD_FILE", "head.html")
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_AUTO_DOWNLOAD", "[html,md,pdf]")
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_SQL_OUTPUT", "csv")
codeflash_output = overloads_from_env(); config = codeflash_output # 22.4μs -> 20.2μs (11.1% faster)
def test_env_var_with_non_ascii(monkeypatch):
"""Test non-ASCII characters in env var value."""
monkeypatch.setenv("_MARIMO_APP_OVERLOAD_APP_TITLE", "Mårïmø 🚀")
codeflash_output = overloads_from_env(); config = codeflash_output # 13.7μs -> 13.4μs (2.96% faster)
codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from marimo._ast.app_config import overloads_from_env
def test_overloads_from_env():
overloads_from_env()
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_bps3n5s8/tmp6garfwr7/test_concolic_coverage.py::test_overloads_from_envTo edit these changes
git checkout codeflash/optimize-overloads_from_env-mhvkvptuand push.