Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Dec 4, 2025

📄 49% (0.49x) speedup for is_platform_arm in pandas/compat/__init__.py

⏱️ Runtime : 52.7 milliseconds 35.4 milliseconds (best of 5 runs)

📝 Explanation and details

The optimization eliminates redundant platform.machine() calls by storing the result in a variable. In the original code, platform.machine() is called twice - once for the in check and potentially again for the startswith() check. The optimized version calls it once and reuses the result.

Key changes:

  • Added mach = platform.machine() to cache the machine architecture string
  • Replaced both platform.machine() calls with the cached mach variable

Why this leads to speedup:
platform.machine() involves system calls to query hardware information, which has significant overhead. The line profiler shows the original version spent 99% of its time on the line with platform.machine() calls. By eliminating one of these expensive calls, the optimized version reduces total execution time by 48%.

Performance characteristics:
The optimization provides the most benefit when the first condition (mach in ("arm64", "aarch64")) fails and the second condition (mach.startswith("armv")) must be evaluated. In such cases, the original code makes two system calls while the optimized version makes only one. The test results show particularly strong improvements (35-60% faster) for cases involving armv prefixes and non-ARM architectures where both conditions are evaluated.

Impact on workloads:
This function appears to be used for platform detection, likely called during pandas initialization or compatibility checks. Even though individual calls are microsecond-level, the 48% speedup becomes meaningful if called frequently during startup or in loops, reducing overall initialization time and improving user experience.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 4072 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
# Helper to patch platform.machine for our tests
from unittest.mock import patch

# imports
import pytest  # used for our unit tests
from pandas.compat.__init__ import is_platform_arm

# unit tests


# ---------------------------
# 1. Basic Test Cases
# ---------------------------


def test_arm64_returns_true():
    # Basic ARM64 identifier
    with patch("platform.machine", return_value="arm64"):
        codeflash_output = is_platform_arm()  # 16.0μs -> 16.2μs (1.41% slower)


def test_aarch64_returns_true():
    # Basic AARCH64 identifier
    with patch("platform.machine", return_value="aarch64"):
        codeflash_output = is_platform_arm()  # 15.0μs -> 14.3μs (5.10% faster)


def test_armv7l_returns_true():
    # ARM version identifier
    with patch("platform.machine", return_value="armv7l"):
        codeflash_output = is_platform_arm()  # 21.9μs -> 14.7μs (48.6% faster)


def test_armv8_returns_true():
    # Another ARM version identifier
    with patch("platform.machine", return_value="armv8"):
        codeflash_output = is_platform_arm()  # 19.8μs -> 14.1μs (40.6% faster)


def test_x86_64_returns_false():
    # Common non-ARM architecture
    with patch("platform.machine", return_value="x86_64"):
        codeflash_output = is_platform_arm()  # 21.6μs -> 14.6μs (47.6% faster)


def test_amd64_returns_false():
    # Another common non-ARM architecture
    with patch("platform.machine", return_value="AMD64"):
        codeflash_output = is_platform_arm()  # 20.3μs -> 13.4μs (52.0% faster)


# ---------------------------
# 2. Edge Test Cases
# ---------------------------


def test_empty_string_returns_false():
    # Edge case: empty string
    with patch("platform.machine", return_value=""):
        codeflash_output = is_platform_arm()  # 20.9μs -> 13.3μs (56.7% faster)


def test_armv_case_sensitive():
    # Edge case: case sensitivity
    with patch("platform.machine", return_value="ARMV7L"):
        codeflash_output = is_platform_arm()  # 20.5μs -> 14.2μs (43.9% faster)


def test_armv_in_middle_returns_false():
    # Edge case: 'armv' not at start
    with patch("platform.machine", return_value="x86_armv7l"):
        codeflash_output = is_platform_arm()  # 20.0μs -> 13.6μs (47.0% faster)


def test_armv_only_prefix_returns_true():
    # Edge case: only prefix
    with patch("platform.machine", return_value="armv"):
        codeflash_output = is_platform_arm()  # 19.6μs -> 14.5μs (35.1% faster)


def test_armv_with_suffix_returns_true():
    # Edge case: valid prefix with suffix
    with patch("platform.machine", return_value="armv99"):
        codeflash_output = is_platform_arm()  # 21.1μs -> 13.1μs (60.6% faster)


def test_arm64_with_trailing_space_returns_false():
    # Edge case: trailing whitespace
    with patch("platform.machine", return_value="arm64 "):
        codeflash_output = is_platform_arm()  # 20.1μs -> 14.5μs (38.4% faster)


def test_armv7l_with_leading_space_returns_false():
    # Edge case: leading whitespace
    with patch("platform.machine", return_value=" armv7l"):
        codeflash_output = is_platform_arm()  # 20.3μs -> 13.7μs (48.7% faster)


def test_numeric_string_returns_false():
    # Edge case: numeric string
    with patch("platform.machine", return_value="12345"):
        codeflash_output = is_platform_arm()  # 19.4μs -> 14.1μs (38.2% faster)


def test_special_characters_returns_false():
    # Edge case: special characters
    with patch("platform.machine", return_value="!@#$%"):
        codeflash_output = is_platform_arm()  # 20.8μs -> 13.7μs (52.0% faster)


def test_none_returns_false():
    # Edge case: NoneType
    with patch("platform.machine", return_value=None):
        codeflash_output = is_platform_arm()


# ---------------------------
# 3. Large Scale Test Cases
# ---------------------------


@pytest.mark.parametrize("arch", ["armv" + str(i) for i in range(1000)])
def test_many_armv_variants_return_true(arch):
    # Large scale: 1000 armv variants
    with patch("platform.machine", return_value=arch):
        codeflash_output = is_platform_arm()  # 20.3ms -> 13.9ms (45.7% faster)


@pytest.mark.parametrize("arch", ["x86_" + str(i) for i in range(1000)])
def test_many_non_arm_variants_return_false(arch):
    # Large scale: 1000 x86 variants
    with patch("platform.machine", return_value=arch):
        codeflash_output = is_platform_arm()  # 20.2ms -> 13.9ms (45.6% faster)


@pytest.mark.parametrize(
    "arch", ["arm64", "aarch64", "armv7l", "armv8", "armv", "armv99"]
)
def test_all_known_arm_variants_return_true(arch):
    # Large scale: all known ARM variants
    with patch("platform.machine", return_value=arch):
        codeflash_output = is_platform_arm()  # 109μs -> 83.0μs (32.0% faster)


@pytest.mark.parametrize(
    "arch", ["x86_64", "AMD64", "i386", "powerpc", "mips", "sparc", "ppc64"]
)
def test_all_known_non_arm_variants_return_false(arch):
    # Large scale: all known non-ARM variants
    with patch("platform.machine", return_value=arch):
        codeflash_output = is_platform_arm()  # 142μs -> 96.3μs (48.2% faster)


def test_performance_large_batch():
    # Performance: check 500 arm and 500 non-arm in a batch
    arm_archs = ["armv" + str(i) for i in range(500)] + ["arm64", "aarch64"]
    non_arm_archs = ["x86_" + str(i) for i in range(500)] + ["x86_64", "AMD64"]
    # Test all arm archs
    for arch in arm_archs:
        with patch("platform.machine", return_value=arch):
            codeflash_output = is_platform_arm()
    # Test all non-arm archs
    for arch in non_arm_archs:
        with patch("platform.machine", return_value=arch):
            codeflash_output = is_platform_arm()


# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
from __future__ import annotations

import platform

# imports
import pytest
from pandas.compat.__init__ import is_platform_arm

# unit tests


@pytest.mark.parametrize(
    "machine_str,expected",
    [
        # Basic ARM architectures
        ("arm64", True),  # Apple Silicon, ARM64
        ("aarch64", True),  # 64-bit ARM
        ("armv7l", True),  # ARMv7 (Raspberry Pi, etc.)
        ("armv8", True),  # ARMv8
        ("armv6l", True),  # ARMv6
        ("armv5te", True),  # Older ARMv5
        # Non-ARM architectures
        ("x86_64", False),  # Standard 64-bit Intel/AMD
        ("i386", False),  # 32-bit Intel
        ("amd64", False),  # AMD 64-bit
        ("ppc64le", False),  # PowerPC
        ("mips", False),  # MIPS
        ("sparc", False),  # SPARC
        ("", False),  # Empty string
        ("unknown", False),  # Unknown architecture
        # Edge cases: similar but not ARM
        ("arm", False),  # Not a valid machine string for ARM
        ("armv", True),  # Just "armv" should match .startswith("armv")
        ("ARMV7L", False),  # Case-sensitive: should be False
        ("AARCH64", False),  # Case-sensitive: should be False
        (" arm64", False),  # Leading space, should be False
        ("arm64 ", False),  # Trailing space, should be False
        ("armv7l-extra", False),  # Suffix, should be False
        # Large scale: long non-ARM strings
        ("x86_64" * 100, False),  # Very long string, not ARM
        ("armv" + "7" * 995, True),  # Very long valid ARM string
        ("aarch64" + "x" * 993, False),  # Long string, but doesn't match
    ],
)
def test_is_platform_arm_various(monkeypatch, machine_str, expected):
    """
    Test is_platform_arm for a variety of platform.machine() return values,
    including basic, edge, and large-scale cases.
    """
    # Patch platform.machine to return the test string
    monkeypatch.setattr(platform, "machine", lambda: machine_str)
    codeflash_output = is_platform_arm()  # 23.9μs -> 22.2μs (7.61% faster)


def test_is_platform_arm_actual_machine(monkeypatch):
    """
    Test is_platform_arm using the actual platform.machine() value.
    This test does not patch anything and ensures the function runs without error.
    """
    # The result should be a boolean
    codeflash_output = is_platform_arm()
    result = codeflash_output  # 3.70μs -> 3.87μs (4.37% slower)


def test_is_platform_arm_does_not_crash_on_nonstring(monkeypatch):
    """
    Test that is_platform_arm does not crash if platform.machine() returns a non-string.
    Should raise AttributeError (since .startswith will fail).
    """
    monkeypatch.setattr(platform, "machine", lambda: None)
    with pytest.raises(AttributeError):
        is_platform_arm()  # 2.36μs -> 2.42μs (2.32% slower)
    monkeypatch.setattr(platform, "machine", lambda: 12345)
    with pytest.raises(AttributeError):
        is_platform_arm()  # 1.96μs -> 1.79μs (9.44% faster)
    monkeypatch.setattr(platform, "machine", lambda: ["arm64"])
    with pytest.raises(AttributeError):
        is_platform_arm()  # 1.41μs -> 1.22μs (15.3% faster)


def test_is_platform_arm_case_sensitivity(monkeypatch):
    """
    Test that is_platform_arm is case-sensitive and only matches exact lowercase strings.
    """
    monkeypatch.setattr(platform, "machine", lambda: "ARM64")
    codeflash_output = is_platform_arm()  # 1.14μs -> 1.04μs (10.0% faster)
    monkeypatch.setattr(platform, "machine", lambda: "AARCH64")
    codeflash_output = is_platform_arm()  # 605ns -> 619ns (2.26% slower)
    monkeypatch.setattr(platform, "machine", lambda: "Armv7l")
    codeflash_output = is_platform_arm()  # 560ns -> 555ns (0.901% faster)


def test_is_platform_arm_performance_large_scale(monkeypatch):
    """
    Test is_platform_arm performance with a large number of different machine strings.
    Ensures that the function is efficient and correct for up to 1000 unique cases.
    """
    # Generate 500 valid and 500 invalid machine strings
    valid = [f"armv{i}" for i in range(500)]
    invalid = [f"x86_64_{i}" for i in range(500)]
    # Test all valid cases
    for s in valid:
        monkeypatch.setattr(platform, "machine", lambda s=s: s)
        codeflash_output = is_platform_arm()  # 187μs -> 170μs (10.3% faster)
    # Test all invalid cases
    for s in invalid:
        monkeypatch.setattr(platform, "machine", lambda s=s: s)
        codeflash_output = is_platform_arm()  # 182μs -> 163μs (11.8% faster)


def test_is_platform_arm_unicode_handling(monkeypatch):
    """
    Test is_platform_arm with unicode and non-ASCII machine strings.
    """
    monkeypatch.setattr(platform, "machine", lambda: "ármv7l")
    codeflash_output = is_platform_arm()  # 1.11μs -> 1.10μs (1.55% faster)
    monkeypatch.setattr(platform, "machine", lambda: "aarch64✓")
    codeflash_output = is_platform_arm()  # 636ns -> 623ns (2.09% faster)
    monkeypatch.setattr(platform, "machine", lambda: "armv七")
    codeflash_output = is_platform_arm()  # 618ns -> 594ns (4.04% faster)


def test_is_platform_arm_whitespace(monkeypatch):
    """
    Test is_platform_arm with whitespace in machine string.
    """
    monkeypatch.setattr(platform, "machine", lambda: " armv7l")
    codeflash_output = is_platform_arm()  # 980ns -> 776ns (26.3% faster)
    monkeypatch.setattr(platform, "machine", lambda: "armv7l ")
    codeflash_output = is_platform_arm()  # 651ns -> 601ns (8.32% faster)
    monkeypatch.setattr(platform, "machine", lambda: "  armv7l  ")
    codeflash_output = is_platform_arm()  # 556ns -> 411ns (35.3% faster)


def test_is_platform_arm_substring(monkeypatch):
    """
    Test is_platform_arm where ARM substring is present but not at start.
    """
    monkeypatch.setattr(platform, "machine", lambda: "x86_armv7l")
    codeflash_output = is_platform_arm()  # 893ns -> 851ns (4.94% faster)
    monkeypatch.setattr(platform, "machine", lambda: "x86aarch64")
    codeflash_output = is_platform_arm()  # 526ns -> 527ns (0.190% slower)
    monkeypatch.setattr(platform, "machine", lambda: "fooarm64bar")
    codeflash_output = is_platform_arm()  # 500ns -> 386ns (29.5% 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-is_platform_arm-mir3b1ce and push.

Codeflash Static Badge

The optimization eliminates redundant `platform.machine()` calls by storing the result in a variable. In the original code, `platform.machine()` is called twice - once for the `in` check and potentially again for the `startswith()` check. The optimized version calls it once and reuses the result.

**Key changes:**
- Added `mach = platform.machine()` to cache the machine architecture string
- Replaced both `platform.machine()` calls with the cached `mach` variable

**Why this leads to speedup:**
`platform.machine()` involves system calls to query hardware information, which has significant overhead. The line profiler shows the original version spent 99% of its time on the line with `platform.machine()` calls. By eliminating one of these expensive calls, the optimized version reduces total execution time by 48%.

**Performance characteristics:**
The optimization provides the most benefit when the first condition (`mach in ("arm64", "aarch64")`) fails and the second condition (`mach.startswith("armv")`) must be evaluated. In such cases, the original code makes two system calls while the optimized version makes only one. The test results show particularly strong improvements (35-60% faster) for cases involving `armv` prefixes and non-ARM architectures where both conditions are evaluated.

**Impact on workloads:**
This function appears to be used for platform detection, likely called during pandas initialization or compatibility checks. Even though individual calls are microsecond-level, the 48% speedup becomes meaningful if called frequently during startup or in loops, reducing overall initialization time and improving user experience.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 December 4, 2025 07:04
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Dec 4, 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 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant