Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 34% (0.34x) speedup for get_prediction_aip_http_port in google/cloud/aiplatform/utils/prediction_utils.py

⏱️ Runtime : 34.5 microseconds 25.8 microseconds (best of 125 runs)

📝 Explanation and details

The optimization replaces the explicit None check and length validation with a single truthiness test. The original code uses serving_container_ports is not None and len(serving_container_ports) > 0, which requires two separate evaluations: a None check and a length calculation. The optimized version uses if serving_container_ports:, which leverages Python's truthiness rules where empty sequences (lists, tuples) and None both evaluate to False.

This change eliminates the len() function call, which has overhead even for small sequences, and reduces the boolean expression from two operations to one. The line profiler shows the conditional check time dropped from 64,650ns to 40,013ns (38% reduction), contributing significantly to the overall 33% speedup.

The optimization is particularly effective for:

  • Empty containers (lists, tuples): 47-58% faster as shown in tests, since no length calculation is needed
  • Large sequences: 51-68% faster improvements, as len() overhead becomes more significant with larger containers
  • None inputs: 11-21% faster, eliminating the explicit None comparison

The truthiness approach is idiomatic Python and maintains identical behavior - both None and empty sequences return the default port, while non-empty sequences return their first element.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 58 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from typing import Optional, Sequence

# imports
import pytest  # used for our unit tests
from aiplatform.utils.prediction_utils import get_prediction_aip_http_port

# function to test
# -*- coding: utf-8 -*-

# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


# Simulate the google.cloud.aiplatform.constants.prediction.DEFAULT_AIP_HTTP_PORT
class prediction:
    DEFAULT_AIP_HTTP_PORT = 8080
from aiplatform.utils.prediction_utils import get_prediction_aip_http_port

# unit tests

# ----------- BASIC TEST CASES -----------

def test_none_returns_default():
    # Test with None input returns default port
    codeflash_output = get_prediction_aip_http_port(None) # 401ns -> 335ns (19.7% faster)

def test_empty_list_returns_default():
    # Test with empty list returns default port
    codeflash_output = get_prediction_aip_http_port([]) # 540ns -> 356ns (51.7% faster)

def test_single_port_list_returns_first():
    # Test with single port returns that port
    codeflash_output = get_prediction_aip_http_port([9000]) # 468ns -> 333ns (40.5% faster)

def test_multiple_ports_returns_first():
    # Test with multiple ports returns the first one
    codeflash_output = get_prediction_aip_http_port([1234, 5678, 9101]) # 444ns -> 323ns (37.5% faster)

def test_tuple_input():
    # Test with tuple input returns first element
    codeflash_output = get_prediction_aip_http_port((4321, 8765)) # 502ns -> 331ns (51.7% faster)

def test_default_port_constant():
    # Test that the default port constant is used
    codeflash_output = get_prediction_aip_http_port() # 530ns -> 475ns (11.6% faster)

# ----------- EDGE TEST CASES -----------

def test_zero_port():
    # Test with port 0 (valid port value)
    codeflash_output = get_prediction_aip_http_port([0]) # 467ns -> 321ns (45.5% faster)

def test_negative_port():
    # Test with negative port (unusual, but function should not validate)
    codeflash_output = get_prediction_aip_http_port([-1, 8080]) # 459ns -> 306ns (50.0% faster)

def test_large_port_number():
    # Test with very large port number (above typical port range)
    codeflash_output = get_prediction_aip_http_port([65536]) # 427ns -> 288ns (48.3% faster)



def test_mixed_type_ports():
    # Test with mixed types in list, first is int, rest are not
    codeflash_output = get_prediction_aip_http_port([8081, '8082', None]) # 634ns -> 456ns (39.0% faster)



def test_input_is_set():
    # Test with set input, should raise TypeError because sets are unordered
    with pytest.raises(TypeError):
        get_prediction_aip_http_port({8080, 9000}) # 1.30μs -> 1.13μs (15.0% faster)




def test_input_is_object():
    # Test with object input, should raise TypeError
    with pytest.raises(TypeError):
        get_prediction_aip_http_port(object()) # 1.20μs -> 1.05μs (14.1% faster)

def test_input_is_bool():
    # Test with bool input, should raise TypeError
    with pytest.raises(TypeError):
        get_prediction_aip_http_port(True) # 1.14μs -> 970ns (17.2% faster)

def test_input_is_int():
    # Test with int input, should raise TypeError
    with pytest.raises(TypeError):
        get_prediction_aip_http_port(8080) # 1.06μs -> 924ns (15.3% faster)

def test_input_is_float():
    # Test with float input, should raise TypeError
    with pytest.raises(TypeError):
        get_prediction_aip_http_port(8080.0) # 1.08μs -> 1.00μs (7.86% faster)

def test_input_is_generator():
    # Test with generator input, should raise TypeError
    def port_gen():
        yield 8080
    with pytest.raises(TypeError):
        get_prediction_aip_http_port(port_gen()) # 1.02μs -> 906ns (12.5% faster)

def test_input_is_range():
    # Test with range input, should work as range is a Sequence
    codeflash_output = get_prediction_aip_http_port(range(9000, 9002)) # 838ns -> 702ns (19.4% faster)

def test_input_is_frozenset():
    # Test with frozenset input, should raise TypeError
    with pytest.raises(TypeError):
        get_prediction_aip_http_port(frozenset([8080, 9000])) # 1.09μs -> 939ns (16.2% faster)


def test_input_is_empty_tuple():
    # Test with empty tuple returns default port
    codeflash_output = get_prediction_aip_http_port(()) # 885ns -> 680ns (30.1% faster)

# ----------- LARGE SCALE TEST CASES -----------

def test_large_list_returns_first():
    # Test with large list, returns first element
    large_ports = list(range(1000, 2000))
    codeflash_output = get_prediction_aip_http_port(large_ports) # 585ns -> 367ns (59.4% faster)

def test_large_tuple_returns_first():
    # Test with large tuple, returns first element
    large_ports = tuple(range(2000, 3000))
    codeflash_output = get_prediction_aip_http_port(large_ports) # 578ns -> 370ns (56.2% faster)

def test_large_range_returns_first():
    # Test with large range, returns first element
    codeflash_output = get_prediction_aip_http_port(range(3000, 4000)) # 770ns -> 679ns (13.4% faster)

def test_large_list_of_same_port():
    # Test with large list of same port, returns that port
    ports = [8081] * 1000
    codeflash_output = get_prediction_aip_http_port(ports) # 538ns -> 325ns (65.5% faster)





def test_large_list_with_first_port_negative():
    # Test with large list, first port is negative
    ports = [-10000] + list(range(1, 1000))
    codeflash_output = get_prediction_aip_http_port(ports) # 665ns -> 437ns (52.2% faster)

def test_large_list_with_first_port_zero():
    # Test with large list, first port is zero
    ports = [0] + list(range(1, 1000))
    codeflash_output = get_prediction_aip_http_port(ports) # 582ns -> 360ns (61.7% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from typing import Optional, Sequence

# imports
import pytest  # used for our unit tests
from aiplatform.utils.prediction_utils import get_prediction_aip_http_port

# function to test
# -*- coding: utf-8 -*-

# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Since we don't have google.cloud.aiplatform.constants.prediction.DEFAULT_AIP_HTTP_PORT,
# we'll define it here for testing purposes.
class prediction:
    DEFAULT_AIP_HTTP_PORT = 8080
from aiplatform.utils.prediction_utils import get_prediction_aip_http_port

# unit tests

# ------------------ BASIC TEST CASES ------------------

def test_default_port_when_none():
    # Test when serving_container_ports is None
    codeflash_output = get_prediction_aip_http_port(None) # 654ns -> 540ns (21.1% faster)

def test_default_port_when_empty_list():
    # Test when serving_container_ports is an empty list
    codeflash_output = get_prediction_aip_http_port([]) # 583ns -> 396ns (47.2% faster)

def test_default_port_when_empty_tuple():
    # Test when serving_container_ports is an empty tuple
    codeflash_output = get_prediction_aip_http_port(()) # 590ns -> 373ns (58.2% faster)

def test_first_port_is_returned_single_element():
    # Test when serving_container_ports has one element
    codeflash_output = get_prediction_aip_http_port([1234]) # 467ns -> 341ns (37.0% faster)

def test_first_port_is_returned_multiple_elements():
    # Test when serving_container_ports has multiple elements
    codeflash_output = get_prediction_aip_http_port([4321, 5678, 9999]) # 489ns -> 327ns (49.5% faster)

def test_first_port_is_returned_tuple():
    # Test when serving_container_ports is a tuple
    codeflash_output = get_prediction_aip_http_port((5555, 6666)) # 538ns -> 385ns (39.7% faster)

def test_first_port_is_returned_large_int():
    # Test with a large integer port number
    codeflash_output = get_prediction_aip_http_port([65535]) # 483ns -> 331ns (45.9% faster)

# ------------------ EDGE TEST CASES ------------------

def test_negative_port_number():
    # Edge case: negative port number (should return the negative number as is)
    codeflash_output = get_prediction_aip_http_port([-1, 8081]) # 487ns -> 333ns (46.2% faster)

def test_zero_port_number():
    # Edge case: zero port number (valid integer, but not a valid port)
    codeflash_output = get_prediction_aip_http_port([0, 8081]) # 448ns -> 320ns (40.0% faster)

def test_first_port_is_none_in_list():
    # Edge case: first element is None (should return None)
    ports = [None, 8081]
    codeflash_output = get_prediction_aip_http_port(ports) # 448ns -> 314ns (42.7% faster)

def test_first_port_is_string():
    # Edge case: first element is a string (should return the string)
    ports = ["8081", 8082]
    codeflash_output = get_prediction_aip_http_port(ports) # 454ns -> 315ns (44.1% faster)

def test_first_port_is_float():
    # Edge case: first element is a float (should return the float)
    ports = [8080.5, 8081]
    codeflash_output = get_prediction_aip_http_port(ports) # 440ns -> 321ns (37.1% faster)

def test_first_port_is_bool():
    # Edge case: first element is a boolean (should return the boolean)
    ports = [True, 8081]
    codeflash_output = get_prediction_aip_http_port(ports) # 458ns -> 316ns (44.9% faster)

def test_first_port_is_complex():
    # Edge case: first element is a complex number (should return the complex number)
    ports = [3+4j, 8081]
    codeflash_output = get_prediction_aip_http_port(ports) # 431ns -> 314ns (37.3% faster)


def test_first_port_is_object():
    # Edge case: first element is an object (should return the object)
    class Dummy:
        pass
    dummy = Dummy()
    ports = [dummy, 8081]
    codeflash_output = get_prediction_aip_http_port(ports) # 610ns -> 445ns (37.1% faster)

def test_first_port_is_list():
    # Edge case: first element is a list (should return the list)
    ports = [[8081, 8082], 8083]
    codeflash_output = get_prediction_aip_http_port(ports) # 507ns -> 325ns (56.0% faster)

def test_first_port_is_dict():
    # Edge case: first element is a dict (should return the dict)
    ports = [{"port": 8081}, 8082]
    codeflash_output = get_prediction_aip_http_port(ports) # 493ns -> 363ns (35.8% faster)

def test_first_port_is_empty_string():
    # Edge case: first element is an empty string
    ports = ["", 8082]
    codeflash_output = get_prediction_aip_http_port(ports) # 484ns -> 336ns (44.0% faster)

def test_first_port_is_bytes():
    # Edge case: first element is bytes
    ports = [b"8081", 8082]
    codeflash_output = get_prediction_aip_http_port(ports) # 487ns -> 330ns (47.6% faster)

# ------------------ LARGE SCALE TEST CASES ------------------

def test_large_list_first_port():
    # Large scale: list with 1000 elements, first element is returned
    ports = [12345] + [i for i in range(1000)]
    codeflash_output = get_prediction_aip_http_port(ports) # 526ns -> 330ns (59.4% faster)

def test_large_list_default_port():
    # Large scale: list with 1000 elements, but it's empty
    ports = []
    codeflash_output = get_prediction_aip_http_port(ports) # 742ns -> 585ns (26.8% faster)

def test_large_list_first_port_is_none():
    # Large scale: first element is None, rest are integers
    ports = [None] + [i for i in range(1, 1000)]
    codeflash_output = get_prediction_aip_http_port(ports) # 547ns -> 350ns (56.3% faster)

def test_large_list_first_port_is_string():
    # Large scale: first element is a string
    ports = ["large_port"] + [i for i in range(1, 1000)]
    codeflash_output = get_prediction_aip_http_port(ports) # 515ns -> 307ns (67.8% faster)

def test_large_tuple_first_port():
    # Large scale: tuple with 1000 elements, first element is returned
    ports = tuple([54321] + [i for i in range(1000)])
    codeflash_output = get_prediction_aip_http_port(ports) # 530ns -> 337ns (57.3% faster)

def test_large_list_first_port_is_list():
    # Large scale: first element is a list
    ports = [[1, 2, 3]] + [i for i in range(1, 1000)]
    codeflash_output = get_prediction_aip_http_port(ports) # 503ns -> 317ns (58.7% faster)

def test_large_list_first_port_is_dict():
    # Large scale: first element is a dict
    ports = [{"port": 12345}] + [i for i in range(1, 1000)]
    codeflash_output = get_prediction_aip_http_port(ports) # 446ns -> 315ns (41.6% faster)

def test_large_list_first_port_is_float():
    # Large scale: first element is a float
    ports = [12345.678] + [i for i in range(1, 1000)]
    codeflash_output = get_prediction_aip_http_port(ports) # 460ns -> 303ns (51.8% faster)

def test_large_list_first_port_is_bool():
    # Large scale: first element is a bool
    ports = [False] + [i for i in range(1, 1000)]
    codeflash_output = get_prediction_aip_http_port(ports) # 487ns -> 305ns (59.7% faster)

def test_large_list_first_port_is_complex():
    # Large scale: first element is a complex number
    ports = [7+8j] + [i for i in range(1, 1000)]
    codeflash_output = get_prediction_aip_http_port(ports) # 486ns -> 295ns (64.7% faster)

# ------------------ DETERMINISM TEST CASE ------------------

def test_determinism_with_same_input():
    # Determinism: repeated calls with same input should yield same result
    ports = [2020, 3030]
    codeflash_output = get_prediction_aip_http_port(ports); result1 = codeflash_output # 484ns -> 313ns (54.6% faster)
    codeflash_output = get_prediction_aip_http_port(ports); result2 = codeflash_output # 225ns -> 216ns (4.17% faster)

def test_determinism_with_none():
    # Determinism: repeated calls with None should yield same result
    codeflash_output = get_prediction_aip_http_port(None); result1 = codeflash_output # 588ns -> 527ns (11.6% faster)
    codeflash_output = get_prediction_aip_http_port(None); result2 = codeflash_output # 220ns -> 197ns (11.7% faster)
# 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-get_prediction_aip_http_port-mglmrngq and push.

Codeflash

The optimization replaces the explicit `None` check and length validation with a single truthiness test. The original code uses `serving_container_ports is not None and len(serving_container_ports) > 0`, which requires two separate evaluations: a None check and a length calculation. The optimized version uses `if serving_container_ports:`, which leverages Python's truthiness rules where empty sequences (lists, tuples) and `None` both evaluate to `False`.

This change eliminates the `len()` function call, which has overhead even for small sequences, and reduces the boolean expression from two operations to one. The line profiler shows the conditional check time dropped from 64,650ns to 40,013ns (38% reduction), contributing significantly to the overall 33% speedup.

The optimization is particularly effective for:
- **Empty containers** (lists, tuples): 47-58% faster as shown in tests, since no length calculation is needed
- **Large sequences**: 51-68% faster improvements, as `len()` overhead becomes more significant with larger containers
- **None inputs**: 11-21% faster, eliminating the explicit None comparison

The truthiness approach is idiomatic Python and maintains identical behavior - both `None` and empty sequences return the default port, while non-empty sequences return their first element.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 11, 2025 02:02
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 11, 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