Skip to content

perf(pytest): cache path resolution to reduce discovery time for large test suites - #25655

Merged
Eleanor Boyd (eleanorjboyd) merged 7 commits into
microsoft:mainfrom
eleanorjboyd:institutional-boar
Dec 12, 2025
Merged

Eleanor Boyd (eleanorjboyd) merged 7 commits into
microsoft:mainfrom
eleanorjboyd:institutional-boar

Conversation

@eleanorjboyd

@eleanorjboyd Eleanor Boyd (eleanorjboyd) commented Dec 12, 2025 •

Copy link
Copy Markdown

fixes: #25348

Test discovery for large suites (~150k tests) shows 10x slowdown vs native pytest due to redundant path operations in pytest_sessionfinish hook. Profiling indicates repeated pathlib.Path.cwd() calls, os.fspath() conversions, and exception-based dictionary lookups dominate execution time.

Changes

Caching infrastructure

  • Module-level caches: _path_cache (node paths by id), _path_to_str_cache (string conversions), _CACHED_CWD (working directory)
  • cached_fsdecode(): memoized os.fspath() wrapper used for dictionary keys throughout tree building

Modified get_node_path()

  • Object id-based cache lookup before path resolution
  • Lazy initialization of cached cwd, eliminates 150k+ redundant syscalls
  • Store result before return

Control flow optimization

  • Replace try/except KeyError with dict.get() in 5 hotpath locations: process_parameterized_test(), build_test_tree(), build_nested_folders()
  • 3-5x faster for cache-hit case
Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
@eleanorjboyd Eleanor Boyd (eleanorjboyd) changed the title Institutional-boar Dec 12, 2025
@eleanorjboyd Eleanor Boyd (eleanorjboyd) added the debt Code quality issues label Dec 12, 2025
@eleanorjboyd
Eleanor Boyd (eleanorjboyd) marked this pull request as ready for review December 12, 2025 19:46
@vs-code-engineering vs-code-engineering Bot added this to the December / January 2026 milestone Dec 12, 2025
@eleanorjboyd
Eleanor Boyd (eleanorjboyd) merged commit 38e1cc6 into microsoft:main Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

debt Code quality issues

3 participants