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 address_in_network by 79% #21

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 Dec 22, 2024

📄 79% (0.79x) speedup for address_in_network in src/requests/utils.py

⏱️ Runtime : 2.84 milliseconds 1.59 millisecond (best of 31 runs)

📝 Explanation and details

Here's the optimized version of the given Python program.

Explanation of Optimizations.

  1. Avoid Redundant Struct Unpacking.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 56 Passed
🌀 Generated Regression Tests 552 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests Details
- test_utils.py
🌀 Generated Regression Tests Details
import socket
import struct

# imports
import pytest  # used for our unit tests
from src.requests.utils import address_in_network


# unit tests
def test_basic_functionality():
    # Single IP in a small subnet
    codeflash_output = address_in_network("192.168.1.1", "192.168.1.0/24")
    codeflash_output = address_in_network("192.168.1.1", "192.168.2.0/24")

    # Single IP in a large subnet
    codeflash_output = address_in_network("10.0.0.1", "10.0.0.0/8")
    codeflash_output = address_in_network("172.16.0.1", "192.168.0.0/16")

def test_edge_cases():
    # Boundary IP addresses
    codeflash_output = address_in_network("192.168.1.0", "192.168.1.0/24")
    codeflash_output = address_in_network("192.168.1.255", "192.168.1.0/24")
    codeflash_output = address_in_network("192.168.1.0", "192.168.0.0/23")
    codeflash_output = address_in_network("192.168.1.255", "192.168.0.0/23")

    # Single IP with /32 subnet (host address)
    codeflash_output = address_in_network("192.168.1.1", "192.168.1.1/32")
    codeflash_output = address_in_network("192.168.1.2", "192.168.1.1/32")


def test_large_scale():
    # Large subnets
    codeflash_output = address_in_network("10.0.0.1", "10.0.0.0/8")
    codeflash_output = address_in_network("10.255.255.255", "10.0.0.0/8")
    codeflash_output = address_in_network("11.0.0.1", "10.0.0.0/8")

    # Performance with many IPs
    for i in range(256):
        ip = f"10.0.0.{i}"
        codeflash_output = address_in_network(ip, "10.0.0.0/8")

def test_special_cases():
    # Loopback addresses
    codeflash_output = address_in_network("127.0.0.1", "127.0.0.0/8")
    codeflash_output = address_in_network("127.0.0.1", "192.168.1.0/24")

    # Link-local addresses
    codeflash_output = address_in_network("169.254.1.1", "169.254.0.0/16")
    codeflash_output = address_in_network("169.254.1.1", "192.168.1.0/24")

# Run the tests
if __name__ == "__main__":
    pytest.main()
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

import socket
import struct

# imports
import pytest  # used for our unit tests
from src.requests.utils import address_in_network


# unit tests
def test_basic_functionality():
    # IP within the subnet
    codeflash_output = address_in_network('192.168.1.1', '192.168.1.0/24')
    codeflash_output = address_in_network('10.0.0.5', '10.0.0.0/8')

    # IP outside the subnet
    codeflash_output = address_in_network('192.168.2.1', '192.168.1.0/24')
    codeflash_output = address_in_network('172.16.0.1', '10.0.0.0/8')

def test_edge_cases():
    # Boundary IP addresses
    codeflash_output = address_in_network('192.168.1.0', '192.168.1.0/24')
    codeflash_output = address_in_network('192.168.1.255', '192.168.1.0/24')

    # Single IP subnet
    codeflash_output = address_in_network('192.168.1.1', '192.168.1.1/32')
    codeflash_output = address_in_network('192.168.1.2', '192.168.1.1/32')


def test_large_scale():
    # Large subnets
    codeflash_output = address_in_network('10.0.0.1', '10.0.0.0/8')
    codeflash_output = address_in_network('10.255.255.255', '10.0.0.0/8')

    # Performance with large data samples
    for i in range(256):
        ip = f'192.168.1.{i}'
        codeflash_output = address_in_network(ip, '192.168.1.0/24')

def test_special_cases():
    # Loopback address
    codeflash_output = address_in_network('127.0.0.1', '127.0.0.0/8')
    codeflash_output = address_in_network('127.0.0.1', '192.168.1.0/24')

    # Broadcast address
    codeflash_output = address_in_network('255.255.255.255', '0.0.0.0/0')
    codeflash_output = address_in_network('255.255.255.255', '192.168.1.0/24')

def test_mixed_validity():
    # IP address at the edge of subnet boundary
    codeflash_output = address_in_network('192.168.1.255', '192.168.1.128/25')
    codeflash_output = address_in_network('192.168.1.127', '192.168.1.128/25')

    # Subnet with varying prefix lengths
    codeflash_output = address_in_network('192.168.1.1', '192.168.1.0/30')
    codeflash_output = address_in_network('192.168.1.4', '192.168.1.0/30')

if __name__ == "__main__":
    pytest.main()
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

📢 Feedback on this optimization? Discord

Here's the optimized version of the given Python program.

### Explanation of Optimizations.

2. **Avoid Redundant Struct Unpacking**.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 22, 2024
@codeflash-ai codeflash-ai bot requested a review from alvin-r December 22, 2024 16:19
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