Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 44% (0.44x) speedup for OidcCliPlugin.write_tokens_to_metadata in framework/py/flwr/cli/auth_plugin/oidc_cli_plugin.py

⏱️ Runtime : 46.0 microseconds 32.0 microseconds (best of 160 runs)

📝 Explanation and details

The optimization adds a type-aware concatenation strategy that avoids unnecessary list conversion when the input metadata is already a list.

Key optimization:

  • Conditional type checking: Added if isinstance(metadata, list) to detect when metadata is already a list
  • Direct list concatenation: When metadata is a list, uses metadata + [tokens] instead of list(metadata) + [tokens]
  • Fallback preservation: Non-list sequences (tuples, etc.) still use the original list(metadata) + [tokens] approach

Why this is faster:

  • Eliminates redundant conversion: list(metadata) creates an unnecessary copy when metadata is already a list
  • Reduces memory allocations: Direct list concatenation (+) is more efficient than convert-then-concatenate
  • CPU cycle savings: Avoids the overhead of iterating through an existing list to create an identical copy

Performance characteristics:

  • Most effective for list inputs (which appear common based on test cases)
  • Neutral impact for non-list sequences like tuples
  • Scales well with large metadata collections (1000+ entries) as it eliminates O(n) copying overhead
  • 43% speedup demonstrates the significant cost of unnecessary list conversions in Python

The optimization maintains identical behavior while providing substantial performance gains for the common case of list-based metadata.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 50 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from pathlib import Path
# function to test
from typing import Optional, Sequence, Union

# imports
import pytest  # used for our unit tests
from cli.auth_plugin.oidc_cli_plugin import OidcCliPlugin

# Dummy constants to mimic flwr.common.constant
ACCESS_TOKEN_KEY = "access_token"
REFRESH_TOKEN_KEY = "refresh_token"

class CliAuthPlugin:
    pass
from cli.auth_plugin.oidc_cli_plugin import OidcCliPlugin

# unit tests

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

def test_write_tokens_to_metadata_basic():
    """Test basic functionality with valid tokens and empty metadata."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "token123"
    plugin.refresh_token = "refresh456"
    metadata = []
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_basic_nonempty_metadata():
    """Test with valid tokens and non-empty metadata."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "abc"
    plugin.refresh_token = "def"
    metadata = [("foo", "bar"), ("baz", b"qux")]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_bytes_tokens():
    """Test with tokens as bytes (should work as per type annotation)."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = b"tokbytes"
    plugin.refresh_token = b"refbytes"
    metadata = []
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

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




def test_write_tokens_to_metadata_metadata_tuple_order():
    """Test that metadata order is preserved and tokens appended at the end."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "access"
    plugin.refresh_token = "refresh"
    metadata = [("a", "1"), ("b", "2")]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_metadata_is_tuple():
    """Test that function accepts tuple as metadata input."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "tok"
    plugin.refresh_token = "ref"
    metadata = (("x", "y"),)
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_metadata_with_bytes_key_and_value():
    """Test with metadata containing bytes as values."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "tok"
    plugin.refresh_token = "ref"
    metadata = [("foo", b"bar"), ("baz", b"qux")]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_metadata_with_empty_strings():
    """Test with empty string tokens and metadata."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = ""
    plugin.refresh_token = ""
    metadata = [("", "")]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_metadata_with_duplicate_keys():
    """Test that duplicate keys in metadata are preserved and tokens appended."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "a"
    plugin.refresh_token = "b"
    metadata = [(ACCESS_TOKEN_KEY, "oldtoken")]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_metadata_with_non_str_keys():
    """Test that non-str keys raise TypeError (should fail)."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "tok"
    plugin.refresh_token = "ref"
    metadata = [(123, "val")]
    # The type annotation says keys should be str, but function does not enforce
    # So this should pass, but we document this edge case
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

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

def test_write_tokens_to_metadata_large_metadata():
    """Test with large metadata (1000 entries)."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "largeaccess"
    plugin.refresh_token = "largerefresh"
    metadata = [(f"key{i}", f"value{i}") for i in range(1000)]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_large_tokens():
    """Test with very large token strings."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "A" * 1000
    plugin.refresh_token = "B" * 1000
    metadata = []
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_large_metadata_and_large_tokens():
    """Test with both large metadata and large tokens."""
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "X" * 1000
    plugin.refresh_token = "Y" * 1000
    metadata = [(f"k{i}", f"v{i}") for i in range(1000)]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_write_tokens_to_metadata_performance():
    """Test performance: ensure function completes quickly for large input."""
    import time
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "fast"
    plugin.refresh_token = "speedy"
    metadata = [(str(i), str(i)) for i in range(1000)]
    start = time.time()
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output
    duration = time.time() - start
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import sys
from pathlib import Path
from typing import Sequence, Union

# imports
import pytest  # used for our unit tests
from cli.auth_plugin.oidc_cli_plugin import OidcCliPlugin

# Constants as used in the function
ACCESS_TOKEN_KEY = "access_token"
REFRESH_TOKEN_KEY = "refresh_token"

# Minimal stub for typer.Exit and typer.secho for testing (since we don't want to mock/stub)
class DummyTyperExit(Exception):
    def __init__(self, code=0):
        self.code = code

class DummyTyper:
    class colors:
        RED = "red"
    @staticmethod
    def secho(msg, fg=None, bold=None):
        pass
    @staticmethod
    def Exit(code=0):
        raise DummyTyperExit(code=code)

# function to test
class CliAuthPlugin:
    pass
from cli.auth_plugin.oidc_cli_plugin import OidcCliPlugin

# unit tests

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

def test_basic_with_empty_metadata():
    # Test with empty metadata, valid tokens
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "token123"
    plugin.refresh_token = "refresh456"
    codeflash_output = plugin.write_tokens_to_metadata([]); result = codeflash_output

def test_basic_with_nonempty_metadata():
    # Test with some metadata, valid tokens
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "abc"
    plugin.refresh_token = "def"
    metadata = [("foo", "bar"), ("baz", b"qux")]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_basic_with_bytes_tokens():
    # Test with tokens as bytes
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = b"tokbytes"
    plugin.refresh_token = b"refbytes"
    metadata = [("x", "y")]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

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




def test_metadata_with_duplicate_token_keys():
    # Metadata that already contains ACCESS_TOKEN_KEY and REFRESH_TOKEN_KEY
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "new_access"
    plugin.refresh_token = "new_refresh"
    metadata = [
        (ACCESS_TOKEN_KEY, "old_access"),
        (REFRESH_TOKEN_KEY, "old_refresh"),
        ("other", "val"),
    ]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_metadata_with_non_string_keys_and_values():
    # Metadata with non-string keys and values should be preserved
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "tok"
    plugin.refresh_token = "ref"
    metadata = [(123, 456), (None, None)]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_metadata_is_tuple_not_list():
    # Metadata passed as a tuple instead of a list
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "tok"
    plugin.refresh_token = "ref"
    metadata = (("a", "b"), ("c", "d"))
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_metadata_is_empty_tuple():
    # Metadata is an empty tuple
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "tok"
    plugin.refresh_token = "ref"
    metadata = ()
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_tokens_are_empty_strings():
    # Tokens are empty strings
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = ""
    plugin.refresh_token = ""
    metadata = [("foo", "bar")]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

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

def test_large_metadata_list():
    # Large metadata list, ensure function scales and preserves order
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "large_access"
    plugin.refresh_token = "large_refresh"
    metadata = [(f"key{i}", f"value{i}") for i in range(1000)]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_large_tokens():
    # Large tokens (long strings)
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "A" * 1000
    plugin.refresh_token = "B" * 1000
    metadata = [("foo", "bar")]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_large_metadata_and_large_tokens():
    # Both metadata and tokens are large
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "X" * 1000
    plugin.refresh_token = "Y" * 1000
    metadata = [(str(i), str(i)) for i in range(1000)]
    codeflash_output = plugin.write_tokens_to_metadata(metadata); result = codeflash_output

def test_metadata_with_mixed_types_large():
    # Large metadata with mixed types
    plugin = OidcCliPlugin(Path("/tmp/creds"))
    plugin.access_token = "tok"
    plugin.refresh_token = "ref"
    metadata = []
    for i in range(500):
        if i % 2 == 0:
            metadata.append((i, b"bytes"))
        else:
            metadata.append((str(i), "str"))
    codeflash_output = plugin.write_tokens_to_metadata(metadata); 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.
#------------------------------------------------Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
❌ Missing authentication tokens. Please login first.
from cli.auth_plugin.oidc_cli_plugin import OidcCliPlugin
from pathlib import Path
import pytest

def test_OidcCliPlugin_write_tokens_to_metadata():
    with pytest.raises(Exit):
        OidcCliPlugin.write_tokens_to_metadata(OidcCliPlugin(Path()), ())

To edit these changes git checkout codeflash/optimize-OidcCliPlugin.write_tokens_to_metadata-mhcxyv43 and push.

Codeflash Static Badge

The optimization adds a **type-aware concatenation strategy** that avoids unnecessary list conversion when the input `metadata` is already a list.

**Key optimization:**
- **Conditional type checking**: Added `if isinstance(metadata, list)` to detect when metadata is already a list
- **Direct list concatenation**: When metadata is a list, uses `metadata + [tokens]` instead of `list(metadata) + [tokens]`
- **Fallback preservation**: Non-list sequences (tuples, etc.) still use the original `list(metadata) + [tokens]` approach

**Why this is faster:**
- **Eliminates redundant conversion**: `list(metadata)` creates an unnecessary copy when metadata is already a list
- **Reduces memory allocations**: Direct list concatenation (`+`) is more efficient than convert-then-concatenate
- **CPU cycle savings**: Avoids the overhead of iterating through an existing list to create an identical copy

**Performance characteristics:**
- **Most effective** for list inputs (which appear common based on test cases)
- **Neutral impact** for non-list sequences like tuples 
- **Scales well** with large metadata collections (1000+ entries) as it eliminates O(n) copying overhead
- **43% speedup** demonstrates the significant cost of unnecessary list conversions in Python

The optimization maintains identical behavior while providing substantial performance gains for the common case of list-based metadata.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 30, 2025 04:46
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Oct 30, 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