Skip to content

perf(paths): decide common-shape path containment without pathlib's parent walk #5135

Done

Description

Part of #4942.

Problem

Python's pathlib containment methods are slow on hot paths. relative_to walks every parent of the base and builds a path object per step, and is_relative_to does the same. A warm install at t50 makes about 25,900 relative_to and 12,500 is_relative_to calls, which slows install, audit and the no-op install.

Proposal

Add explicit helpers to the path owner utils/paths.py (relative_to, relative_posix, is_within) with semantics identical to pathlib. They decide only the common shape, absolute paths of one class rooted at POSIX / or Windows X:\, by comparing normalized strings, and defer every other shape to pathlib unchanged. The ten owner files that make hot containment calls move to the helpers. Every resolve()/realpath call stays where it is, so each containment check makes the same filesystem calls in the same order; only the pure comparison step after resolution changes.

Done when

  • A fuzz-equivalence property test runs in CI and is green.
  • Each helper has red-first equivalence cells.
  • One pre-declared 15-pair run on all 3 OSes at the PR head (the full 15 pairs on every OS).
  • A W3b design note and QL H8.

Out of scope

  • Any path cache or memo.
  • Patching the standard library.
  • Changes to containment decisions.

Implementation: #5132.

Activity

  1. added
    type/performance
    on Nov 23, 2026
  2. danielmeppiel commented on Nov 23, 2026

    @danielmeppiel
    CollaboratorAuthor

    Decision: approve
    Area: project
    Scope: replace pathlib relative/containment hot calls in 10 owner files with one explicit utils/paths helper of identical semantics, making the same syscalls at every containment check.
    Done when: fuzz-equivalence CI property test green; red-first equivalence cells per helper; one pre-declared 15-pair 3-OS run at the PR head (full 15 pairs on every OS); W3b design note; QL H8.
    Out of scope: any path cache or memo; stdlib patching; changes to containment decisions.
    Review contact: Daniel Meppiel (@danielmeppiel)

  3. added
    status/accepted
    on Nov 23, 2026
  4. moved this from Todo to In Progress in APM Roadmapon Nov 23, 2026
  5. danielmeppiel commented on Nov 24, 2026

    @danielmeppiel
    CollaboratorAuthor

    #5132 merged (c4071af): APM's path checks now use one string-based helper for "is this path inside that directory" instead of building and comparing pathlib objects each time. Same answers, including for symlinks and ..; less work per file.

    CPU at tree-50 (50 packages), paired p50 over 15 interleaved pairs, main a725feb vs cc25bda (a final 30-pair rerun on macOS for the install cells):

    Journey Linux macOS Windows
    No-op install -18.2% -23.0% -11.3%
    Audit -15.2% 0.0% -9.8%
    Install, warm cache -10.3% -7.4% (30-pair rerun) -6.5%
    Cold install -7.7% -7.5% (30-pair rerun) -4.6%

    No cell regressed. Requests and process counts are identical between base and new. Runs: Linux 45299448704, macOS 45299452582 plus final rerun 45304713784 and 45304718096, Windows 45299455907, 45299459920, 45299463934.

  6. danielmeppiel commented on Nov 24, 2026

    @danielmeppiel
    CollaboratorAuthor

    Done in #5132 (merged c4071af): explicit string-based containment helpers in utils/paths.py with pathlib-identical semantics. Measured numbers were posted above (t50 no-op -18.2% / -23.0% / -11.3% CPU on Linux / macOS / Windows).

  7. moved this from In Progress to Done in APM Roadmapon Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/acceptedtype/performance

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions