Skip to content

Commit a1b00a6

Browse files
Merge pull request #22 from Zektopic/jules-16876764488483228441-ca118caf
⚡ Bolt: [performance improvement] Cache ping absolute path
2 parents a571b51 + 25b72cc commit a1b00a6

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

.jules/bolt.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
## 2024-05-24 - [Thread Pool Size for Concurrent I/O]
1010
**Learning:** Hardcoded, small thread pool limits (like `max_workers=50`) act as severe bottlenecks for highly I/O bound concurrent network tasks like ping sweeping an entire subnet. Because pings spend most of their time waiting on network timeouts, artificially restricting concurrency forces the pool to process timeouts in batches, drastically increasing total scan time.
1111
**Action:** When using `concurrent.futures.ThreadPoolExecutor` for pure I/O or network tasks where the operation is mostly waiting, dynamically size `max_workers` to handle the full workload concurrently (e.g., `min(total_tasks, 256)`) to complete all timeouts in parallel.
12+
13+
## 2026-03-23 - [Subprocess PATH lookup overhead]
14+
**Learning:** Calling `subprocess.call(["ping", ...])` without the absolute path causes the OS/Python interpreter to repeatedly scan through all directories listed in the `PATH` environment variable to locate the executable file for *every single* invocation. In highly concurrent or iterative loops (like a network sweep using `ThreadPoolExecutor`), this redundant lookup creates a measurable performance bottleneck.
15+
**Action:** When invoking external commands repetitively via `subprocess` in a tight loop or concurrently, cache the absolute path of the executable once at module initialization using `shutil.which("command") or "command"` to eliminate `PATH` traversal overhead.

test_testping1.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def test_is_reachable_prevents_log_injection(self, mock_call):
8989
@patch('testping1.subprocess.call')
9090
def test_is_reachable_subprocess_timeout(self, mock_call):
9191
"""Test is_reachable handles subprocess.TimeoutExpired securely."""
92-
mock_call.side_effect = subprocess.TimeoutExpired(cmd='ping', timeout=7)
92+
from testping1 import PING_PATH
93+
mock_call.side_effect = subprocess.TimeoutExpired(cmd=PING_PATH, timeout=7)
9394
with self.assertLogs(level='ERROR') as log:
9495
self.assertFalse(is_reachable('127.0.0.1', timeout=5))
9596
self.assertIn("Ping command timed out unexpectedly.", log.output[0])
@@ -98,12 +99,13 @@ def test_is_reachable_subprocess_timeout(self, mock_call):
9899
@patch('testping1.subprocess.call')
99100
def test_is_reachable_calls_ping_correctly(self, mock_call):
100101
"""Test is_reachable calls the ping command with correct arguments."""
102+
from testping1 import PING_PATH
101103
mock_call.return_value = 0
102104

103105
is_reachable('192.168.1.1', timeout=5)
104106
# Verify that subprocess.call was called with the correct arguments, including the timeout
105107
mock_call.assert_called_once_with(
106-
['ping', '-n', '-c', '1', '-W', '5', '192.168.1.1'],
108+
[PING_PATH, '-n', '-c', '1', '-W', '5', '192.168.1.1'],
107109
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=7
108110
)
109111

testping1.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
import concurrent.futures
33
import ipaddress
44
import logging
5+
import shutil
56
from tqdm import tqdm # Install with `pip install tqdm`
67

8+
# ⚡ Bolt: Cache the absolute path of the ping executable.
9+
# Calling shutil.which() once at module load avoids the overhead of traversing
10+
# the system PATH environment variable during every subprocess.call() execution.
11+
# This yields a measurable speedup when firing thousands of concurrent pings.
12+
PING_PATH = shutil.which("ping") or "ping"
13+
714
def is_reachable(ip, timeout=1):
815
"""Checks if a device at the given IP address is reachable with a ping.
916
@@ -43,7 +50,7 @@ def is_reachable(ip, timeout=1):
4350
# The `-n` flag skips reverse DNS resolution. Without it, ping attempts to
4451
# resolve the hostname for every IP, which can cause multi-second delays
4552
# (even with a 1s timeout) if the IP lacks a PTR record or DNS is unresponsive.
46-
command = ["ping", "-n", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux)
53+
command = [PING_PATH, "-n", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux)
4754

4855
# ⚡ Bolt: Optimized ping execution by using subprocess.call and redirecting
4956
# output to DEVNULL instead of using Popen with PIPE.

0 commit comments

Comments
 (0)