Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 28, 2025

📄 281% (2.81x) speedup for NoOpControlAuthnPlugin.get_login_details in framework/py/flwr/superlink/auth_plugin/noop_auth_plugin.py

⏱️ Runtime : 1.69 milliseconds 444 microseconds (best of 140 runs)

📝 Explanation and details

The optimization replaces repeated object construction with a pre-computed module-level constant. The original code creates a new AccountAuthLoginDetails object every time get_login_details() is called, requiring constructor execution and memory allocation. The optimized version creates this object once at module import time as _NOOP_LOGIN_DETAILS and simply returns the same immutable object reference on each call.

Key changes:

  • Added module-level constant _NOOP_LOGIN_DETAILS containing the pre-constructed object
  • Modified get_login_details() to return the constant instead of creating new instances

Why this is faster:
The line profiler shows the original constructor call takes 8.2ms total time across multiple invocations, with significant overhead in object instantiation and field assignment. The optimized version reduces this to just 0.6ms - simply returning a reference to an existing object.

Performance characteristics:
This optimization provides consistent 2.8x speedup regardless of call frequency, making it particularly effective for:

  • High-frequency access patterns (like the 1000-call performance test)
  • Bulk operations with many plugin instances
  • Any scenario where get_login_details() is called repeatedly

The optimization is safe because AccountAuthLoginDetails appears to be an immutable data structure (NamedTuple), so sharing the same instance across calls maintains correctness while eliminating redundant object creation overhead.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 3771 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 2 Passed
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from enum import Enum
from pathlib import Path
from typing import NamedTuple, Optional

# imports
import pytest  # used for our unit tests
from superlink.auth_plugin.noop_auth_plugin import NoOpControlAuthnPlugin


# --- Stubs for flwr.common.constant.AuthnType ---
class AuthnType(Enum):
    NOOP = "noop"
    OTHER = "other"  # for negative/mutation testing

# --- Stubs for flwr.common.typing.AccountAuthLoginDetails ---
class AccountAuthLoginDetails(NamedTuple):
    authn_type: AuthnType
    device_code: str
    verification_uri_complete: str
    expires_in: int
    interval: int

# --- Abstract base class stub for ControlAuthnPlugin ---
class ControlAuthnPlugin:
    def __init__(self, account_auth_config_path: Path, verify_tls_cert: bool):
        pass
    def get_login_details(self) -> Optional[AccountAuthLoginDetails]:
        raise NotImplementedError
from superlink.auth_plugin.noop_auth_plugin import NoOpControlAuthnPlugin

# unit tests

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

def test_get_login_details_returns_expected_type():
    """Test that get_login_details returns an AccountAuthLoginDetails instance."""
    plugin = NoOpControlAuthnPlugin(Path("/tmp/dummy"), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_authn_type_is_noop():
    """Test that authn_type is AuthnType.NOOP."""
    plugin = NoOpControlAuthnPlugin(Path("/tmp/dummy"), verify_tls_cert=False)
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_device_code_empty():
    """Test that device_code is empty string."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=False)
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_verification_uri_complete_empty():
    """Test that verification_uri_complete is empty string."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_expires_in_zero():
    """Test that expires_in is zero."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_interval_zero():
    """Test that interval is zero."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=False)
    codeflash_output = plugin.get_login_details(); details = codeflash_output

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

def test_get_login_details_with_nonexistent_path():
    """Test with a path that does not exist (should not affect output)."""
    plugin = NoOpControlAuthnPlugin(Path("/path/does/not/exist"), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_with_empty_path():
    """Test with an empty path string."""
    plugin = NoOpControlAuthnPlugin(Path(""), verify_tls_cert=False)
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_with_various_verify_tls_cert_values():
    """Test with both True and False for verify_tls_cert."""
    plugin_true = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    plugin_false = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=False)
    codeflash_output = plugin_true.get_login_details(); details_true = codeflash_output
    codeflash_output = plugin_false.get_login_details(); details_false = codeflash_output

def test_get_login_details_returns_new_instance_each_time():
    """Test that get_login_details returns a new instance each call."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); d1 = codeflash_output
    codeflash_output = plugin.get_login_details(); d2 = codeflash_output

def test_get_login_details_does_not_raise():
    """Test that get_login_details never raises an exception."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    try:
        plugin.get_login_details()
    except Exception as e:
        pytest.fail(f"get_login_details raised an exception: {e}")

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

def test_get_login_details_many_instances():
    """Test creating many plugin instances and calling get_login_details."""
    # Test with 500 different plugin instances
    paths = [Path(f"/tmp/path_{i}") for i in range(500)]
    plugins = [NoOpControlAuthnPlugin(p, verify_tls_cert=bool(i % 2)) for i, p in enumerate(paths)]
    for plugin in plugins:
        codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_called_many_times():
    """Test calling get_login_details many times in a row."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    for _ in range(1000):
        codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_memory_usage():
    """Test that repeated calls do not accumulate state (basic memory check)."""
    import gc
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    refs_before = len(gc.get_objects())
    for _ in range(500):
        plugin.get_login_details()
    gc.collect()
    refs_after = len(gc.get_objects())

# ----------- Mutation/Negative Test Cases -----------

def test_get_login_details_fails_if_authn_type_wrong(monkeypatch):
    """Mutation test: fails if authn_type is not NOOP."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    # Patch the method to return wrong authn_type
    original = plugin.get_login_details
    def fake_get_login_details():
        return AccountAuthLoginDetails(
            authn_type=AuthnType.OTHER,
            device_code="",
            verification_uri_complete="",
            expires_in=0,
            interval=0,
        )
    plugin.get_login_details = fake_get_login_details
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_fails_if_device_code_not_empty(monkeypatch):
    """Mutation test: fails if device_code is not empty."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    # Patch the method to return non-empty device_code
    def fake_get_login_details():
        return AccountAuthLoginDetails(
            authn_type=AuthnType.NOOP,
            device_code="something",
            verification_uri_complete="",
            expires_in=0,
            interval=0,
        )
    plugin.get_login_details = fake_get_login_details
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_fails_if_expires_in_not_zero(monkeypatch):
    """Mutation test: fails if expires_in is not zero."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    def fake_get_login_details():
        return AccountAuthLoginDetails(
            authn_type=AuthnType.NOOP,
            device_code="",
            verification_uri_complete="",
            expires_in=123,
            interval=0,
        )
    plugin.get_login_details = fake_get_login_details
    codeflash_output = plugin.get_login_details(); details = codeflash_output

def test_get_login_details_fails_if_interval_not_zero(monkeypatch):
    """Mutation test: fails if interval is not zero."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    def fake_get_login_details():
        return AccountAuthLoginDetails(
            authn_type=AuthnType.NOOP,
            device_code="",
            verification_uri_complete="",
            expires_in=0,
            interval=5,
        )
    plugin.get_login_details = fake_get_login_details
    codeflash_output = plugin.get_login_details(); details = codeflash_output
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from enum import Enum
from pathlib import Path
from typing import NamedTuple, Optional

# imports
import pytest  # used for our unit tests
from superlink.auth_plugin.noop_auth_plugin import NoOpControlAuthnPlugin

# function to test
# (copied from the provided code, with necessary mock classes for dependencies)

# --- Begin: Mocked dependencies for testing ---


# Mock AuthnType enum
class AuthnType(Enum):
    NOOP = "noop"
    OTHER = "other"

# Mock AccountAuthLoginDetails NamedTuple
class AccountAuthLoginDetails(NamedTuple):
    authn_type: AuthnType
    device_code: str
    verification_uri_complete: str
    expires_in: int
    interval: int

# Mock ControlAuthnPlugin base class
class ControlAuthnPlugin:
    pass
from superlink.auth_plugin.noop_auth_plugin import NoOpControlAuthnPlugin

# unit tests

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

def test_get_login_details_returns_expected_type():
    """Test that get_login_details returns an AccountAuthLoginDetails object."""
    plugin = NoOpControlAuthnPlugin(Path("/tmp/fake"), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_authn_type_is_noop():
    """Test that authn_type is NOOP."""
    plugin = NoOpControlAuthnPlugin(Path("/tmp/fake"), verify_tls_cert=False)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_device_code_empty():
    """Test that device_code is an empty string."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_verification_uri_complete_empty():
    """Test that verification_uri_complete is an empty string."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=False)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_expires_in_zero():
    """Test that expires_in is zero."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_interval_zero():
    """Test that interval is zero."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=False)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

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

def test_get_login_details_with_nonexistent_path():
    """Test plugin with a non-existent config path."""
    plugin = NoOpControlAuthnPlugin(Path("/path/does/not/exist"), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_with_empty_path():
    """Test plugin with an empty string as path."""
    plugin = NoOpControlAuthnPlugin(Path(""), verify_tls_cert=False)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_with_various_verify_tls_cert_values():
    """Test plugin with both True and False for verify_tls_cert."""
    for val in [True, False]:
        plugin = NoOpControlAuthnPlugin(Path("/tmp/somefile"), verify_tls_cert=val)
        codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_is_deterministic():
    """Test that repeated calls return the same result (object equality)."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result1 = codeflash_output
    codeflash_output = plugin.get_login_details(); result2 = codeflash_output


def test_get_login_details_many_instances():
    """Test creating and using many plugin instances in succession."""
    paths = [Path(f"/tmp/fake{i}") for i in range(100)]
    plugins = [NoOpControlAuthnPlugin(p, verify_tls_cert=(i % 2 == 0)) for i, p in enumerate(paths)]
    for i, plugin in enumerate(plugins):
        codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_bulk_call_performance():
    """Test calling get_login_details many times in a loop (performance/scalability)."""
    plugin = NoOpControlAuthnPlugin(Path("/tmp/fake"), verify_tls_cert=True)
    # Call 1000 times and check all results
    for _ in range(1000):
        codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_with_long_path():
    """Test plugin with a very long path."""
    long_path = Path("/tmp/" + "a" * 200)
    plugin = NoOpControlAuthnPlugin(long_path, verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

# ------------------------
# Mutation Testing Guards
# ------------------------

def test_get_login_details_fails_if_authn_type_changed():
    """Guard: If authn_type is not NOOP, test should fail."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_fails_if_device_code_nonempty():
    """Guard: If device_code is not empty, test should fail."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_fails_if_verification_uri_complete_nonempty():
    """Guard: If verification_uri_complete is not empty, test should fail."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_fails_if_expires_in_nonzero():
    """Guard: If expires_in is not zero, test should fail."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output

def test_get_login_details_fails_if_interval_nonzero():
    """Guard: If interval is not zero, test should fail."""
    plugin = NoOpControlAuthnPlugin(Path("."), verify_tls_cert=True)
    codeflash_output = plugin.get_login_details(); result = codeflash_output
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from pathlib import Path
from superlink.auth_plugin.noop_auth_plugin import NoOpControlAuthnPlugin

def test_NoOpControlAuthnPlugin_get_login_details():
    NoOpControlAuthnPlugin.get_login_details(NoOpControlAuthnPlugin(Path(), False))
🔎 Concolic Coverage Tests and Runtime

To edit these changes git checkout codeflash/optimize-NoOpControlAuthnPlugin.get_login_details-mha06rmt and push.

Codeflash

The optimization replaces repeated object construction with a pre-computed module-level constant. The original code creates a new `AccountAuthLoginDetails` object every time `get_login_details()` is called, requiring constructor execution and memory allocation. The optimized version creates this object once at module import time as `_NOOP_LOGIN_DETAILS` and simply returns the same immutable object reference on each call.

**Key changes:**
- Added module-level constant `_NOOP_LOGIN_DETAILS` containing the pre-constructed object
- Modified `get_login_details()` to return the constant instead of creating new instances

**Why this is faster:**
The line profiler shows the original constructor call takes 8.2ms total time across multiple invocations, with significant overhead in object instantiation and field assignment. The optimized version reduces this to just 0.6ms - simply returning a reference to an existing object.

**Performance characteristics:**
This optimization provides consistent 2.8x speedup regardless of call frequency, making it particularly effective for:
- High-frequency access patterns (like the 1000-call performance test)
- Bulk operations with many plugin instances 
- Any scenario where `get_login_details()` is called repeatedly

The optimization is safe because `AccountAuthLoginDetails` appears to be an immutable data structure (NamedTuple), so sharing the same instance across calls maintains correctness while eliminating redundant object creation overhead.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 28, 2025 03:24
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Oct 28, 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