You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
refactor(pathfinder): write the "first hit, else every miss" loop once - #3632
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). Three planted bugs in the helper each fail the suite.
Testing
Full cuda_pathfinder test suite at this commit: 1298 passed, 6 skipped (Linux, Python 3.14).
pre-commit clean, including mypy-pathfinder. mypy also passes with --platform win32.
Reviewing files that changed from the base of the PR and between 73d5042 and 91265d1.
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
Refactor
Streamlined how the application searches for NVIDIA CUDA libraries. The existing search order and results are unchanged, so this update does not alter the user-facing behavior.
The changes add first_hit to handle ordered search results and misses, then use it across dynamic library searches, including the CTK root canary search. Existing search ordering, filters, and miss aggregation remain unchanged.
Adds first_hit to lazily return the first match or combine misses in order. Directory, environment-root, Program Files, step-sequence, and CTK canary searches use the helper. Their ordering and filters remain unchanged.
Possibly related PRs
NVIDIA/cuda-python#3607: Defines the search-step result contract that first_hit uses when combining misses.
This refactor consolidates repeated first-hit search loops into one helper without a behavior change. No concrete defect was found, and the existing tests plus the author's comparisons support merging.
Architecture risk: High · up to 73d50
The change affects 1 system.
Changed systems:cuda_pathfinder
Architecture concerns
No architecture-level concerns identified.
Review details
Systems and components
observed — cuda_pathfinder (service) was modified; 2 changed files map to changed impact.
Before / after behavior
observed — Modified behavior in cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py: The first_hit helper is imported for use by the canary search.
observed — Modified behavior in cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py: _try_ctk_root_canary replaces its explicit loop—which skipped unresolved anchors, returned on the first found result, and accumulated misses—with a generator of resolved roots and a first_hit call over searches for non-None roots.
observed — Modified behavior in cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py: The Iterable type is added to the collection imports.
observed — Modified behavior in cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py: Adds first_hit, which evaluates results lazily, returns the first hit, and otherwise combines misses in iteration order.
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 8 remain after this review.
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 <noreply@anthropic.com>
Signed-off-by: Rob Parolin <rparolin@nvidia.com>
The reason will be displayed to describe this comment to others. Learn more.
Readability suggestion (non-blocking, no behavior change): the two-stage generator plus the trailing if root filter makes the reader assemble the "skip unset or empty env vars" rule from two expressions. A small named generator, in the style of _iter_lib_dirs, keeps the loop-and-if shape visible and leaves the caller as a one-line first_hit(...). It stays lazy, so os.environ.get is still read in the same order relative to the searches.
def_iter_install_root_env_vars(ctx: SearchContext) ->Iterator[tuple[str, str]]:
"""Yield (env_var, root) for each descriptor env var that is set and non-empty."""forenv_varinctx.install_root_env_vars:
root=os.environ.get(env_var)
ifroot:
yieldenv_var, rootdeffind_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_dirsreturnfirst_hit(
_find_under_root(ctx, root, rel_dirs, env_var)
forenv_var, rootin_iter_install_root_env_vars(ctx)
)
The reason will be displayed to describe this comment to others. Learn more.
Readability suggestion (non-blocking, no behavior change): this is the hardest of the new expressions to read. It has a nested for, a sorted(...) with a key and reverse, and a filter, all inside one call argument. Moving the candidate enumeration into a named generator documents the ordering rule ("newest version first") and leaves the caller trivial. Laziness is unchanged: glob.glob for the next root_glob still runs only after the previous roots missed.
def_iter_program_files_roots(ctx: SearchContext) ->Iterator[str]:
"""Yield existing Program Files install roots, newest version first."""forroot_globinctx.program_files_root_globs():
forrootinsorted(glob.glob(root_glob), key=numeric_aware_path_sort_key, reverse=True):
ifos.path.isdir(root):
yieldrootdeffind_in_program_files_roots(ctx: SearchContext) ->FindResult|NotFound:
"""Search descriptor-configured installation roots under Program Files."""returnfirst_hit(
_find_under_anchor_root(ctx, resolve_path(root), FOUND_VIA_PROGRAM_FILES)
forrootin_iter_program_files_roots(ctx)
)
The reason will be displayed to describe this comment to others. Learn more.
Readability suggestion (non-blocking, no behavior change): same idea here, to avoid chaining two generators with a None filter on the second. A named generator makes "skip anchors that do not resolve" explicit, and it stays lazy, so the canary probe subprocess still runs one anchor at a time and stops at the first hit.
def_iter_canary_ctk_roots(ctx: SearchContext) ->Iterator[str]:
"""Yield the CTK root found by each canary anchor library, skipping anchors that do not resolve."""forlibnameinctx.desc.ctk_root_canary_anchor_libnames:
ctk_root=resolve_ctk_root_via_canary(libname)
ifctk_rootisnotNone:
yieldctk_rootdef_try_ctk_root_canary(ctx: SearchContext) ->FindResult|NotFound:
"""Try CTK-root canary fallback for descriptor-configured libraries."""returnfirst_hit(find_via_ctk_root(ctx, ctk_root) forctk_rootin_iter_canary_ctk_roots(ctx))
Please keep the helper in this module: resolve_ctk_root_via_canary is looked up as a module attribute here, and tests may patch it at this location. This file would also need Iterator imported (from collections.abc).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
— codex on behalf of Ralf —
Description
Part 23 of 23 of a readability stack for
cuda.pathfinder(stack: #3623 → #3602 → #3603 → #3604 → #3605 → #3606 → #3607 → #3608 → #3609 → #3610 → #3611 → #3613 → #3618 → #3619 → #3620 → #3621 → #3622 → #3627 → #3628 → #3629 → #3630 → #3631 → #3632). No behavior change.Five functions repeated the same loop: take results in order, return the first hit, otherwise add up the misses.
first_hit()insearch_steps.pynow holds it, and the five pass it their results:_find_under_rootfind_in_install_root_env_varsfind_in_program_files_rootsrun_find_steps_try_ctk_root_canaryinload_nvidia_dynamic_lib.pyResults 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_stepstests cover the helper (first hit wins, later steps are not called, misses combine in order). Three planted bugs in the helper each fail the suite.Testing
cuda_pathfindertest suite at this commit: 1298 passed, 6 skipped (Linux, Python 3.14).mypy-pathfinder. mypy also passes with--platform win32.mainare still byte-identical (Linux, simulated Windows x64 and arm64), and all 526 public-API scenarios give the same result as fix(pathfinder): check for an already-loaded library before searching the disk #3623.PLC Local Security Evidence (Advisory)
Local advisory checks only, run over the whole stack. Not release-gate results.
subprocess.runcall in the canary probe, now incanary_probe.pyChecklist
🤖 Generated with Claude Code