Skip to content

Commit b43ebb7

Browse files
committed
Cache split nodes results to reduce long tests collection time on large test suites
1 parent cb15e7c commit b43ebb7

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

changelog/5516.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cache node splitting function which can improve collection performance in very large test suites.

src/_pytest/nodes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import warnings
3+
from functools import lru_cache
34

45
import py
56

@@ -13,6 +14,7 @@
1314
tracebackcutdir = py.path.local(_pytest.__file__).dirpath()
1415

1516

17+
@lru_cache(maxsize=None)
1618
def _splitnode(nodeid):
1719
"""Split a nodeid into constituent 'parts'.
1820
@@ -30,11 +32,12 @@ def _splitnode(nodeid):
3032
"""
3133
if nodeid == "":
3234
# If there is no root node at all, return an empty list so the caller's logic can remain sane
33-
return []
35+
return ()
3436
parts = nodeid.split(SEP)
3537
# Replace single last element 'test_foo.py::Bar' with multiple elements 'test_foo.py', 'Bar'
3638
parts[-1:] = parts[-1].split("::")
37-
return parts
39+
# Convert parts into a tuple to avoid possible errors with caching of a mutable type
40+
return tuple(parts)
3841

3942

4043
def ischildnode(baseid, nodeid):

0 commit comments

Comments
 (0)