Skip to content

⚡️ Speed up function _get_epsg_srs by 7%#32

Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
codeflash/optimize-_get_epsg_srs-mh5rdsq0
Open

⚡️ Speed up function _get_epsg_srs by 7%#32
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
codeflash/optimize-_get_epsg_srs-mh5rdsq0

Conversation

@codeflash-ai
Copy link
Copy Markdown

@codeflash-ai codeflash-ai Bot commented Oct 25, 2025

📄 7% (0.07x) speedup for _get_epsg_srs in opendm/tiles/gdal2tiles.py

⏱️ Runtime : 298 microseconds 279 microseconds (best of 181 runs)

📝 Explanation and details

Optimization rationale:

  • Replaces .get()/is None with a direct try/except KeyError access pattern for _EPSG_SRS_CACHE, which empirical CPython benchmarks show to be faster when the hit rate is high and the value is never intended to be None.
  • 'osr.SpatialReference' object creation and EPSG import logic are unchanged and only occur if the cache miss happens, so behavior is preserved.
  • No other logic or side-effects are changed.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 210 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import pytest  # used for our unit tests
from opendm.tiles.gdal2tiles import _get_epsg_srs
from osgeo import osr

# function to test
# (copied from the provided code)
_EPSG_SRS_CACHE = {}
from opendm.tiles.gdal2tiles import _get_epsg_srs

# unit tests

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








def test_edge_invalid_type_string():
    """Test that passing a string instead of an int raises TypeError or RuntimeError."""
    with pytest.raises((TypeError, RuntimeError)):
        _get_epsg_srs("4326") # 18.9μs -> 20.0μs (5.61% slower)

def test_edge_invalid_type_float():
    """Test that passing a float instead of an int raises TypeError or RuntimeError."""
    with pytest.raises((TypeError, RuntimeError)):
        _get_epsg_srs(4326.0)









#------------------------------------------------
import pytest  # used for our unit tests
from opendm.tiles.gdal2tiles import _get_epsg_srs
from osgeo import osr

# function to test
_EPSG_SRS_CACHE = {}
from opendm.tiles.gdal2tiles import _get_epsg_srs

# unit tests

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





def test_edge_invalid_epsg_code():
    # Test with an invalid EPSG code (should raise an exception)
    with pytest.raises(Exception):
        _get_epsg_srs(-1)

def test_edge_nonexistent_epsg_code():
    # Test with a non-existent EPSG code (should raise an exception)
    with pytest.raises(Exception):
        _get_epsg_srs(999999)

def test_edge_epsg_code_as_string():
    # Test with EPSG code as a string (should raise TypeError)
    with pytest.raises(TypeError):
        _get_epsg_srs("4326") # 19.0μs -> 20.0μs (4.76% slower)

def test_edge_epsg_code_as_float():
    # Test with EPSG code as a float (should raise TypeError)
    with pytest.raises(TypeError):
        _get_epsg_srs(4326.0)

def test_edge_epsg_code_zero():
    # EPSG:0 is not defined, should raise an exception
    with pytest.raises(Exception):
        _get_epsg_srs(0)

def test_edge_epsg_code_none():
    # None is not a valid EPSG code, should raise TypeError
    with pytest.raises(TypeError):
        _get_epsg_srs(None) # 19.1μs -> 19.6μs (2.80% slower)



def test_large_scale_cache_memory():
    # Test that cache does not grow unbounded for repeated calls
    _EPSG_SRS_CACHE.clear()
    codes = list(range(4326, 4326 + 100))
    for code in codes:
        try:
            _get_epsg_srs(code)
        except Exception:
            pass  # Some codes may not exist, that's fine


def test_large_scale_invalid_codes():
    # Test many invalid codes do not pollute cache
    _EPSG_SRS_CACHE.clear()
    invalid_codes = list(range(900000, 900100))
    for code in invalid_codes:
        with pytest.raises(Exception):
            _get_epsg_srs(code)

# ---------------------- Miscellaneous/Robustness ----------------------


def test_misc_epsg_code_as_bool():
    # Boolean should not be accepted as EPSG code
    with pytest.raises(TypeError):
        _get_epsg_srs(True)
    with pytest.raises(TypeError):
        _get_epsg_srs(False)
# 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_epsg_srs-mh5rdsq0 and push.

Codeflash

**Optimization rationale:**
- Replaces `.get()`/`is None` with a direct `try/except KeyError` access pattern for `_EPSG_SRS_CACHE`, which empirical CPython benchmarks show to be faster when the hit rate is high and the value is never intended to be `None`.
- `'osr.SpatialReference'` object creation and EPSG import logic are unchanged and only occur if the cache miss happens, so behavior is preserved.
- No other logic or side-effects are changed.
@codeflash-ai codeflash-ai Bot requested a review from mashraf-222 October 25, 2025 04:07
@codeflash-ai codeflash-ai Bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 25, 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.

0 participants