Skip to content

Conversation

@eleanorjboyd
Copy link
Member

@eleanorjboyd eleanorjboyd commented Dec 12, 2025

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

eleanorjboyd and others added 6 commits December 12, 2025 10:49
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 eleanorjboyd changed the title Institutional-boar perf(pytest): cache path resolution to reduce discovery time for large test suites Dec 12, 2025
@eleanorjboyd eleanorjboyd self-assigned this Dec 12, 2025
@eleanorjboyd eleanorjboyd added the debt Covers everything internal: CI, testing, refactoring of the codebase, etc. label Dec 12, 2025
@eleanorjboyd 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 eleanorjboyd merged commit 38e1cc6 into microsoft:main Dec 12, 2025
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

debt Covers everything internal: CI, testing, refactoring of the codebase, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(pytest collection): long runtimes for larger number of tests

2 participants