Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 30% (0.30x) speedup for serialize_datetime in src/deepgram/core/datetime_utils.py

⏱️ Runtime : 3.58 milliseconds 2.75 milliseconds (best of 195 runs)

📝 Explanation and details

Key optimizations:

  • Caches the local timezone object on first usage, avoiding unnecessary calls to dt.datetime.now().astimezone().tzinfo.
  • Caches the computation of UTC tzname for faster comparisons.
  • Optimizes "Z" replacement using str.endswith and slicing, which is faster than str.replace when the string likely ends with "+00:00".
  • All behavioral requirements and comments are carefully preserved.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 1849 Passed
⏪ Replay Tests 6 Passed
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import datetime as dt

# imports
import pytest  # used for our unit tests
from deepgram.core.datetime_utils import serialize_datetime

# unit tests

# ------------------- Basic Test Cases -------------------

def test_serialize_utc_datetime():
    # Test serialization of a UTC datetime
    d = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=dt.timezone.utc)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 5.13μs -> 4.94μs (3.87% faster)

def test_serialize_non_utc_positive_offset():
    # Test serialization of a datetime with +05:00 offset
    tz = dt.timezone(dt.timedelta(hours=5))
    d = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 4.29μs -> 4.25μs (0.847% faster)

def test_serialize_non_utc_negative_offset():
    # Test serialization of a datetime with -07:00 offset
    tz = dt.timezone(dt.timedelta(hours=-7))
    d = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 4.31μs -> 4.11μs (4.84% faster)

def test_serialize_naive_datetime():
    # Test serialization of a naive datetime (no tzinfo)
    # Should use local timezone
    d = dt.datetime(2024, 6, 1, 12, 30, 45)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 12.5μs -> 6.57μs (89.4% faster)

# ------------------- Edge Test Cases -------------------

def test_serialize_datetime_with_microseconds():
    # Test serialization with microseconds
    tz = dt.timezone.utc
    d = dt.datetime(2024, 6, 1, 12, 30, 45, 123456, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 4.32μs -> 4.15μs (3.95% faster)

def test_serialize_datetime_min_value():
    # Test serialization of the earliest possible datetime
    tz = dt.timezone.utc
    d = dt.datetime.min.replace(tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 4.02μs -> 4.02μs (0.075% slower)

def test_serialize_datetime_max_value():
    # Test serialization of the latest possible datetime
    tz = dt.timezone.utc
    d = dt.datetime.max.replace(tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 4.04μs -> 4.19μs (3.51% slower)

def test_serialize_datetime_without_seconds():
    # Test serialization of a datetime with zero seconds
    tz = dt.timezone.utc
    d = dt.datetime(2024, 6, 1, 12, 30, 0, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 3.98μs -> 4.16μs (4.35% slower)

def test_serialize_datetime_dst_transition():
    # Test serialization of a datetime during DST transition (simulate with a tz offset)
    # Python's stdlib doesn't provide DST-aware timezones, so we use fixed offset
    tz = dt.timezone(dt.timedelta(hours=2))
    d = dt.datetime(2024, 3, 31, 2, 30, 0, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 4.06μs -> 4.07μs (0.295% slower)

def test_serialize_datetime_zero_offset():
    # Test serialization of a datetime with offset +00:00 but not UTC tzinfo
    tz = dt.timezone(dt.timedelta(hours=0))
    d = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 4.00μs -> 4.00μs (0.200% faster)

def test_serialize_datetime_subsecond_precision():
    # Test serialization with microseconds and verify correct placement
    tz = dt.timezone.utc
    d = dt.datetime(2024, 6, 1, 12, 30, 45, 1, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 3.99μs -> 4.14μs (3.58% slower)

def test_serialize_datetime_with_different_tzname():
    # Test that a timezone with offset 0 but different tzname is not treated as UTC
    class FakeTZ(dt.tzinfo):
        def utcoffset(self, dtval): return dt.timedelta(0)
        def tzname(self, dtval): return "Fake"
        def dst(self, dtval): return None
    tz = FakeTZ()
    d = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 4.96μs -> 4.89μs (1.55% faster)

def test_serialize_datetime_leap_year():
    # Test serialization on a leap day
    tz = dt.timezone.utc
    d = dt.datetime(2024, 2, 29, 23, 59, 59, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 4.08μs -> 4.10μs (0.634% slower)

def test_serialize_datetime_leap_second():
    # Python datetime does not support leap seconds, but test for 23:59:59
    tz = dt.timezone.utc
    d = dt.datetime(2024, 6, 30, 23, 59, 59, tzinfo=tz)
    codeflash_output = serialize_datetime(d); result = codeflash_output # 3.96μs -> 3.91μs (1.31% faster)

# ------------------- Large Scale Test Cases -------------------





def test_serialize_datetime_wrong_type():
    # Test that passing a non-datetime raises an AttributeError
    with pytest.raises(AttributeError):
        serialize_datetime("2024-06-01T12:30:45Z") # 1.82μs -> 1.92μs (4.85% slower)

def test_serialize_datetime_none():
    # Test that passing None raises an AttributeError
    with pytest.raises(AttributeError):
        serialize_datetime(None) # 1.39μs -> 1.46μs (4.47% slower)

def test_serialize_datetime_date_object():
    # Test that passing a date (not datetime) raises an AttributeError
    with pytest.raises(AttributeError):
        serialize_datetime(dt.date(2024, 6, 1)) # 1.51μs -> 1.41μs (7.16% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import datetime as dt

# imports
import pytest  # used for our unit tests
from deepgram.core.datetime_utils import serialize_datetime

# unit tests

# --- Basic Test Cases ---

def test_serialize_datetime_utc():
    # Test serialization of UTC datetime
    dt_utc = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=dt.timezone.utc)
    codeflash_output = serialize_datetime(dt_utc); result = codeflash_output # 6.78μs -> 6.94μs (2.32% slower)

def test_serialize_datetime_non_utc_positive_offset():
    # Test serialization of datetime with positive offset (+05:00)
    tz_offset = dt.timezone(dt.timedelta(hours=5))
    dt_pos = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=tz_offset)
    codeflash_output = serialize_datetime(dt_pos); result = codeflash_output # 4.47μs -> 4.61μs (2.95% slower)

def test_serialize_datetime_non_utc_negative_offset():
    # Test serialization of datetime with negative offset (-07:00)
    tz_offset = dt.timezone(dt.timedelta(hours=-7))
    dt_neg = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=tz_offset)
    codeflash_output = serialize_datetime(dt_neg); result = codeflash_output # 4.29μs -> 4.37μs (1.85% slower)

def test_serialize_datetime_naive_datetime():
    # Test serialization of naive datetime (no tzinfo)
    # Should use local timezone
    naive_dt = dt.datetime(2024, 6, 1, 12, 30, 45)
    codeflash_output = serialize_datetime(naive_dt); result = codeflash_output # 12.9μs -> 6.88μs (87.9% faster)
    # The result should be ISO format with local offset
    # We reconstruct expected value using local tzinfo
    local_tz = dt.datetime.now().astimezone().tzinfo
    localized_dt = naive_dt.replace(tzinfo=local_tz)
    expected = localized_dt.isoformat()

def test_serialize_datetime_with_microseconds():
    # Test serialization of datetime with microseconds
    tz_offset = dt.timezone(dt.timedelta(hours=2))
    dt_micro = dt.datetime(2024, 6, 1, 12, 30, 45, 123456, tzinfo=tz_offset)
    codeflash_output = serialize_datetime(dt_micro); result = codeflash_output # 4.06μs -> 4.08μs (0.563% slower)

# --- Edge Test Cases ---

def test_serialize_datetime_min_value_utc():
    # Test serialization of minimum datetime value with UTC
    min_dt = dt.datetime.min.replace(tzinfo=dt.timezone.utc)
    codeflash_output = serialize_datetime(min_dt); result = codeflash_output # 4.06μs -> 3.99μs (1.65% faster)

def test_serialize_datetime_max_value_non_utc():
    # Test serialization of maximum datetime value with non-UTC offset
    tz_offset = dt.timezone(dt.timedelta(hours=14))
    max_dt = dt.datetime.max.replace(tzinfo=tz_offset)
    codeflash_output = serialize_datetime(max_dt); result = codeflash_output # 3.80μs -> 4.00μs (4.92% slower)

def test_serialize_datetime_dst_transition():
    # Test serialization of datetime during DST transition (simulate using fixed offset)
    # E.g., US Eastern Time: UTC-5 or UTC-4 (simulate with fixed offsets)
    tz_standard = dt.timezone(dt.timedelta(hours=-5))
    tz_dst = dt.timezone(dt.timedelta(hours=-4))
    dt_standard = dt.datetime(2024, 11, 3, 1, 30, 0, tzinfo=tz_standard)  # Standard time
    dt_dst = dt.datetime(2024, 3, 10, 2, 30, 0, tzinfo=tz_dst)  # DST time
    codeflash_output = serialize_datetime(dt_standard); result_standard = codeflash_output # 4.07μs -> 4.01μs (1.47% faster)
    codeflash_output = serialize_datetime(dt_dst); result_dst = codeflash_output # 1.89μs -> 1.87μs (0.855% faster)

def test_serialize_datetime_zero_offset_not_utc():
    # Test serialization of datetime with zero offset but not UTC tzinfo object
    zero_offset = dt.timezone(dt.timedelta(0), name="NonUTCZero")
    dt_zero = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=zero_offset)
    codeflash_output = serialize_datetime(dt_zero); result = codeflash_output # 3.29μs -> 3.35μs (2.03% slower)

def test_serialize_datetime_invalid_type():
    # Test passing an invalid type (not a datetime object)
    with pytest.raises(AttributeError):
        serialize_datetime("2024-06-01T12:30:45Z") # 1.34μs -> 1.40μs (3.72% slower)

def test_serialize_datetime_none_input():
    # Test passing None as input
    with pytest.raises(AttributeError):
        serialize_datetime(None) # 1.37μs -> 1.35μs (1.78% faster)

# --- Large Scale Test Cases ---


def test_serialize_datetime_large_batch_naive():
    # Test serialization of a large batch of naive datetimes
    base_dt = dt.datetime(2024, 6, 1, 0, 0, 0)
    datetimes = [base_dt.replace(hour=h, minute=m) for h in range(24) for m in range(0, 60, 2)]
    # Should be 24*30 = 720 datetimes
    local_tz = dt.datetime.now().astimezone().tzinfo
    for d in datetimes:
        codeflash_output = serialize_datetime(d); result = codeflash_output # 2.08ms -> 1.29ms (61.2% faster)
        expected = d.replace(tzinfo=local_tz).isoformat()

def test_serialize_datetime_performance_many_calls():
    # Test performance by serializing 1000 UTC datetimes
    # Should not raise or timeout
    dt_utc = dt.datetime(2024, 6, 1, 12, 30, 45, tzinfo=dt.timezone.utc)
    for i in range(1000):
        codeflash_output = serialize_datetime(dt_utc); result = codeflash_output # 1.18ms -> 1.17ms (0.721% faster)

def test_serialize_datetime_microsecond_precision_large_batch():
    # Test serialization of datetimes with microsecond precision in a large batch
    tz_offset = dt.timezone(dt.timedelta(hours=1))
    for us in range(0, 1000000, 10000):  # 0 to 999000, step 10000 (100 values)
        dt_obj = dt.datetime(2024, 6, 1, 12, 30, 45, us, tzinfo=tz_offset)
        codeflash_output = serialize_datetime(dt_obj); result = codeflash_output # 137μs -> 136μs (1.32% faster)
        expected = dt_obj.isoformat()
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
⏪ Replay Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_pytest_testsintegrationstest_integration_scenarios_py_testsunittest_core_utils_py_testsutilstest_htt__replay_test_0.py::test_deepgram_core_datetime_utils_serialize_datetime 33.0μs 21.9μs 50.3%✅
test_pytest_testsintegrationstest_manage_client_py_testsunittest_core_query_encoder_py_testsunittest_type__replay_test_0.py::test_deepgram_core_datetime_utils_serialize_datetime 25.0μs 15.9μs 57.2%✅

To edit these changes git checkout codeflash/optimize-serialize_datetime-mh2qlbcu and push.

Codeflash

**Key optimizations:**
- *Caches the local timezone object* on first usage, avoiding unnecessary calls to `dt.datetime.now().astimezone().tzinfo`.
- *Caches the computation of UTC tzname* for faster comparisons.
- *Optimizes `"Z"` replacement* using `str.endswith` and slicing, which is faster than `str.replace` when the string likely ends with "+00:00".
- All behavioral requirements and comments are carefully preserved.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 23, 2025 01:22
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 23, 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.

1 participant