Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 152% (1.52x) speedup for KeyData.is_expired in backend/python/app/config/providers/in_memory_store.py

⏱️ Runtime : 5.08 milliseconds 2.01 milliseconds (best of 5 runs)

📝 Explanation and details

The optimized code achieves a 152% speedup by implementing conditional logging guards that eliminate expensive debug operations when logging is disabled.

Key Optimization:

  • Added logger.isEnabledFor(10) checks before debug logging calls in the is_expired() method
  • Eliminated expensive string formatting when debug logging is disabled

Why This Works:
The original code unconditionally executed logger.debug() calls with expensive string formatting operations (time.strftime() and string interpolation), even when debug logging was disabled. The profiler shows these operations consumed 45.6% of total runtime (25.2% + 21.5% + 3.5% from logger.debug lines).

The optimization wraps debug statements with logger.isEnabledFor(10) guards (DEBUG level = 10), which is a lightweight check that prevents:

  • String interpolation operations
  • time.strftime() calls (1279.6 nanoseconds per hit)
  • time.localtime() conversions
  • Multiple function call overhead

Performance Impact by Test Case:

  • No TTL cases: 6-30% improvement (minimal logging overhead)
  • Unexpired TTL cases: 78-180% improvement (avoids expensive time formatting)
  • Expired TTL cases: 202-260% improvement (eliminates both logging paths)
  • Large-scale tests: 181-208% improvement (multiplicative effect across many calls)

Production Benefit:
This optimization is particularly effective in production environments where debug logging is typically disabled, making the is_expired() method significantly faster for cache hit/miss decisions and TTL validations without changing any functional behavior.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 13085 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import time

# imports
import pytest  # used for our unit tests
from app.config.providers.in_memory_store import KeyData

# unit tests

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


def test_no_ttl_never_expires():
    """Test that a KeyData with no TTL never expires."""
    kd = KeyData("test_value")
    codeflash_output = kd.is_expired()  # 1.04μs -> 978ns (6.85% faster)


def test_positive_ttl_not_expired_immediately():
    """Test that a KeyData with a positive TTL is not expired right after creation."""
    kd = KeyData("test_value", ttl=5)
    codeflash_output = kd.is_expired()  # 2.98μs -> 1.23μs (143% faster)


def test_positive_ttl_expires_after_wait():
    """Test that a KeyData with a positive TTL expires after waiting for TTL seconds."""
    kd = KeyData("test_value", ttl=1)
    time.sleep(1.1)  # Wait just over the TTL
    codeflash_output = kd.is_expired()  # 27.0μs -> 8.05μs (235% faster)


def test_zero_ttl_expires_immediately():
    """Test that a KeyData with zero TTL is expired immediately."""
    kd = KeyData("test_value", ttl=0)
    codeflash_output = kd.is_expired()  # 1.05μs -> 1.09μs (4.30% slower)


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


def test_negative_ttl_expires_immediately():
    """Test that a KeyData with negative TTL is expired immediately."""
    kd = KeyData("test_value", ttl=-10)
    codeflash_output = kd.is_expired()  # 3.27μs -> 1.14μs (186% faster)


def test_float_ttl():
    """Test that a KeyData with a float TTL expires correctly."""
    kd = KeyData("test_value", ttl=0.5)
    codeflash_output = kd.is_expired()  # 2.93μs -> 1.11μs (163% faster)
    time.sleep(0.6)
    codeflash_output = kd.is_expired()  # 26.6μs -> 8.15μs (227% faster)


def test_large_ttl_not_expired():
    """Test that a KeyData with a very large TTL is not expired."""
    kd = KeyData("test_value", ttl=1e6)
    codeflash_output = kd.is_expired()  # 2.97μs -> 1.66μs (78.9% faster)


def test_none_ttl_equivalent_to_no_expiry():
    """Test that a KeyData with ttl=None is equivalent to no expiry."""
    kd = KeyData("test_value", ttl=None)
    codeflash_output = kd.is_expired()  # 897ns -> 748ns (19.9% faster)


def test_ttl_as_string_raises_typeerror():
    """Test that passing a string as TTL raises a TypeError."""
    with pytest.raises(TypeError):
        KeyData("test_value", ttl="100")  # Should not accept string


def test_ttl_as_list_raises_typeerror():
    """Test that passing a list as TTL raises a TypeError."""
    with pytest.raises(TypeError):
        KeyData("test_value", ttl=[1, 2, 3])  # Should not accept list


def test_expiry_field_is_none_when_no_ttl():
    """Test that expiry is None when no TTL is given."""
    kd = KeyData("test_value")


def test_expiry_field_is_float_when_ttl_given():
    """Test that expiry is a float when TTL is given."""
    kd = KeyData("test_value", ttl=2)


def test_expiry_field_is_float_when_float_ttl_given():
    """Test that expiry is a float when TTL is a float."""
    kd = KeyData("test_value", ttl=2.5)


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


def test_many_keydata_instances_varying_ttl():
    """Test the function's scalability with many KeyData instances and varying TTLs."""
    instances = []
    for i in range(1000):
        ttl = i % 5  # Vary TTL between 0 and 4
        instances.append(KeyData(f"value_{i}", ttl=ttl))
    # After creation, only those with ttl=0 should be expired
    expired_count = sum(kd.is_expired() for kd in instances)


def test_many_keydata_instances_expiry_after_wait():
    """Test that all KeyData instances expire after waiting past their TTL."""
    instances = []
    for i in range(1000):
        ttl = 0.1 + (i % 3) * 0.1  # TTLs: 0.1, 0.2, 0.3
        instances.append(KeyData(f"value_{i}", ttl=ttl))
    time.sleep(0.35)  # Wait longer than the max TTL


def test_mixed_ttl_and_no_ttl_instances():
    """Test a mix of KeyData with and without TTLs."""
    instances = [KeyData("no_ttl") for _ in range(500)] + [
        KeyData("ttl", ttl=0.1) for _ in range(500)
    ]
    time.sleep(0.15)  # Wait for TTL to expire
    # First 500 should never expire, last 500 should be expired
    for i, kd in enumerate(instances):
        if i < 500:
            codeflash_output = kd.is_expired()
        else:
            codeflash_output = kd.is_expired()


def test_performance_large_batch_creation_and_expiry():
    """Test performance and correctness of batch creation and expiry check."""
    start = time.time()
    instances = [KeyData(i, ttl=0.05) for i in range(1000)]
    time.sleep(0.06)
    expired = [kd.is_expired() for kd in instances]
    duration = time.time() - start


# ---------------- MUTATION-FLAGGING TESTS ----------------


def test_is_expired_returns_boolean():
    """Test that is_expired always returns a boolean."""
    kd = KeyData("test_value")
    kd2 = KeyData("test_value", ttl=1)


def test_expiry_boundary_exact():
    """Test expiry at the exact boundary."""
    kd = KeyData("test_value", ttl=0.2)
    # Wait just less than TTL
    time.sleep(0.19)
    codeflash_output = kd.is_expired()  # 25.1μs -> 7.77μs (223% faster)
    # Wait a tiny bit more to cross boundary
    time.sleep(0.02)
    codeflash_output = kd.is_expired()  # 13.5μs -> 5.01μs (169% faster)


def test_ttl_as_boolean_false():
    """Test that passing ttl=False results in no expiry."""
    kd = KeyData("test_value", ttl=False)
    codeflash_output = kd.is_expired()  # 1.16μs -> 910ns (27.7% faster)


def test_ttl_as_boolean_true_raises_typeerror():
    """Test that passing ttl=True raises TypeError (since True coerces to 1)."""
    # Accepting True as 1 is a subtle bug, so we want to catch it.
    kd = KeyData("test_value", ttl=True)
    # Should not be expired immediately
    codeflash_output = kd.is_expired()  # 3.42μs -> 1.22μs (180% faster)
    time.sleep(1.1)
    codeflash_output = kd.is_expired()  # 28.0μs -> 7.77μs (260% faster)


def test_expiry_field_is_correctly_calculated():
    """Test that expiry is correctly calculated as now + ttl."""
    now = time.time()
    kd = KeyData("test_value", ttl=2)


# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import time
from typing import TypeVar

# imports
import pytest
from app.config.providers.in_memory_store import KeyData

T = TypeVar("T")

# unit tests

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


def test_is_expired_no_ttl_returns_false():
    """Test that is_expired returns False when no TTL is set."""
    kd = KeyData("foo")
    codeflash_output = kd.is_expired()  # 1.09μs -> 982ns (11.0% faster)


def test_is_expired_with_positive_ttl_not_expired():
    """Test that is_expired returns False immediately after creation with positive TTL."""
    kd = KeyData("bar", ttl=5)
    codeflash_output = kd.is_expired()  # 3.10μs -> 1.29μs (140% faster)


def test_is_expired_with_zero_ttl_is_expired():
    """Test that is_expired returns True when TTL is zero (should expire immediately)."""
    kd = KeyData("baz", ttl=0)
    codeflash_output = kd.is_expired()  # 981ns -> 729ns (34.6% faster)


def test_is_expired_with_negative_ttl_is_expired():
    """Test that is_expired returns True when TTL is negative (should expire immediately)."""
    kd = KeyData("qux", ttl=-10)
    codeflash_output = kd.is_expired()  # 2.88μs -> 1.13μs (155% faster)


def test_is_expired_after_ttl_expiry():
    """Test that is_expired returns True after TTL seconds have passed."""
    kd = KeyData("delayed", ttl=1)
    time.sleep(1.1)  # Sleep slightly longer than TTL
    codeflash_output = kd.is_expired()  # 27.2μs -> 9.01μs (202% faster)


def test_is_expired_before_ttl_expiry():
    """Test that is_expired returns False before TTL seconds have passed."""
    kd = KeyData("early", ttl=2)
    time.sleep(0.5)
    codeflash_output = kd.is_expired()  # 27.0μs -> 8.26μs (227% faster)


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


def test_is_expired_ttl_is_none_explicit():
    """Test that is_expired returns False when TTL is explicitly None."""
    kd = KeyData("none", ttl=None)
    codeflash_output = kd.is_expired()  # 1.17μs -> 893ns (30.8% faster)


def test_is_expired_ttl_is_large_value():
    """Test that is_expired returns False for a very large TTL."""
    kd = KeyData("long", ttl=10**6)
    codeflash_output = kd.is_expired()  # 3.14μs -> 1.20μs (163% faster)


def test_is_expired_ttl_is_minimal_positive():
    """Test that is_expired returns False for a minimal positive TTL just after creation."""
    kd = KeyData("minimal", ttl=1)
    codeflash_output = kd.is_expired()  # 2.98μs -> 1.13μs (164% faster)


def test_is_expired_ttl_is_minimal_positive_after_expiry():
    """Test that is_expired returns True for a minimal positive TTL after expiry."""
    kd = KeyData("minimal", ttl=1)
    time.sleep(1.05)
    codeflash_output = kd.is_expired()  # 26.5μs -> 8.56μs (209% faster)


def test_is_expired_multiple_calls_consistent():
    """Test that multiple calls to is_expired are consistent (do not alternate)."""
    kd = KeyData("repeat", ttl=1)
    codeflash_output = kd.is_expired()
    val1 = codeflash_output  # 3.39μs -> 1.33μs (154% faster)
    codeflash_output = kd.is_expired()
    val2 = codeflash_output  # 1.59μs -> 625ns (154% faster)


def test_is_expired_float_ttl():
    """Test that is_expired works with float TTL values."""
    kd = KeyData("float", ttl=0.5)
    time.sleep(0.6)
    codeflash_output = kd.is_expired()  # 27.8μs -> 9.09μs (206% faster)


def test_is_expired_ttl_is_falsey_value():
    """Test that is_expired returns False when TTL is a falsey value (None, 0)."""
    kd_none = KeyData("none", ttl=None)
    kd_zero = KeyData("zero", ttl=0)
    codeflash_output = kd_none.is_expired()  # 1.00μs -> 886ns (13.4% faster)
    codeflash_output = kd_zero.is_expired()  # 440ns -> 365ns (20.5% faster)


def test_is_expired_ttl_is_large_negative():
    """Test that is_expired returns True for a very large negative TTL."""
    kd = KeyData("neg", ttl=-(10**6))
    codeflash_output = kd.is_expired()  # 3.16μs -> 1.13μs (181% faster)


def test_is_expired_expiry_attribute_manually_set():
    """Test that manually setting expiry attribute works as expected."""
    kd = KeyData("manual", ttl=5)
    kd.expiry = time.time() - 1  # set expiry in the past
    codeflash_output = kd.is_expired()  # 2.88μs -> 983ns (192% faster)
    kd.expiry = time.time() + 10  # set expiry in the future
    codeflash_output = kd.is_expired()  # 1.78μs -> 602ns (196% faster)


def test_is_expired_expiry_attribute_set_to_none():
    """Test that manually setting expiry to None disables expiry."""
    kd = KeyData("manual", ttl=1)
    kd.expiry = None
    codeflash_output = kd.is_expired()  # 863ns -> 747ns (15.5% faster)


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


def test_is_expired_many_instances_varied_ttl():
    """Test is_expired on a large number of instances with varied TTLs."""
    instances = []
    for i in range(1000):
        ttl = i % 5  # TTL cycles from 0 to 4
        instances.append(KeyData(f"val{i}", ttl=ttl))
    time.sleep(5.1)  # Wait so all TTLs have expired
    for kd in instances:
        codeflash_output = kd.is_expired()  # 1.22ms -> 435μs (181% faster)


def test_is_expired_many_instances_no_ttl():
    """Test is_expired on a large number of instances with no TTL."""
    instances = [KeyData(f"val{i}") for i in range(1000)]
    for kd in instances:
        codeflash_output = kd.is_expired()  # 365μs -> 277μs (31.6% faster)


def test_is_expired_performance_many_non_expired():
    """Test performance and correctness for many non-expired instances."""
    instances = [KeyData(f"val{i}", ttl=1000) for i in range(1000)]
    for kd in instances:
        codeflash_output = kd.is_expired()  # 1.37ms -> 445μs (208% faster)


def test_is_expired_performance_mix_expired_and_non_expired():
    """Test correctness for a mix of expired and non-expired instances."""
    expired = [KeyData(f"expired{i}", ttl=0) for i in range(500)]
    non_expired = [KeyData(f"nonexpired{i}", ttl=10) for i in range(500)]
    for kd in expired:
        codeflash_output = kd.is_expired()  # 185μs -> 141μs (30.7% faster)
    for kd in non_expired:
        codeflash_output = kd.is_expired()  # 685μs -> 219μs (212% faster)


def test_is_expired_high_precision_ttl():
    """Test that is_expired works with high-precision float TTLs."""
    kd = KeyData("precise", ttl=0.001)
    time.sleep(0.002)
    codeflash_output = kd.is_expired()  # 13.3μs -> 1.87μs (611% faster)


# -------- MISCELLANEOUS CASES --------


def test_is_expired_with_various_types():
    """Test that is_expired works regardless of value type."""
    kd_str = KeyData("string", ttl=1)
    kd_int = KeyData(123, ttl=1)
    kd_list = KeyData([1, 2, 3], ttl=1)
    kd_dict = KeyData({"a": 1}, ttl=1)
    time.sleep(1.1)
    codeflash_output = kd_str.is_expired()  # 26.6μs -> 8.61μs (209% faster)
    codeflash_output = kd_int.is_expired()  # 2.58μs -> 752ns (243% faster)
    codeflash_output = kd_list.is_expired()  # 1.66μs -> 600ns (176% faster)
    codeflash_output = kd_dict.is_expired()  # 1.43μs -> 462ns (210% faster)


def test_is_expired_ttl_is_boolean():
    """Test that is_expired treats boolean TTLs correctly (True=1s, False=expired)."""
    kd_true = KeyData("booltrue", ttl=True)
    kd_false = KeyData("boolfalse", ttl=False)
    time.sleep(1.1)
    codeflash_output = kd_true.is_expired()  # 27.1μs -> 8.39μs (223% faster)
    codeflash_output = kd_false.is_expired()  # 1.05μs -> 508ns (107% faster)


def test_is_expired_ttl_is_string_raises_typeerror():
    """Test that is_expired raises TypeError if TTL is a string (invalid type)."""
    with pytest.raises(TypeError):
        KeyData("oops", ttl="notanumber")


def test_is_expired_ttl_is_object_raises_typeerror():
    """Test that is_expired raises TypeError if TTL is an object (invalid type)."""

    class Dummy:
        pass

    with pytest.raises(TypeError):
        KeyData("oops", ttl=Dummy())


# 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-KeyData.is_expired-mja2cksq and push.

Codeflash Static Badge

The optimized code achieves a **152% speedup** by implementing **conditional logging guards** that eliminate expensive debug operations when logging is disabled.

**Key Optimization:**
- **Added `logger.isEnabledFor(10)` checks** before debug logging calls in the `is_expired()` method
- **Eliminated expensive string formatting** when debug logging is disabled

**Why This Works:**
The original code unconditionally executed `logger.debug()` calls with expensive string formatting operations (`time.strftime()` and string interpolation), even when debug logging was disabled. The profiler shows these operations consumed 45.6% of total runtime (25.2% + 21.5% + 3.5% from logger.debug lines).

The optimization wraps debug statements with `logger.isEnabledFor(10)` guards (DEBUG level = 10), which is a lightweight check that prevents:
- String interpolation operations
- `time.strftime()` calls (1279.6 nanoseconds per hit)
- `time.localtime()` conversions
- Multiple function call overhead

**Performance Impact by Test Case:**
- **No TTL cases**: 6-30% improvement (minimal logging overhead)
- **Unexpired TTL cases**: 78-180% improvement (avoids expensive time formatting)
- **Expired TTL cases**: 202-260% improvement (eliminates both logging paths)
- **Large-scale tests**: 181-208% improvement (multiplicative effect across many calls)

**Production Benefit:**
This optimization is particularly effective in production environments where debug logging is typically disabled, making the `is_expired()` method significantly faster for cache hit/miss decisions and TTL validations without changing any functional behavior.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 December 17, 2025 13:44
@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