Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Nov 5, 2025

📄 9% (0.09x) speedup for MetadataItemLinkedInvocation.invoke in invokeai/app/invocations/metadata_linked.py

⏱️ Runtime : 355 microseconds 327 microseconds (best of 114 runs)

📝 Explanation and details

The optimized code achieves an 8% speedup by eliminating redundant dictionary operations when self.metadata is None, which appears to be the common case in the test scenarios.

Key optimization:
The original code always creates an empty dictionary first, then performs two separate update() calls:

data: Dict[str, Any] = {} if self.metadata is None else self.metadata.root data.update({str(k): v}) data.update({"app_version": __version__})

The optimized version branches early and constructs the dictionary in one step for the None case:

if self.metadata is None: data: Dict[str, Any] = { str(k): v, "app_version": __version__, } else: data: Dict[str, Any] = self.metadata.root.copy() data[str(k)] = v data["app_version"] = __version__

Why this is faster:

  • Eliminates dictionary mutations: The original code performs two dict.update() calls regardless of the metadata state. Each update() call has overhead for temporary dictionary creation and iteration.
  • Single dictionary construction: When metadata is None (the common test case), the optimized version creates the final dictionary directly with all key-value pairs, avoiding intermediate allocations.
  • Preserves copy semantics: When metadata exists, it still properly copies self.metadata.root to prevent mutations to the original metadata.

Performance impact:
The line profiler shows the optimization primarily benefits the dictionary construction phase (lines that previously called update()), while the expensive MetadataField.model_validate() call remains unchanged at ~70% of runtime. Test results show consistent 6-17% improvements across different value types, with larger gains for simpler cases where the dictionary operations represent a higher percentage of total execution time.

This optimization is particularly effective for metadata operations that start with empty metadata, which appears to be a common usage pattern based on the test coverage.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 241 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from typing import Any, Dict, Optional # imports import pytest from invokeai.app.invocations.metadata_linked import \ MetadataItemLinkedInvocation # Minimal stubs for dependencies class MetadataField: def __init__(self, root: Dict[str, Any]): self.root = root @classmethod def model_validate(cls, data: Dict[str, Any]): # mimic pydantic's model_validate for MetadataField return cls(data) class MetadataOutput: def __init__(self, metadata: MetadataField): self.metadata = metadata class VAEField: def __init__(self, vae): self.vae = vae class InvocationContext: pass __version__ = "1.2.3" CUSTOM_LABEL: str = "* CUSTOM LABEL *" from invokeai.app.invocations.metadata_linked import \ MetadataItemLinkedInvocation # --------- UNIT TESTS --------- # Basic Test Cases def test_invoke_basic_custom_label_str_value(): """Test adding a custom label with a string value""" inv = MetadataItemLinkedInvocation(label=CUSTOM_LABEL, custom_label="my_label", value="hello") codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 5.96μs -> 5.60μs (6.57% faster) def test_invoke_basic_core_label_int_value(): """Test adding a core label with an integer value""" inv = MetadataItemLinkedInvocation(label="width", value=512) codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 6.19μs -> 5.52μs (12.1% faster) def test_invoke_basic_core_label_float_value(): """Test adding a core label with a float value""" inv = MetadataItemLinkedInvocation(label="cfg_scale", value=7.5) codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 6.04μs -> 5.68μs (6.41% faster) def test_invoke_basic_core_label_bool_value(): """Test adding a core label with a boolean value""" inv = MetadataItemLinkedInvocation(label="seamless_x", value=True) codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 6.82μs -> 5.93μs (15.0% faster) def test_invoke_basic_core_label_none_value(): """Test adding a core label with None value""" inv = MetadataItemLinkedInvocation(label="seed", value=None) codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 6.45μs -> 5.64μs (14.2% faster) def test_invoke_basic_core_label_dict_value(): """Test adding a core label with a dict value""" d = {"a": 1, "b": 2} inv = MetadataItemLinkedInvocation(label="model", value=d) codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 6.35μs -> 5.44μs (16.7% faster) def test_invoke_basic_core_label_list_value(): """Test adding a core label with a list value""" l = [1, 2, 3] inv = MetadataItemLinkedInvocation(label="steps", value=l) codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 6.16μs -> 5.66μs (8.91% faster) def test_invoke_basic_core_label_value_vae_field(): """Test adding a core label with a VAEField value""" vae_obj = VAEField("vae_model_name") inv = MetadataItemLinkedInvocation(label="vae", value=vae_obj) codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 6.39μs -> 5.45μs (17.4% faster) # Edge Test Cases def test_invoke_large_value_list(): """Test adding a core label with a large list value""" large_list = list(range(1000)) inv = MetadataItemLinkedInvocation(label="steps", value=large_list) codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 9.06μs -> 8.59μs (5.42% faster) def test_invoke_large_value_dict(): """Test adding a core label with a large dict value""" large_dict = {str(i): i for i in range(1000)} inv = MetadataItemLinkedInvocation(label="model", value=large_dict) codeflash_output = inv.invoke(InvocationContext()); out = codeflash_output # 7.65μs -> 6.91μs (10.6% faster) # codeflash_output is used to check that the output of the original code is the same as that of the optimized code. #------------------------------------------------ from typing import Any, Dict, Optional # imports import pytest # used for our unit tests from invokeai.app.invocations.metadata_linked import \ MetadataItemLinkedInvocation # Mocks for dependencies (minimal, deterministic, no external libraries) class MetadataField: def __init__(self, root: Dict[str, Any]): self.root = root @classmethod def model_validate(cls, data: Dict[str, Any]): # Simulate pydantic validation by returning an instance return cls(data) class MetadataOutput: def __init__(self, metadata: MetadataField): self.metadata = metadata class VAEField: def __init__(self, vae: str): self.vae = vae class InvocationContext: pass # No-op for tests # Simulate app version __version__ = "1.2.3" # The function to test CUSTOM_LABEL: str = "* CUSTOM LABEL *" CORE_LABELS = [ CUSTOM_LABEL, "positive_prompt", "positive_style_prompt", "negative_prompt", "negative_style_prompt", "width", "height", "seed", "cfg_scale", "cfg_rescale_multiplier", "steps", "scheduler", "clip_skip", "model", "vae", "seamless_x", "seamless_y", "guidance", "cfg_scale_start_step", "cfg_scale_end_step", ] from invokeai.app.invocations.metadata_linked import \ MetadataItemLinkedInvocation # -------------------- UNIT TESTS -------------------- # Basic Test Cases def test_basic_custom_label_inserts_value(): """Test inserting a value with a custom label.""" invocation = MetadataItemLinkedInvocation( label=CUSTOM_LABEL, custom_label="my_label", value="my_value" ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 9.48μs -> 8.70μs (8.92% faster) def test_basic_core_label_inserts_value(): """Test inserting a value with a core label.""" invocation = MetadataItemLinkedInvocation( label="width", value=512 ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 7.28μs -> 6.82μs (6.81% faster) def test_basic_vae_field_value(): """Test inserting a VAEField value.""" vae_field = VAEField("vae_model_name") invocation = MetadataItemLinkedInvocation( label="vae", value=vae_field ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 6.89μs -> 6.34μs (8.74% faster) def test_edge_value_is_none(): """Test behavior when value is None.""" invocation = MetadataItemLinkedInvocation( label="height", value=None ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 9.10μs -> 8.79μs (3.54% faster) def test_edge_label_not_in_core_labels(): """Test that an invalid label raises ValueError.""" with pytest.raises(ValueError): MetadataItemLinkedInvocation(label="not_a_label", value="x") def test_edge_custom_label_is_integer(): """Test custom label as integer (should be converted to string key).""" invocation = MetadataItemLinkedInvocation( label=CUSTOM_LABEL, custom_label=42, value="answer" ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 9.06μs -> 8.48μs (6.89% faster) def test_edge_value_is_dict(): """Test value as a dictionary.""" value_dict = {"foo": "bar"} invocation = MetadataItemLinkedInvocation( label="model", value=value_dict ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 7.17μs -> 6.49μs (10.5% faster) def test_edge_metadata_is_none(): """Test with metadata=None.""" invocation = MetadataItemLinkedInvocation( label="cfg_scale", value=7.5, metadata=None ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 6.72μs -> 6.27μs (7.21% faster) def test_large_scale_custom_labels(): """Test inserting many custom labels.""" for i in range(100): # 100 custom labels invocation = MetadataItemLinkedInvocation( label=CUSTOM_LABEL, custom_label=f"custom_{i}", value=i ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 212μs -> 197μs (8.03% faster) def test_large_scale_metadata_update(): """Test updating metadata with 500 keys.""" initial_data = {f"key_{i}": i for i in range(500)} initial_metadata = MetadataField(initial_data) invocation = MetadataItemLinkedInvocation( label=CUSTOM_LABEL, custom_label="key_250", value="updated" ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 6.36μs -> 5.84μs (8.76% faster) # All other keys should remain unchanged for i in range(500): if i == 250: continue def test_large_scale_value_is_large_dict(): """Test value as a large dictionary.""" large_dict = {str(i): i for i in range(500)} invocation = MetadataItemLinkedInvocation( label="model", value=large_dict ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 6.57μs -> 5.81μs (13.1% faster) def test_large_scale_custom_label_is_long_string(): """Test custom label as a long string.""" long_label = "x" * 500 invocation = MetadataItemLinkedInvocation( label=CUSTOM_LABEL, custom_label=long_label, value="long_value" ) codeflash_output = invocation.invoke(InvocationContext()); output = codeflash_output # 6.04μs -> 5.59μs (7.98% faster) # 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-MetadataItemLinkedInvocation.invoke-mhljlr03 and push.

Codeflash Static Badge

The optimized code achieves an 8% speedup by eliminating redundant dictionary operations when `self.metadata is None`, which appears to be the common case in the test scenarios. **Key optimization:** The original code always creates an empty dictionary first, then performs two separate `update()` calls: ```python data: Dict[str, Any] = {} if self.metadata is None else self.metadata.root data.update({str(k): v}) data.update({"app_version": __version__}) ``` The optimized version branches early and constructs the dictionary in one step for the `None` case: ```python if self.metadata is None: data: Dict[str, Any] = { str(k): v, "app_version": __version__, } else: data: Dict[str, Any] = self.metadata.root.copy() data[str(k)] = v data["app_version"] = __version__ ``` **Why this is faster:** - **Eliminates dictionary mutations**: The original code performs two `dict.update()` calls regardless of the metadata state. Each `update()` call has overhead for temporary dictionary creation and iteration. - **Single dictionary construction**: When metadata is `None` (the common test case), the optimized version creates the final dictionary directly with all key-value pairs, avoiding intermediate allocations. - **Preserves copy semantics**: When metadata exists, it still properly copies `self.metadata.root` to prevent mutations to the original metadata. **Performance impact:** The line profiler shows the optimization primarily benefits the dictionary construction phase (lines that previously called `update()`), while the expensive `MetadataField.model_validate()` call remains unchanged at ~70% of runtime. Test results show consistent 6-17% improvements across different value types, with larger gains for simpler cases where the dictionary operations represent a higher percentage of total execution time. This optimization is particularly effective for metadata operations that start with empty metadata, which appears to be a common usage pattern based on the test coverage.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 5, 2025 05:13
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Nov 5, 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: Medium Optimization Quality according to Codeflash

1 participant