diff --git a/.jules/bolt.md b/.jules/bolt.md index 63607c3..ce62852 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -60,3 +60,7 @@ ## 2026-06-25 - [Optimize Path.exists() when paired with stat()] **Learning:** Checking `Path.exists()` before `Path.stat()` introduces a redundant system call because `exists()` internally uses `stat()`. **Action:** Rely on catching the `OSError` from `Path.stat()` to simultaneously check for existence and retrieve file attributes, saving measurable I/O overhead on large filesystems. + +## 2024-05-24 - [Avoid excessive .resolve() for subprocess arguments] +**Learning:** `pathlib.Path.resolve()` performs heavy disk I/O to check for symlinks and build a canonical path. When passing paths to external binaries (like `ffmpeg`, `setfile`, `brctl`) via `subprocess.run`, strict canonicalization is generally unnecessary because OS APIs and external tools handle relative paths, `..`, and symlinks natively. +**Action:** Prefer `pathlib.Path.absolute()` over `pathlib.Path.resolve()` when building command-line arguments for external tools to save fractions of a millisecond, though recognize this is a micro-optimization and focus on larger architectural bottlenecks first. diff --git a/media_shrinker.py b/media_shrinker.py index 3eede1d..00db8ac 100644 --- a/media_shrinker.py +++ b/media_shrinker.py @@ -132,9 +132,9 @@ def command( raise MediaShrinkerError( "ffmpeg argument template is missing '-i'" ) from exc - args[input_index] = str(input_path.resolve()) + args[input_index] = str(input_path.absolute()) if output_path is not None: - args[-1] = str(output_path.resolve()) + args[-1] = str(output_path.absolute()) if overwrite: args = ["-y" if arg == "-n" else arg for arg in args] @@ -633,7 +633,7 @@ def build_icloud_download_command( ) -> list[str]: """Build a safe iCloud download command for source_path.""" - return [brctl_path, "download", str(source_path.resolve())] + return [brctl_path, "download", str(source_path.absolute())] def download_from_icloud(source_path: Path, *, brctl_path: str = "brctl") -> None: @@ -1630,7 +1630,7 @@ def _copy_macos_creation_time( "%m/%d/%Y %H:%M:%S" ) subprocess.run( - [setfile_path, "-d", creation_date, str(dest.resolve())], + [setfile_path, "-d", creation_date, str(dest.absolute())], check=False, capture_output=True, text=True, diff --git a/tests/test_media_shrinker.py b/tests/test_media_shrinker.py index 1b202d2..a726ed6 100644 --- a/tests/test_media_shrinker.py +++ b/tests/test_media_shrinker.py @@ -280,9 +280,9 @@ def test_conversion_command_resolves_input_and_output_overrides(self) -> None: self.assertEqual( command[command.index("-i") + 1], - str(Path("-input.wav").resolve()), + str(Path("-input.wav").absolute()), ) - self.assertEqual(command[-1], str(Path("-output.flac").resolve())) + self.assertEqual(command[-1], str(Path("-output.flac").absolute())) def test_lossy_audio_uses_highest_opus_bitrate_that_fits_target_with_safety_margin( self, @@ -1042,7 +1042,7 @@ def test_build_icloud_download_command_uses_argument_list_for_paths_with_spaces( self.assertEqual( command, - ["brctl", "download", str(Path("folder/file with spaces.m4a").resolve())], + ["brctl", "download", str(Path("folder/file with spaces.m4a").absolute())], )