Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up function ascii_domain_regex by 60% #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Jan 24, 2025

📄 60% (0.60x) speedup for ascii_domain_regex in pydantic/v1/networks.py

⏱️ Runtime : 10.6 microseconds 6.60 microseconds (best of 244 runs)

📝 Explanation and details

Changes and Improvements.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 84 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests Details
import re
from typing import Pattern

# imports
import pytest  # used for our unit tests
from pydantic.v1.networks import ascii_domain_regex

# function to test
_ascii_domain_regex_cache = None
from pydantic.v1.networks import ascii_domain_regex

# unit tests

@pytest.mark.parametrize("domain", [
    "example.com", 
    "subdomain.example.com",
    "example.co",
    "example.company",
    "example.technology",
    "ex-ample.com",
    "ex_ample.com",
    "sub-domain.example.com",
    "sub_domain.example.com",
    "example123.com",
    "123example.com",
    "sub123.example.com",
    "example.com.",
    "subdomain.example.com.",
    "Example.Com",
    "SubDomain.Example.Com",
    "a-very-long-subdomain-name-that-is-still-valid-because-it-is-under-63-characters.example.com",
    "a.co",
    "a.b"
])
def test_valid_domains(domain):
    codeflash_output = ascii_domain_regex()

@pytest.mark.parametrize("domain", [
    "-example.com", 
    "example-.com",
    "example..com",
    "example.com-",
    "example.c",
    "example.thisisaverylongtldthatshouldnotmatch",
    "example!.com",
    "[email protected]",
    "example#.com",
    "example$.com",
    "",
])
def test_invalid_domains(domain):
    codeflash_output = ascii_domain_regex()

def test_large_scale_domains():
    codeflash_output = ascii_domain_regex()
    long_domain = "sub1.sub2.sub3.sub4.sub5.sub6.sub7.sub8.sub9.sub10.example.com"
    
    max_length_domain = (
        "a-very-long-domain-name-that-is-still-valid-because-it-is-under-63-characters."
        "a-very-long-domain-name-that-is-still-valid-because-it-is-under-63-characters."
        "a-very-long-domain-name-that-is-still-valid-because-it-is-under-63-characters."
        "a-very-long-domain-name-that-is-still-valid-because-it-is-under-63-characters.com"
    )

def test_performance():
    codeflash_output = ascii_domain_regex()
    domains = ["example.com"] * 1000 + ["invalid_domain"] * 1000
    for domain in domains:
        pattern.fullmatch(domain)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

import re
from typing import Pattern

# imports
import pytest  # used for our unit tests
from pydantic.v1.networks import ascii_domain_regex

# function to test
_ascii_domain_regex_cache = None
from pydantic.v1.networks import ascii_domain_regex

# unit tests

@pytest.mark.parametrize("domain", [
    "example.com",
    "sub.example.com",
    "example.co.uk",
    "example.abc",
    "example.abcdefg",
    "example.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk",
    "ex-ample.com",
    "ex_ample.com",
    "sub-domain.example.com",
    "sub_domain.example.com",
    "example.com.",
    "sub.example.com.",
    "Example.COM",
    "Sub.Example.CoM",
    "example123.com",
    "123example.com",
    "123.example.com",
])
def test_valid_domains(domain):
    codeflash_output = ascii_domain_regex()


@pytest.mark.parametrize("domain", [
    "-example.com",
    "example-.com",
    "example..com",
    "example.com..",
    "example..com.",
    "example.a",
    "a" * 64 + ".com",
    "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.com",
    "",
    ".",
    ".com",
    "sub.",
    "exa mple.com",
    "exa\tmple.com",
    "exa\nmple.com",
    "exámple.com",
    "例子.com",
    "пример.com",
    "exämple.com",
    "example!com",
    "example@com",
    "example#com",
    "exa!mple.com",
    "exa_mple-.com",
    " example.com",
    "example.com ",
    " example.com ",
    "\texample.com",
    "example.com\n",
    "..com",
    ".example.com",
    "example..com",
    "ExAmPlE!.CoM",
    "[email protected]",
    "example.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl",
])
def test_invalid_domains(domain):
    codeflash_output = ascii_domain_regex()


def test_large_scale_valid_domain():
    domain = "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.com"
    codeflash_output = ascii_domain_regex()


def test_large_scale_mixed_domains():
    mixed_domains = (
        "example.com invalid-domain example.co.uk exa mple.com "
        "sub.example.com example@com example.com. example..com"
    )
    codeflash_output = ascii_domain_regex()
    valid_domains = [match.group(0) for match in pattern.finditer(mixed_domains)]
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from pydantic.v1.networks import ascii_domain_regex

def test_ascii_domain_regex():
    assert ascii_domain_regex() == re.compile('(?:[_0-9a-z](?:[-_0-9a-z]{0,61}[_0-9a-z])?\\.)*?[_0-9a-z](?:[-_0-9a-z]{0,61}[_0-9a-z])?(?P<tld>\\.[a-z]{2,63})?\\.?', re.IGNORECASE)

📢 Feedback on this optimization? Discord

@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Jan 24, 2025
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 January 24, 2025 08:44
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 relnotes-fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants