Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Stop three studio meta-tests re-cutting and re-walking the same sources (#10886)

Daniel Han8bcf16031a31
diff --git a/tests/studio/test_backend_ci_parallel_isolation.py b/tests/studio/test_backend_ci_parallel_isolation.py
index 3199f6a6c0c..577f7dea415 100644
--- a/tests/studio/test_backend_ci_parallel_isolation.py
+++ b/tests/studio/test_backend_ci_parallel_isolation.py
@@ -302,6 +302,9 @@ def _is_timed(node: ast.AST, names: set, helpers: set) -> bool:
return False
+_FRAGILE_CACHE: dict = {}
+
+
def _fragile_timing_asserts(path: Path) -> list:
"""Assertions whose outcome depends on how the process was scheduled.
@@ -314,10 +317,24 @@ def _fragile_timing_asserts(path: Path) -> list:
Read with ast, not a regex: grepping `< 0.05` matches a float tolerance, and grepping
`elapsed` matches whatever a variable happens to be called.
+
+ Memoised on (resolved path, file text). Two tests below scan all 924 backend test
+ files and the dict comprehension in one of them calls this twice per path, so the
+ same parse-and-walk ran roughly three times over: 19.0s + 20.6s of the file's 37.8s.
+ The read is deliberately still done every call and the text is part of the key, so a
+ file rewritten mid-session is rescanned rather than served a stale verdict; only the
+ parse and the walks are shared. The stored list is copied out, so no caller can
+ mutate another's result, and the tree never leaves this function.
"""
+ source = path.read_text(encoding = "utf-8", errors = "replace")
+ key = (str(path.resolve()), source)
+ cached = _FRAGILE_CACHE.get(key)
+ if cached is not None:
+ return list(cached)
try:
- tree = ast.parse(path.read_text(encoding = "utf-8", errors = "replace"))
+ tree = ast.parse(source)
except SyntaxError:
+ _FRAGILE_CACHE[key] = []
return []
# Helpers first: a name can hold a duration only because a helper returned one.
helpers = _timing_helpers(tree)
@@ -357,7 +374,8 @@ def _fragile_timing_asserts(path: Path) -> list:
elif isinstance(upper, ast.Constant) and isinstance(upper.value, (int, float)):
if upper.value <= TIGHT_BOUND_S:
found.append(f"{path.name}:{node.lineno} duration < {upper.value}")
- return found
+ _FRAGILE_CACHE[key] = found
+ return list(found)
@pytest.mark.parametrize("path, reason", ISOLATED, ids = [p for p, _ in ISOLATED])
diff --git a/tests/studio/test_cancel_atomicity.py b/tests/studio/test_cancel_atomicity.py
index 6309bec4c03..361b8208933 100644
--- a/tests/studio/test_cancel_atomicity.py
+++ b/tests/studio/test_cancel_atomicity.py
@@ -129,28 +129,47 @@ def _load_active_generations():
return module
-def _load_registry_module():
+_REGISTRY_SOURCE = None
+
+
+def _registry_source():
+ """The `_WANTED` top-level definitions, verbatim, joined in file order.
+
+ `ast.get_source_segment` re-splits the whole 1.72 MB source on every call, so
+ asking it for all 1011 top-level nodes took ~13s -- and each of the seven tests
+ below paid it again. Two changes, neither of which alters a byte of the result:
+ the membership test now runs before the segment is cut, so only the nodes that
+ are kept are ever cut, and the joined text is built once per process. The `exec`
+ stays per call, so every test still gets its own fresh `_CANCEL_REGISTRY`.
+ """
+ global _REGISTRY_SOURCE
+ if _REGISTRY_SOURCE is not None:
+ return _REGISTRY_SOURCE
chunks = []
for n in _TREE.body:
+ if isinstance(n, (ast.FunctionDef, ast.ClassDef)):
+ wanted = n.name in _WANTED
+ elif isinstance(n, ast.Assign):
+ wanted = any(t.id in _WANTED for t in n.targets if isinstance(t, ast.Name))
+ elif isinstance(n, ast.AnnAssign):
+ wanted = isinstance(n.target, ast.Name) and n.target.id in _WANTED
+ else:
+ wanted = False
+ if not wanted:
+ continue
seg = ast.get_source_segment(_SRC, n)
if seg is None:
continue
- if isinstance(n, (ast.FunctionDef, ast.ClassDef)) and n.name in _WANTED:
- chunks.append(seg)
- elif isinstance(n, ast.Assign):
- names = [t.id for t in n.targets if isinstance(t, ast.Name)]
- if any(name in _WANTED for name in names):
- chunks.append(seg)
- elif (
- isinstance(n, ast.AnnAssign)
- and isinstance(n.target, ast.Name)
- and n.target.id in _WANTED
- ):
- chunks.append(seg)
+ chunks.append(seg)
+ _REGISTRY_SOURCE = "\n\n".join(chunks)
+ return _REGISTRY_SOURCE
+
+
+def _load_registry_module():
mod = {"active_generations": _load_active_generations()}
exec(
"import threading, time\nfrom typing import Optional\n_account_cancel_key = lambda key: key\n"
- + "\n\n".join(chunks),
+ + _registry_source(),
mod,
)
return mod
diff --git a/tests/studio/test_stream_cancel_registration_timing.py b/tests/studio/test_stream_cancel_registration_timing.py
index 9625189ac3f..7117b257bd8 100644
--- a/tests/studio/test_stream_cancel_registration_timing.py
+++ b/tests/studio/test_stream_cancel_registration_timing.py
@@ -278,27 +278,46 @@ def _load_active_generations():
return module
-def _load_registry_module():
+_REGISTRY_SOURCE = None
+
+
+def _registry_source():
+ """The `_WANTED` top-level definitions, verbatim, joined in file order.
+
+ `ast.get_source_segment` re-splits the whole 1.72 MB source on every call, so
+ asking it for all 1011 top-level nodes took ~13s, once per test that loads the
+ registry. Two changes, neither of which alters a byte of the result: the
+ membership test now runs before the segment is cut, so only the nodes that are
+ kept are ever cut, and the joined text is built once per process. The `exec`
+ stays per call, so every test still gets its own fresh `_CANCEL_REGISTRY`.
+ """
+ global _REGISTRY_SOURCE
+ if _REGISTRY_SOURCE is not None:
+ return _REGISTRY_SOURCE
chunks = []
for n in _TREE.body:
+ if isinstance(n, (ast.FunctionDef, ast.ClassDef)):
+ wanted = n.name in _WANTED
+ elif isinstance(n, ast.Assign):
+ wanted = any(t.id in _WANTED for t in n.targets if isinstance(t, ast.Name))
+ elif isinstance(n, ast.AnnAssign):
+ wanted = isinstance(n.target, ast.Name) and n.target.id in _WANTED
+ else:
+ wanted = False
+ if not wanted:
+ continue
seg = ast.get_source_segment(SRC, n)
if seg is None:
continue
- if isinstance(n, (ast.FunctionDef, ast.ClassDef)) and n.name in _WANTED:
- chunks.append(seg)
- elif isinstance(n, ast.Assign):
- names = [t.id for t in n.targets if isinstance(t, ast.Name)]
- if any(name in _WANTED for name in names):
- chunks.append(seg)
- elif (
- isinstance(n, ast.AnnAssign)
- and isinstance(n.target, ast.Name)
- and n.target.id in _WANTED
- ):
- chunks.append(seg)
+ chunks.append(seg)
+ _REGISTRY_SOURCE = "\n\n".join(chunks)
+ return _REGISTRY_SOURCE
+
+
+def _load_registry_module():
mod = {"active_generations": _load_active_generations()}
exec(
- "import threading, time\n_account_cancel_key = lambda key: key\n" + "\n\n".join(chunks), mod
+ "import threading, time\n_account_cancel_key = lambda key: key\n" + _registry_source(), mod
)
return mod