From 32c87c82522b4b27970ccfc79db29a2e026db525 Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Thu, 26 Nov 2026 11:38:04 -0700 Subject: [PATCH 1/2] refactor(pathfinder): write the "first hit, else every miss" loop once Five functions repeated the same loop: take results in order, return the first hit, otherwise add up the misses. `first_hit()` in `search_steps.py` now holds it, and the five pass it their results: - `_find_under_root` - `find_in_install_root_env_vars` - `find_in_program_files_roots` - `run_find_steps` - `_try_ctk_root_canary` in `load_nvidia_dynamic_lib.py` Results are produced lazily, so nothing after the first hit runs. That matters for the canary step, which starts a subprocess per anchor: it still stops at the first root that has the library. No behavior change. The existing `run_find_steps` tests cover the helper (first hit wins, later steps are not called, misses combine in order), and three planted bugs in it each fail the suite. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- .../_dynamic_libs/load_nvidia_dynamic_lib.py | 13 +--- .../pathfinder/_dynamic_libs/search_steps.py | 59 ++++++++----------- 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py index 84311f86240..f0159b08ff8 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py @@ -26,6 +26,7 @@ NotFound, SearchContext, find_via_ctk_root, + first_hit, run_find_steps, ) from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import ALL_AVAILABLE_LIBNAMES @@ -62,16 +63,8 @@ def _load_driver_lib_no_cache(desc: LibDescriptor) -> LoadedDL: def _try_ctk_root_canary(ctx: SearchContext) -> FindResult | NotFound: """Try CTK-root canary fallback for descriptor-configured libraries.""" - missed = NotFound() - for canary_libname in ctx.desc.ctk_root_canary_anchor_libnames: - ctk_root = resolve_ctk_root_via_canary(canary_libname) - if ctk_root is None: - continue - result = find_via_ctk_root(ctx, ctk_root) - if result.found: - return result - missed += result - return missed + ctk_roots = (resolve_ctk_root_via_canary(libname) for libname in ctx.desc.ctk_root_canary_anchor_libnames) + return first_hit(find_via_ctk_root(ctx, ctk_root) for ctk_root in ctk_roots if ctk_root is not None) def _load_lib_no_cache(libname: str) -> LoadedDL: diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py index 924dccf5288..3536f76f4e9 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py @@ -25,7 +25,7 @@ import glob import os -from collections.abc import Callable, Iterator +from collections.abc import Callable, Iterable, Iterator from cuda.pathfinder._dynamic_libs.search_platform import ( FindResult, @@ -49,6 +49,19 @@ FindStep = Callable[[SearchContext], FindResult | NotFound] +def first_hit(results: Iterable[FindResult | NotFound]) -> FindResult | NotFound: + """Return the first hit in *results*, or else every miss combined in order. + + *results* is read lazily: nothing after the first hit is evaluated. + """ + missed = NotFound() + for result in results: + if result.found: + return result + missed += result + return missed + + def _iter_lib_dirs(root: str, rel_dirs: tuple[str, ...]) -> Iterator[str]: """Yield existing library directories under *root* in descriptor order.""" for rel_path in rel_dirs: @@ -64,13 +77,7 @@ def _find_under_root( found_via: str, ) -> FindResult | NotFound: """Resolve *rel_dirs* under *root*, then find the requested library.""" - missed = NotFound() - for lib_dir in _iter_lib_dirs(root, rel_dirs): - result = find_lib_in_dir(ctx, lib_dir, found_via) - if result.found: - return result - missed += result - return missed + return first_hit(find_lib_in_dir(ctx, lib_dir, found_via) for lib_dir in _iter_lib_dirs(root, rel_dirs)) def _find_under_anchor_root(ctx: SearchContext, root: str, found_via: str) -> FindResult | NotFound: @@ -156,16 +163,8 @@ def find_in_conda(ctx: SearchContext) -> FindResult | NotFound: def find_in_install_root_env_vars(ctx: SearchContext) -> FindResult | NotFound: """Search installation roots named by descriptor-specific environment variables.""" rel_dirs = ctx.install_root_env_rel_dirs - missed = NotFound() - for env_var in ctx.install_root_env_vars: - root = os.environ.get(env_var) - if not root: - continue - result = _find_under_root(ctx, root, rel_dirs, env_var) - if result.found: - return result - missed += result - return missed + roots = ((env_var, os.environ.get(env_var)) for env_var in ctx.install_root_env_vars) + return first_hit(_find_under_root(ctx, root, rel_dirs, env_var) for env_var, root in roots if root) def find_in_cuda_path(ctx: SearchContext) -> FindResult | NotFound: @@ -187,16 +186,12 @@ def find_in_cuda_path(ctx: SearchContext) -> FindResult | NotFound: def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound: """Search descriptor-configured installation roots under Program Files.""" - missed = NotFound() - for root_glob in ctx.program_files_root_globs(): - for root in sorted(glob.glob(root_glob), key=numeric_aware_path_sort_key, reverse=True): - if not os.path.isdir(root): - continue - result = _find_under_anchor_root(ctx, resolve_path(root), FOUND_VIA_PROGRAM_FILES) - if result.found: - return result - missed += result - return missed + return first_hit( + _find_under_anchor_root(ctx, resolve_path(root), FOUND_VIA_PROGRAM_FILES) + for root_glob in ctx.program_files_root_globs() + for root in sorted(glob.glob(root_glob), key=numeric_aware_path_sort_key, reverse=True) + if os.path.isdir(root) + ) # --------------------------------------------------------------------------- @@ -221,10 +216,4 @@ def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound: def run_find_steps(ctx: SearchContext, steps: tuple[FindStep, ...]) -> FindResult | NotFound: """Run find steps in order, returning the first hit or else every step's miss combined.""" - missed = NotFound() - for step in steps: - result = step(ctx) - if result.found: - return result - missed += result - return missed + return first_hit(step(ctx) for step in steps) From a0fac69426e58b7dc9b4a1147d173b6db32e4d95 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 26 Nov 2026 11:17:28 -0800 Subject: [PATCH 2/2] ci: require manual copy-pr-bot approval Signed-off-by: Ralf W. Grosse-Kunstleve