Skip to content

Conversation

misrasaurabh1
Copy link
Contributor

@misrasaurabh1 misrasaurabh1 commented Sep 23, 2025

PR Type

Enhancement, Tests


Description

  • Add common tags utility function

  • Implement unit tests for core logic


Diagram Walkthrough

flowchart LR
  util["common_tags utility"] -- "used by" --> tests["unit tests"]
Loading

File Walkthrough

Relevant files
Enhancement
common_tags.py
Add function to compute common tags                                           

codeflash/result/common_tags.py

  • Introduce find_common_tags function.
  • Handles empty input returning empty set.
  • Computes intersection of tags across articles.
+12/-0   
Tests
test_common_tags.py
Add unit tests for common tags utility                                     

tests/test_common_tags.py

  • Add tests for find_common_tags.
  • Validate common tags across multiple article lists.
+23/-0   

Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 1 🔵⚪⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Algorithm Choice

Current implementation uses list intersection logic each step, which is O(n*m) over lists and may duplicate work; using set intersection throughout would be clearer and more efficient for larger tag lists.

common_tags = articles[0].get("tags", [])
for article in articles[1:]:
    common_tags = [tag for tag in common_tags if tag in article.get("tags", [])]
return set(common_tags)
Type Robustness

The function assumes every article has a list under key tags; consider guarding against non-list values or normalizing case/whitespace if inputs can vary.

def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
    if not articles:
        return set()

    common_tags = articles[0].get("tags", [])
    for article in articles[1:]:
        common_tags = [tag for tag in common_tags if tag in article.get("tags", [])]
    return set(common_tags)

Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Use set intersections for efficiency

Avoid repeated list scans by using set intersections, which are faster and simpler.
Initialize with the first article's tags as a set and intersect with each subsequent
set to prevent duplicates and improve performance.

codeflash/result/common_tags.py [8-11]

-common_tags = articles[0].get("tags", [])
+common_tags = set(articles[0].get("tags", []))
 for article in articles[1:]:
-    common_tags = [tag for tag in common_tags if tag in article.get("tags", [])]
-return set(common_tags)
+    common_tags &= set(article.get("tags", []))
+return common_tags
Suggestion importance[1-10]: 7

__

Why: Correctly replaces list filtering with set intersections, improving performance and clarity while preserving behavior; maps directly to the referenced lines.

Medium
Possible issue
Guard against malformed input

Validate that the first element contains a list of tags to avoid unexpected behavior
or runtime errors when data is malformed. Return an empty set if tags are missing or
not a list.

codeflash/result/common_tags.py [4-6]

 def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
     if not articles:
         return set()
+    first_tags = articles[0].get("tags", [])
+    if not isinstance(first_tags, list):
+        return set()
+    ...
Suggestion importance[1-10]: 5

__

Why: Reasonable defensive check that could prevent errors with malformed input, but the current code and tests don't require it; suggestion uses an ellipsis and is partial, limiting direct applicability.

Low

Comment on lines +8 to +12
common_tags = articles[0].get("tags", [])
for article in articles[1:]:
common_tags = [tag for tag in common_tags if tag in article.get("tags", [])]
return set(common_tags)

No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡️Codeflash found 7,957% (79.57x) speedup for find_common_tags in codeflash/result/common_tags.py

⏱️ Runtime : 578 milliseconds 7.17 milliseconds (best of 91 runs)

📝 Explanation and details

The optimization replaces inefficient list operations with set-based intersection operations, delivering an impressive 79x speedup.

Key Changes:

  1. Initialize with a set: common_tags = set(articles[0].get("tags", [])) instead of keeping it as a list
  2. Use set intersection: common_tags.intersection_update(article.get("tags", [])) instead of list comprehension filtering

Why this is dramatically faster:

  • The original code uses [tag for tag in common_tags if tag in article.get("tags", [])] which has O(n×m) complexity for each article (where n is current common tags, m is article tags)
  • Set intersection is O(min(n,m)) and operates on optimized hash tables
  • The line profiler shows the filtering line went from 637ms (99.5% of runtime) to just 12ms (81% of much smaller total)

Performance characteristics:

  • Small datasets: 10-65% faster across basic test cases
  • Large datasets: Up to 110x faster (e.g., 381ms → 3.44ms for 100 articles with 1000 tags each)
  • The optimization scales particularly well when articles have many tags or when processing many articles, as evidenced by the massive improvements in test_large_number_of_tags (5282% faster) and large-scale test cases (11000%+ faster)

The set-based approach eliminates the quadratic behavior of the original list-in-list membership tests.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 2 Passed
🌀 Generated Regression Tests 29 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 2 Passed
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_common_tags.py::test_common_tags_1 5.96μs 3.90μs 53.0%✅
🌀 Generated Regression Tests and Runtime
# imports
# function to test
from __future__ import annotations

import pytest  # used for our unit tests
from codeflash.result.common_tags import find_common_tags

# unit tests

def test_single_article():
    # Single article should return its tags
    articles = [{"tags": ["python", "coding", "tutorial"]}]
    codeflash_output = find_common_tags(articles) # 1.53μs -> 1.24μs (23.3% faster)
    # Outputs were verified to be equal to the original implementation

def test_multiple_articles_with_common_tags():
    # Multiple articles with common tags should return the common tags
    articles = [
        {"tags": ["python", "coding"]},
        {"tags": ["python", "data"]},
        {"tags": ["python", "machine learning"]}
    ]
    codeflash_output = find_common_tags(articles) # 2.67μs -> 2.13μs (24.9% faster)
    # Outputs were verified to be equal to the original implementation

def test_empty_list_of_articles():
    # Empty list of articles should return an empty set
    articles = []
    codeflash_output = find_common_tags(articles) # 762ns -> 461ns (65.3% faster)
    # Outputs were verified to be equal to the original implementation

def test_articles_with_no_common_tags():
    # Articles with no common tags should return an empty set
    articles = [
        {"tags": ["python"]},
        {"tags": ["java"]},
        {"tags": ["c++"]}
    ]
    codeflash_output = find_common_tags(articles) # 2.31μs -> 1.98μs (16.7% faster)
    # Outputs were verified to be equal to the original implementation

def test_articles_with_empty_tag_lists():
    # Articles with some empty tag lists should return an empty set
    articles = [
        {"tags": []},
        {"tags": ["python"]},
        {"tags": ["python", "java"]}
    ]
    codeflash_output = find_common_tags(articles) # 1.98μs -> 1.78μs (11.2% faster)
    # Outputs were verified to be equal to the original implementation

def test_all_articles_with_empty_tag_lists():
    # All articles with empty tag lists should return an empty set
    articles = [
        {"tags": []},
        {"tags": []},
        {"tags": []}
    ]
    codeflash_output = find_common_tags(articles) # 1.86μs -> 1.69μs (10.0% faster)
    # Outputs were verified to be equal to the original implementation

def test_tags_with_special_characters():
    # Tags with special characters should be handled correctly
    articles = [
        {"tags": ["python!", "coding"]},
        {"tags": ["python!", "data"]}
    ]
    codeflash_output = find_common_tags(articles) # 2.11μs -> 1.72μs (22.7% faster)
    # Outputs were verified to be equal to the original implementation

def test_case_sensitivity():
    # Tags with different cases should not be considered the same
    articles = [
        {"tags": ["Python", "coding"]},
        {"tags": ["python", "data"]}
    ]
    codeflash_output = find_common_tags(articles) # 1.94μs -> 1.66μs (16.9% faster)
    # Outputs were verified to be equal to the original implementation

def test_large_number_of_articles():
    # Large number of articles with a common tag should return that tag
    articles = [{"tags": ["common_tag", f"tag{i}"]} for i in range(1000)]
    codeflash_output = find_common_tags(articles) # 226μs -> 147μs (54.0% faster)
    # Outputs were verified to be equal to the original implementation

def test_large_number_of_tags():
    # Large number of tags with some common tags should return the common tags
    articles = [
        {"tags": [f"tag{i}" for i in range(1000)]},
        {"tags": [f"tag{i}" for i in range(500, 1500)]}
    ]
    expected = {f"tag{i}" for i in range(500, 1000)}
    codeflash_output = find_common_tags(articles) # 4.35ms -> 80.8μs (5282% faster)
    # Outputs were verified to be equal to the original implementation

def test_mixed_length_of_tag_lists():
    # Articles with mixed length of tag lists should return the common tags
    articles = [
        {"tags": ["python", "coding"]},
        {"tags": ["python"]},
        {"tags": ["python", "coding", "tutorial"]}
    ]
    codeflash_output = find_common_tags(articles) # 2.49μs -> 2.01μs (23.9% faster)
    # Outputs were verified to be equal to the original implementation

def test_tags_with_different_data_types():
    # Tags with different data types should only consider strings
    articles = [
        {"tags": ["python", 123]},
        {"tags": ["python", "123"]}
    ]
    codeflash_output = find_common_tags(articles) # 2.25μs -> 1.69μs (33.1% faster)
    # Outputs were verified to be equal to the original implementation

def test_performance_with_large_data():
    # Performance with large data should return the common tag
    articles = [{"tags": ["common_tag", f"tag{i}"]} for i in range(10000)]
    codeflash_output = find_common_tags(articles) # 2.24ms -> 1.46ms (54.0% faster)
    # Outputs were verified to be equal to the original implementation

def test_scalability_with_increasing_tags():
    # Scalability with increasing tags should return the common tag
    articles = [{"tags": ["common_tag"] + [f"tag{i}" for i in range(j)]} for j in range(1, 1001)]
    codeflash_output = find_common_tags(articles) # 444μs -> 308μs (44.0% faster)
    # Outputs were verified to be equal to the original implementation
#------------------------------------------------
# imports
# function to test
from __future__ import annotations

import pytest  # used for our unit tests
from codeflash.result.common_tags import find_common_tags

# unit tests

def test_empty_input_list():
    # Test with an empty list
    codeflash_output = find_common_tags([]) # 681ns -> 521ns (30.7% faster)
    # Outputs were verified to be equal to the original implementation

def test_single_article():
    # Test with a single article with tags
    codeflash_output = find_common_tags([{"tags": ["python", "coding", "development"]}]) # 1.53μs -> 1.31μs (16.8% faster)
    # Test with a single article with no tags
    codeflash_output = find_common_tags([{"tags": []}]) # 581ns -> 501ns (16.0% faster)
    # Outputs were verified to be equal to the original implementation

def test_multiple_articles_some_common_tags():
    # Test with multiple articles having some common tags
    articles = [
        {"tags": ["python", "coding", "development"]},
        {"tags": ["python", "development", "tutorial"]},
        {"tags": ["python", "development", "guide"]}
    ]
    codeflash_output = find_common_tags(articles) # 2.85μs -> 2.20μs (29.5% faster)

    articles = [
        {"tags": ["tech", "news"]},
        {"tags": ["tech", "gadgets"]},
        {"tags": ["tech", "reviews"]}
    ]
    codeflash_output = find_common_tags(articles) # 1.57μs -> 1.07μs (46.7% faster)
    # Outputs were verified to be equal to the original implementation

def test_multiple_articles_no_common_tags():
    # Test with multiple articles having no common tags
    articles = [
        {"tags": ["python", "coding"]},
        {"tags": ["development", "tutorial"]},
        {"tags": ["guide", "learning"]}
    ]
    codeflash_output = find_common_tags(articles) # 2.34μs -> 1.99μs (17.6% faster)

    articles = [
        {"tags": ["apple", "banana"]},
        {"tags": ["orange", "grape"]},
        {"tags": ["melon", "kiwi"]}
    ]
    codeflash_output = find_common_tags(articles) # 1.27μs -> 1.05μs (21.0% faster)
    # Outputs were verified to be equal to the original implementation

def test_articles_with_duplicate_tags():
    # Test with articles having duplicate tags
    articles = [
        {"tags": ["python", "python", "coding"]},
        {"tags": ["python", "development", "python"]},
        {"tags": ["python", "guide", "python"]}
    ]
    codeflash_output = find_common_tags(articles) # 2.71μs -> 2.11μs (28.0% faster)

    articles = [
        {"tags": ["tech", "tech", "news"]},
        {"tags": ["tech", "tech", "gadgets"]},
        {"tags": ["tech", "tech", "reviews"]}
    ]
    codeflash_output = find_common_tags(articles) # 1.62μs -> 1.14μs (42.1% faster)
    # Outputs were verified to be equal to the original implementation

def test_articles_with_mixed_case_tags():
    # Test with articles having mixed case tags
    articles = [
        {"tags": ["Python", "Coding"]},
        {"tags": ["python", "Development"]},
        {"tags": ["PYTHON", "Guide"]}
    ]
    codeflash_output = find_common_tags(articles) # 2.27μs -> 1.95μs (16.4% faster)

    articles = [
        {"tags": ["Tech", "News"]},
        {"tags": ["tech", "Gadgets"]},
        {"tags": ["TECH", "Reviews"]}
    ]
    codeflash_output = find_common_tags(articles) # 1.15μs -> 1.02μs (12.7% faster)
    # Outputs were verified to be equal to the original implementation

def test_articles_with_non_string_tags():
    # Test with articles having non-string tags
    articles = [
        {"tags": ["python", 123, "coding"]},
        {"tags": ["python", "development", 123]},
        {"tags": ["python", "guide", 123]}
    ]
    codeflash_output = find_common_tags(articles) # 2.92μs -> 2.22μs (31.1% faster)

    articles = [
        {"tags": [None, "news"]},
        {"tags": ["tech", None]},
        {"tags": [None, "reviews"]}
    ]
    codeflash_output = find_common_tags(articles) # 1.59μs -> 1.08μs (47.2% faster)
    # Outputs were verified to be equal to the original implementation

def test_large_scale_test_cases():
    # Test with large scale input where all tags should be common
    articles = [
        {"tags": ["tag" + str(i) for i in range(1000)]} for _ in range(100)
    ]
    expected_output = {"tag" + str(i) for i in range(1000)}
    codeflash_output = find_common_tags(articles) # 381ms -> 3.44ms (11001% faster)

    # Test with large scale input where no tags should be common
    articles = [
        {"tags": ["tag" + str(i) for i in range(1000)]} for _ in range(50)
    ] + [{"tags": ["unique_tag"]}]
    codeflash_output = find_common_tags(articles) # 188ms -> 1.70ms (11011% faster)
    # Outputs were verified to be equal to the original implementation
#------------------------------------------------
from codeflash.result.common_tags import find_common_tags

def test_find_common_tags():
    find_common_tags([{}, {}])

def test_find_common_tags_2():
    find_common_tags([])
🔎 Concolic Coverage Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic__qqqxg8q/tmpobx8jmeq/test_concolic_coverage.py::test_find_common_tags 2.04μs 1.68μs 21.4%✅
codeflash_concolic__qqqxg8q/tmpobx8jmeq/test_concolic_coverage.py::test_find_common_tags_2 722ns 430ns 67.9%✅

To test or edit this optimization locally git merge codeflash/optimize-pr755-2025-09-23T17.31.17

Suggested change
common_tags = articles[0].get("tags", [])
for article in articles[1:]:
common_tags = [tag for tag in common_tags if tag in article.get("tags", [])]
return set(common_tags)
common_tags = set(articles[0].get("tags", []))
for article in articles[1:]:
common_tags.intersection_update(article.get("tags", []))
return common_tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant