Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Jun 14, 2024

📄 _sanitize_tag_value() in sentry_sdk/metrics.py

📈 Performance improved by 176% (1.76x faster)

⏱️ Runtime went down from 689 microseconds to 250 microseconds

Explanation and details

Here's an optimized version of the given program.

By using chained replace calls, we avoid the overhead of creating a translation table and using the translate method, which slightly improves the performance, especially for shorter strings.

Correctness verification

The new optimized code was tested for correctness. The results are listed below.

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 24 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
import pytest  # used for our unit tests
from sentry_sdk.metrics import _sanitize_tag_value

# unit tests

# Basic Functionality
def test_basic_no_special_chars():
    assert _sanitize_tag_value("hello") == "hello"
    assert _sanitize_tag_value("world") == "world"

def test_basic_with_special_chars():
    assert _sanitize_tag_value("hello\nworld") == "hello\\nworld"
    assert _sanitize_tag_value("hello\rworld") == "hello\\rworld"
    assert _sanitize_tag_value("hello\tworld") == "hello\\tworld"
    assert _sanitize_tag_value("hello\\world") == "hello\\\\world"
    assert _sanitize_tag_value("hello|world") == "hello\\u{7c}world"
    assert _sanitize_tag_value("hello,world") == "hello\\u{2c}world"

# Edge Cases
def test_empty_string():
    assert _sanitize_tag_value("") == ""

def test_only_special_chars():
    assert _sanitize_tag_value("\n\r\t\\|,") == "\\n\\r\\t\\\\\\u{7c}\\u{2c}"

def test_repeated_special_chars():
    assert _sanitize_tag_value("\\n\\n") == "\\\\n\\\\n"
    assert _sanitize_tag_value("||") == "\\u{7c}\\u{7c}"

# Mixed Content
def test_mixed_alphanumeric_special_chars():
    assert _sanitize_tag_value("abc\n123\rxyz\t456\\789|0,a") == "abc\\n123\\rxyz\\t456\\\\789\\u{7c}0\\u{2c}a"

def test_unicode_characters():
    assert _sanitize_tag_value("hello\u1234world") == "hello\u1234world"

# Performance and Scalability
def test_large_string_no_special_chars():
    assert _sanitize_tag_value("a" * 10000) == "a" * 10000

def test_large_string_with_special_chars():
    assert _sanitize_tag_value("\n" * 10000) == "\\n" * 10000

# Invalid Input Handling
def test_non_string_input():
    with pytest.raises(TypeError):
        _sanitize_tag_value(12345)
    with pytest.raises(TypeError):
        _sanitize_tag_value([1, 2, 3])
    with pytest.raises(TypeError):
        _sanitize_tag_value({"key": "value"})

# Boundary Conditions
def test_boundary_special_chars():
    assert _sanitize_tag_value("\nstart") == "\\nstart"
    assert _sanitize_tag_value("end\n") == "end\\n"
    assert _sanitize_tag_value("|start,end|") == "\\u{7c}start\\u{2c}end\\u{7c}"

# Complex Cases
def test_nested_special_chars():
    assert _sanitize_tag_value("hello\\nworld|hello,world") == "hello\\\\nworld\\u{7c}hello\\u{2c}world"

def test_escape_sequences():
    assert _sanitize_tag_value("hello\\nworld") == "hello\\\\nworld"
    assert _sanitize_tag_value("hello\\rworld") == "hello\\\\rworld"

# Real-World Examples
def test_file_paths():
    assert _sanitize_tag_value("C:\\Users\\Name\\Documents") == "C:\\\\Users\\\\Name\\\\Documents"

def test_urls():
    assert _sanitize_tag_value("https://example.com/path?query=value") == "https://example.com/path?query=value"

🔘 (none found) − ⏪ Replay Tests

Here's an optimized version of the given program.



By using chained `replace` calls, we avoid the overhead of creating a translation table and using the `translate` method, which slightly improves the performance, especially for shorter strings.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Jun 14, 2024
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 June 14, 2024 01:47
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants