Skip to content

Conversation

codeflash-ai[bot]
Copy link

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

📄 11% (0.11x) speedup for determine_profile_session_sampling_decision in sentry_sdk/profiler/continuous_profiler.py

⏱️ Runtime : 1.51 milliseconds 1.36 milliseconds (best of 108 runs)

📝 Explanation and details

The optimized code achieves an 11% speedup through two key optimizations:

1. Single float() conversion: The original code calls float(sample_rate) every time it reaches the comparison line. The optimized version converts once and stores it in sample_rate_f, eliminating redundant type conversions.

2. Early returns for edge cases: The optimization adds explicit checks for sample_rate_f <= 0.0 and sample_rate_f >= 1.0 before calling random.random(). This avoids the expensive random number generation when the result is deterministic:

  • Values ≤ 0 always return False
  • Values ≥ 1 always return True

Performance impact by test case:

  • Best gains (14-57% faster): Cases with sample rates of 1.0, negative values, or values > 1.0 benefit most from skipping random.random()
  • Moderate gains (10-35% faster): Large-scale tests with deterministic rates (0, 1, None) show consistent improvements
  • Slight regression (7-25% slower): Cases requiring random comparison (0 < rate < 1) have minor overhead from the additional checks, but this is offset by the single float conversion

The optimization is particularly effective for applications that frequently use edge case sample rates (0, 1, or out-of-bounds values) where expensive random number generation can be completely avoided.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 7050 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import random

# imports
import pytest  # used for our unit tests
from sentry_sdk.profiler.continuous_profiler import \
    determine_profile_session_sampling_decision

# unit tests

# Basic Test Cases

def test_none_sample_rate_returns_false():
    """Test that None sample_rate returns False."""
    codeflash_output = determine_profile_session_sampling_decision(None) # 425ns -> 414ns (2.66% faster)

def test_zero_sample_rate_returns_false():
    """Test that 0.0 sample_rate returns False."""
    codeflash_output = determine_profile_session_sampling_decision(0.0) # 476ns -> 516ns (7.75% slower)

def test_zero_int_sample_rate_returns_false():
    """Test that 0 (int) sample_rate returns False."""
    codeflash_output = determine_profile_session_sampling_decision(0) # 410ns -> 442ns (7.24% slower)

def test_one_sample_rate_always_true(monkeypatch):
    """Test that sample_rate=1.0 always returns True."""
    monkeypatch.setattr(random, "random", lambda: 0.99999)
    codeflash_output = determine_profile_session_sampling_decision(1.0) # 922ns -> 805ns (14.5% faster)
    monkeypatch.setattr(random, "random", lambda: 0.0)
    codeflash_output = determine_profile_session_sampling_decision(1.0) # 468ns -> 377ns (24.1% faster)

def test_sample_rate_half(monkeypatch):
    """Test that sample_rate=0.5 returns True if random.random() < 0.5, else False."""
    monkeypatch.setattr(random, "random", lambda: 0.3)
    codeflash_output = determine_profile_session_sampling_decision(0.5) # 822ns -> 1.03μs (20.0% slower)
    monkeypatch.setattr(random, "random", lambda: 0.7)
    codeflash_output = determine_profile_session_sampling_decision(0.5) # 490ns -> 557ns (12.0% slower)

def test_sample_rate_float_string(monkeypatch):
    """Test that string float sample_rate works."""
    monkeypatch.setattr(random, "random", lambda: 0.2)
    codeflash_output = determine_profile_session_sampling_decision("0.3") # 1.57μs -> 1.74μs (9.51% slower)
    monkeypatch.setattr(random, "random", lambda: 0.4)
    codeflash_output = determine_profile_session_sampling_decision("0.3") # 627ns -> 695ns (9.78% slower)

def test_sample_rate_int_string(monkeypatch):
    """Test that string int sample_rate works."""
    monkeypatch.setattr(random, "random", lambda: 0.5)
    codeflash_output = determine_profile_session_sampling_decision("1") # 1.19μs -> 1.07μs (10.8% faster)
    monkeypatch.setattr(random, "random", lambda: 0.5)
    codeflash_output = determine_profile_session_sampling_decision("0") # 609ns -> 499ns (22.0% faster)

# Edge Test Cases

def test_negative_sample_rate(monkeypatch):
    """Test that negative sample_rate always returns False."""
    monkeypatch.setattr(random, "random", lambda: 0.0)
    codeflash_output = determine_profile_session_sampling_decision(-0.1) # 772ns -> 688ns (12.2% faster)
    monkeypatch.setattr(random, "random", lambda: 0.5)
    codeflash_output = determine_profile_session_sampling_decision(-1.0) # 444ns -> 336ns (32.1% faster)

def test_sample_rate_above_one(monkeypatch):
    """Test that sample_rate > 1.0 always returns True."""
    monkeypatch.setattr(random, "random", lambda: 0.99)
    codeflash_output = determine_profile_session_sampling_decision(1.1) # 821ns -> 735ns (11.7% faster)
    monkeypatch.setattr(random, "random", lambda: 0.0)
    codeflash_output = determine_profile_session_sampling_decision(2.0) # 445ns -> 365ns (21.9% faster)

def test_sample_rate_exact_threshold(monkeypatch):
    """Test that random.random() == sample_rate returns False."""
    monkeypatch.setattr(random, "random", lambda: 0.5)
    codeflash_output = determine_profile_session_sampling_decision(0.5) # 760ns -> 1.00μs (24.2% slower)

def test_sample_rate_just_below_threshold(monkeypatch):
    """Test that random.random() just below sample_rate returns True."""
    monkeypatch.setattr(random, "random", lambda: 0.499999)
    codeflash_output = determine_profile_session_sampling_decision(0.5) # 808ns -> 968ns (16.5% slower)

def test_sample_rate_just_above_threshold(monkeypatch):
    """Test that random.random() just above sample_rate returns False."""
    monkeypatch.setattr(random, "random", lambda: 0.500001)
    codeflash_output = determine_profile_session_sampling_decision(0.5) # 787ns -> 985ns (20.1% slower)

def test_sample_rate_non_numeric_raises():
    """Test that non-numeric sample_rate raises ValueError."""
    with pytest.raises(ValueError):
        determine_profile_session_sampling_decision("not_a_number") # 3.61μs -> 2.29μs (57.7% faster)
    with pytest.raises(TypeError):
        determine_profile_session_sampling_decision(object()) # 1.89μs -> 1.73μs (8.89% faster)

def test_sample_rate_bool_false_returns_false():
    """Test that sample_rate=False returns False."""
    codeflash_output = determine_profile_session_sampling_decision(False) # 476ns -> 457ns (4.16% faster)

def test_sample_rate_bool_true(monkeypatch):
    """Test that sample_rate=True is treated as 1.0 and always returns True."""
    monkeypatch.setattr(random, "random", lambda: 0.5)
    codeflash_output = determine_profile_session_sampling_decision(True) # 1.04μs -> 891ns (16.5% faster)

# Large Scale Test Cases

def test_large_scale_sampling_distribution(monkeypatch):
    """Test that for a large number of samples, the proportion of True is close to sample_rate."""
    # Use sample_rate = 0.25, simulate 1000 samples
    sample_rate = 0.25
    results = []
    # Use a deterministic PRNG for reproducibility
    rng = random.Random(42)
    monkeypatch.setattr(random, "random", lambda: rng.random())
    for _ in range(1000):
        results.append(determine_profile_session_sampling_decision(sample_rate))
    proportion_true = sum(results) / len(results)

def test_large_scale_sampling_zero(monkeypatch):
    """Test that for large number of samples, sample_rate=0 always returns False."""
    results = []
    for _ in range(1000):
        results.append(determine_profile_session_sampling_decision(0)) # 192μs -> 194μs (0.948% slower)

def test_large_scale_sampling_one(monkeypatch):
    """Test that for large number of samples, sample_rate=1 always returns True."""
    monkeypatch.setattr(random, "random", lambda: 0.0)
    results = []
    for _ in range(1000):
        results.append(determine_profile_session_sampling_decision(1)) # 333μs -> 278μs (19.8% faster)

def test_large_scale_sampling_none(monkeypatch):
    """Test that for large number of samples, sample_rate=None always returns False."""
    results = []
    for _ in range(1000):
        results.append(determine_profile_session_sampling_decision(None)) # 191μs -> 192μs (0.088% slower)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import random

# imports
import pytest  # used for our unit tests
from sentry_sdk.profiler.continuous_profiler import \
    determine_profile_session_sampling_decision

# unit tests

# --- Basic Test Cases ---

def test_sample_rate_none_returns_false():
    """Test that None as sample_rate always returns False."""
    codeflash_output = determine_profile_session_sampling_decision(None) # 475ns -> 552ns (13.9% slower)

def test_sample_rate_zero_returns_false():
    """Test that 0.0 as sample_rate always returns False."""
    codeflash_output = determine_profile_session_sampling_decision(0.0) # 493ns -> 546ns (9.71% slower)

def test_sample_rate_one_returns_true():
    """Test that 1.0 as sample_rate always returns True."""
    # Patch random.random to always return 0.999 (less than 1.0)
    orig_random = random.random
    random.random = lambda: 0.999
    try:
        codeflash_output = determine_profile_session_sampling_decision(1.0)
    finally:
        random.random = orig_random

def test_sample_rate_half_returns_true_or_false():
    """Test that 0.5 as sample_rate can return either True or False."""
    # Patch random.random to return 0.3 (less than 0.5)
    orig_random = random.random
    random.random = lambda: 0.3
    try:
        codeflash_output = determine_profile_session_sampling_decision(0.5)
    finally:
        random.random = orig_random

    # Patch random.random to return 0.7 (greater than 0.5)
    random.random = lambda: 0.7
    try:
        codeflash_output = determine_profile_session_sampling_decision(0.5)
    finally:
        random.random = orig_random

def test_sample_rate_as_string():
    """Test that string input is correctly converted to float."""
    orig_random = random.random
    random.random = lambda: 0.1
    try:
        codeflash_output = determine_profile_session_sampling_decision("0.2")
        codeflash_output = determine_profile_session_sampling_decision("0.05")
    finally:
        random.random = orig_random

def test_sample_rate_as_int():
    """Test that integer input is correctly converted to float."""
    orig_random = random.random
    random.random = lambda: 0.5
    try:
        codeflash_output = determine_profile_session_sampling_decision(1)
        codeflash_output = determine_profile_session_sampling_decision(0)
    finally:
        random.random = orig_random

# --- Edge Test Cases ---

def test_sample_rate_negative():
    """Test that negative sample_rate always returns False."""
    codeflash_output = determine_profile_session_sampling_decision(-0.1) # 2.07μs -> 721ns (187% faster)
    codeflash_output = determine_profile_session_sampling_decision("-1") # 1.19μs -> 965ns (23.6% faster)

def test_sample_rate_greater_than_one():
    """Test that sample_rate > 1 always returns True."""
    orig_random = random.random
    random.random = lambda: 0.9999
    try:
        codeflash_output = determine_profile_session_sampling_decision(1.1)
        codeflash_output = determine_profile_session_sampling_decision("2.0")
    finally:
        random.random = orig_random

def test_sample_rate_very_close_to_zero():
    """Test that sample_rate just above zero only returns True if random.random < sample_rate."""
    orig_random = random.random
    # random.random returns 0.00001, sample_rate is 0.00002
    random.random = lambda: 0.00001
    try:
        codeflash_output = determine_profile_session_sampling_decision(0.00002)
    finally:
        random.random = orig_random

    # random.random returns 0.00003, sample_rate is 0.00002
    random.random = lambda: 0.00003
    try:
        codeflash_output = determine_profile_session_sampling_decision(0.00002)
    finally:
        random.random = orig_random

def test_sample_rate_very_close_to_one():
    """Test that sample_rate just below one returns True if random.random < sample_rate."""
    orig_random = random.random
    # random.random returns 0.99998, sample_rate is 0.99999
    random.random = lambda: 0.99998
    try:
        codeflash_output = determine_profile_session_sampling_decision(0.99999)
    finally:
        random.random = orig_random

    # random.random returns 0.999999, sample_rate is 0.99999
    random.random = lambda: 0.999999
    try:
        codeflash_output = determine_profile_session_sampling_decision(0.99999)
    finally:
        random.random = orig_random

def test_sample_rate_empty_string():
    """Test that empty string as sample_rate returns False."""
    codeflash_output = determine_profile_session_sampling_decision("") # 389ns -> 448ns (13.2% slower)

def test_sample_rate_zero_string():
    """Test that '0' as sample_rate returns False."""
    codeflash_output = determine_profile_session_sampling_decision("0") # 2.38μs -> 1.05μs (126% faster)
    codeflash_output = determine_profile_session_sampling_decision("0.0") # 680ns -> 505ns (34.7% faster)

def test_sample_rate_none_or_false():
    """Test that False as sample_rate returns False."""
    codeflash_output = determine_profile_session_sampling_decision(False) # 419ns -> 421ns (0.475% slower)

def test_sample_rate_non_numeric_string():
    """Test that non-numeric string raises ValueError."""
    with pytest.raises(ValueError):
        determine_profile_session_sampling_decision("abc") # 3.64μs -> 2.55μs (43.0% faster)

def test_sample_rate_nan():
    """Test that float('nan') as sample_rate returns False."""
    import math
    codeflash_output = determine_profile_session_sampling_decision(float('nan')) # 1.85μs -> 2.38μs (22.2% slower)

def test_sample_rate_inf():
    """Test that float('inf') as sample_rate always returns True."""
    orig_random = random.random
    random.random = lambda: 0.99999
    try:
        codeflash_output = determine_profile_session_sampling_decision(float('inf'))
    finally:
        random.random = orig_random

def test_sample_rate_minus_inf():
    """Test that float('-inf') as sample_rate returns False."""
    codeflash_output = determine_profile_session_sampling_decision(float('-inf')) # 1.07μs -> 674ns (58.2% faster)

# --- Large Scale Test Cases ---

def test_large_scale_sampling_distribution():
    """Test that over many samples, the proportion of True is close to sample_rate."""
    sample_rate = 0.25
    trials = 1000
    true_count = 0
    for _ in range(trials):
        if determine_profile_session_sampling_decision(sample_rate):
            true_count += 1
    # Acceptable error margin: 5%
    observed_rate = true_count / trials

def test_large_scale_sampling_all_true():
    """Test that over many samples with sample_rate=1.0, all are True."""
    sample_rate = 1.0
    trials = 1000
    for _ in range(trials):
        codeflash_output = determine_profile_session_sampling_decision(sample_rate) # 358μs -> 266μs (34.6% faster)

def test_large_scale_sampling_all_false():
    """Test that over many samples with sample_rate=0.0, all are False."""
    sample_rate = 0.0
    trials = 1000
    for _ in range(trials):
        codeflash_output = determine_profile_session_sampling_decision(sample_rate) # 195μs -> 194μs (0.536% faster)

def test_large_scale_sampling_all_none():
    """Test that over many samples with sample_rate=None, all are False."""
    trials = 1000
    for _ in range(trials):
        codeflash_output = determine_profile_session_sampling_decision(None) # 194μs -> 191μs (1.37% faster)

def test_large_scale_sampling_edge_rates():
    """Test large scale with edge rates near 0 and 1."""
    trials = 1000

    # Near zero
    sample_rate = 0.001
    true_count = sum(determine_profile_session_sampling_decision(sample_rate) for _ in range(trials)) # 1.63μs -> 1.93μs (15.6% slower)

    # Near one
    sample_rate = 0.999
    true_count = sum(determine_profile_session_sampling_decision(sample_rate) for _ in range(trials)) # 459ns -> 619ns (25.8% slower)
# 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-determine_profile_session_sampling_decision-mg9ix8mz and push.

Codeflash

The optimized code achieves an 11% speedup through two key optimizations:

**1. Single float() conversion:** The original code calls `float(sample_rate)` every time it reaches the comparison line. The optimized version converts once and stores it in `sample_rate_f`, eliminating redundant type conversions.

**2. Early returns for edge cases:** The optimization adds explicit checks for `sample_rate_f <= 0.0` and `sample_rate_f >= 1.0` before calling `random.random()`. This avoids the expensive random number generation when the result is deterministic:
- Values ≤ 0 always return False
- Values ≥ 1 always return True

**Performance impact by test case:**
- **Best gains** (14-57% faster): Cases with sample rates of 1.0, negative values, or values > 1.0 benefit most from skipping `random.random()`
- **Moderate gains** (10-35% faster): Large-scale tests with deterministic rates (0, 1, None) show consistent improvements
- **Slight regression** (7-25% slower): Cases requiring random comparison (0 < rate < 1) have minor overhead from the additional checks, but this is offset by the single float conversion

The optimization is particularly effective for applications that frequently use edge case sample rates (0, 1, or out-of-bounds values) where expensive random number generation can be completely avoided.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 2, 2025 14:41
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 2, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants