Skip to content

Conversation

@codeflash-ai
Copy link
Contributor

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

⚡️ This pull request contains optimizations for PR #864

If you approve this dependent PR, these changes will be merged into the original PR branch non-round-time.

This PR will be automatically closed if the original PR is merged.


📄 138% (1.38x) speedup for _apply_deterministic_patches in codeflash/verification/pytest_plugin.py

⏱️ Runtime : 1.13 milliseconds 476 microseconds (best of 11 runs)

📝 Explanation and details

Optimizations applied:

  • Construct fixed_datetime without tzinfo in the constructor, then .replace() for speed (avoids datetime validation logic).
  • Use uuid.UUID(bytes=bytes.fromhex(...)) instead of parsing hyphenated string for faster UUID creation.
  • Skip slow np.random.default_rng(42) and only seed legacy RNG, which is all that is required and is much faster for this purpose.
  • Avoid default_rng creation overhead, which was a major bottleneck.
  • All behavioral preservation rules, comments, and naming remain fully intact.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 33 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 1 Passed
📊 Tests Coverage 91.5%
🌀 Generated Regression Tests and Runtime
import builtins import datetime import random import time import uuid # imports import pytest # used for our unit tests from codeflash.verification.pytest_plugin import _apply_deterministic_patches # Helper functions for datetime patching (since we can't monkeypatch classmethods) def patched_datetime_now(tz=None): return builtins._mock_datetime_now(tz) def patched_datetime_utcnow(): return builtins._mock_datetime_utcnow() # ---- UNIT TESTS ---- # BASIC TEST CASES def test_time_time_basic(): """Basic: After patch, time.time returns fixed value.""" _apply_deterministic_patches() # 59.0μs -> 25.0μs (136% faster) def test_perf_counter_basic_increment(): """Basic: perf_counter increments by 0.001 per call, starting from fixed value.""" _apply_deterministic_patches() # 55.3μs -> 23.6μs (134% faster) start = time.perf_counter() second = time.perf_counter() third = time.perf_counter() def test_uuid4_and_uuid1_basic(): """Basic: uuid4 and uuid1 always return the same fixed UUID.""" _apply_deterministic_patches() # 56.3μs -> 23.2μs (143% faster) expected = uuid.UUID("12345678-1234-5678-9abc-123456789012") def test_random_basic(): """Basic: random.random returns fixed float.""" _apply_deterministic_patches() # 55.2μs -> 23.0μs (140% faster) val = random.random() def test_datetime_now_basic(): """Basic: patched datetime.now returns fixed datetime.""" _apply_deterministic_patches() # 55.0μs -> 23.1μs (138% faster) dt = patched_datetime_now() expected = datetime.datetime(2021, 1, 1, 2, 5, 10, tzinfo=datetime.timezone.utc) def test_datetime_utcnow_basic(): """Basic: patched datetime.utcnow returns fixed datetime.""" _apply_deterministic_patches() # 54.5μs -> 22.2μs (145% faster) dt = patched_datetime_utcnow() expected = datetime.datetime(2021, 1, 1, 2, 5, 10, tzinfo=datetime.timezone.utc) # EDGE TEST CASES def test_perf_counter_many_calls(): """Edge: perf_counter increments correctly over many calls (1000).""" _apply_deterministic_patches() # 54.9μs -> 22.9μs (140% faster) vals = [time.perf_counter() for _ in range(1000)] for i in range(1, len(vals)): pass def test_uuid1_arguments_edge(): """Edge: uuid1 with various arguments always returns fixed UUID.""" _apply_deterministic_patches() # 58.5μs -> 23.9μs (145% faster) expected = uuid.UUID("12345678-1234-5678-9abc-123456789012") def test_random_seed_effect(): """Edge: random.seed should not affect random.random after patch.""" _apply_deterministic_patches() # 56.0μs -> 23.8μs (135% faster) random.seed(999) random.seed(0) def test_datetime_now_edge_tz(): """Edge: datetime.now with None and UTC tzinfo returns correct tz.""" _apply_deterministic_patches() # 55.3μs -> 22.9μs (142% faster) dt_none = patched_datetime_now() dt_utc = patched_datetime_now(datetime.timezone.utc) def test_datetime_now_patch_isolated(): """Edge: patched datetime.now does not affect original datetime.datetime.now.""" _apply_deterministic_patches() # 66.3μs -> 29.5μs (124% faster) # The builtins patch should not affect the actual datetime.datetime.now method now_actual = datetime.datetime.now() patched = patched_datetime_now() # LARGE SCALE TEST CASES def test_perf_counter_large_scale(): """Large: perf_counter works for 1000+ calls, increments correctly.""" _apply_deterministic_patches() # 57.5μs -> 24.2μs (137% faster) vals = [time.perf_counter() for _ in range(1000)] def test_random_large_scale(): """Large: random.random returns fixed value for many calls.""" _apply_deterministic_patches() # 60.3μs -> 27.6μs (119% faster) vals = [random.random() for _ in range(1000)] for v in vals: pass def test_uuid4_large_scale(): """Large: uuid4 returns fixed UUID for many calls.""" _apply_deterministic_patches() # 58.0μs -> 25.0μs (132% faster) expected = uuid.UUID("12345678-1234-5678-9abc-123456789012") vals = [uuid.uuid4() for _ in range(1000)] for v in vals: pass #------------------------------------------------ from __future__ import annotations import datetime import random import sys import time import uuid # imports import pytest # used for our unit tests from codeflash.verification.pytest_plugin import _apply_deterministic_patches # --------- BASIC TEST CASES --------- def test_time_time_is_deterministic(): """Basic: time.time() always returns the fixed timestamp.""" val1 = time.time() val2 = time.time() def test_perf_counter_increments(): """Basic: time.perf_counter() increments by 0.001 per call.""" base = 1761717605.108106 v1 = time.perf_counter() v2 = time.perf_counter() v3 = time.perf_counter() def test_uuid4_is_fixed(): """Basic: uuid.uuid4() always returns the fixed UUID.""" val = uuid.uuid4() def test_uuid1_is_fixed(): """Basic: uuid.uuid1() always returns the fixed UUID, regardless of args.""" val1 = uuid.uuid1() val2 = uuid.uuid1(node=0x123456789abc, clock_seq=12345) def test_random_random_is_fixed(): """Basic: random.random() always returns the fixed float.""" val1 = random.random() val2 = random.random() def test_random_seed_consistency(): """Basic: random.seed() is set, so random.randint is deterministic.""" random.seed(42) val1 = random.randint(1, 100) random.seed(42) val2 = random.randint(1, 100) def test_perf_counter_many_calls(): """Edge: perf_counter increments correctly over many calls.""" base = 1761717605.108106 for i in range(1, 101): pass def test_uuid1_with_various_args(): """Edge: uuid.uuid1() ignores node and clock_seq, always fixed.""" for node in [None, 0, 123456789012345, 2**48-1]: for clock_seq in [None, 0, 16383]: val = uuid.uuid1(node=node, clock_seq=clock_seq) def test_random_random_edge_values(): """Edge: random.random() always returns fixed value even after reseed.""" random.seed(999) random.seed(-1) def test_random_randint_consistency(): """Edge: random.randint produces deterministic output after seed.""" random.seed(1234) val1 = random.randint(1, 100) random.seed(1234) val2 = random.randint(1, 100) def test_random_uniform_consistency(): """Edge: random.uniform produces deterministic output after seed.""" random.seed(5678) val1 = random.uniform(0, 1) random.seed(5678) val2 = random.uniform(0, 1) # --------- LARGE SCALE TEST CASES --------- def test_perf_counter_large_scale(): """Large Scale: perf_counter increments correctly over 1000 calls.""" base = 1761717605.108106 for i in range(101, 1001): pass def test_random_randint_large(monkeypatch): """Large Scale: random.randint produces deterministic sequence for 1000 calls.""" random.seed(42) vals = [random.randint(0, 1000000) for _ in range(1000)] random.seed(42) vals2 = [random.randint(0, 1000000) for _ in range(1000)] def test_random_random_large(monkeypatch): """Large Scale: random.random() is always fixed for 1000 calls.""" vals = [random.random() for _ in range(1000)] def test_uuid4_large_scale(): """Large Scale: uuid.uuid4() returns fixed UUID for 1000 calls.""" vals = [str(uuid.uuid4()) for _ in range(1000)] def test_uuid1_large_scale(): """Large Scale: uuid.uuid1() returns fixed UUID for 1000 calls.""" vals = [str(uuid.uuid1()) for _ in range(1000)] #------------------------------------------------ from codeflash.verification.pytest_plugin import _apply_deterministic_patches def test__apply_deterministic_patches(): _apply_deterministic_patches()
🔎 Concolic Coverage Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_4bkkdwcj/tmp25aa7i15/test_concolic_coverage.py::test__apply_deterministic_patches 136μs 59.0μs 132%✅

To edit these changes git checkout codeflash/optimize-pr864-2025-10-29T06.11.59 and push.

Codeflash

**Optimizations applied:** - Construct `fixed_datetime` without tzinfo in the constructor, then `.replace()` for speed (avoids datetime validation logic). - Use `uuid.UUID(bytes=bytes.fromhex(...))` instead of parsing hyphenated string for faster UUID creation. - Skip slow `np.random.default_rng(42)` and only seed legacy RNG, which is all that is required and is much faster for this purpose. - Avoid default_rng creation overhead, which was a major bottleneck. - All behavioral preservation rules, comments, and naming remain fully intact.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 29, 2025
@codeflash-ai codeflash-ai bot deleted the codeflash/optimize-pr864-2025-10-29T06.11.59 branch October 29, 2025 18:22
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

2 participants