Skip to content

perf(web): count a closed workspace's runs from counts kept on disk - #314

Merged
harenome merged 1 commit into
ThinkFlowLab:mainfrom
harenome:perf/workspace-counts-summary
Nov 23, 2026
Merged

harenome merged 1 commit into
ThinkFlowLab:mainfrom
harenome:perf/workspace-counts-summary

Conversation

@harenome

@harenome harenome commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #309

What

/workspaces counted each closed workspace by decoding every job cache with all its trials, then read only job-level fields. _COUNTS kept the result in memory, so every start paid again. A closed workspace whose jobs had all ended now also keeps its runs and checks in jobs/counts.json, beside its job caches: {"version", "key", "counts"}, where key is a digest of the key _COUNTS uses (its roots, its job roots, workspace.heads of them, the run records' ids and states). After a start the file answers while that key matches, and no job is read. The key now also carries the catalogue's signature, in memory and on disk.

The open workspace is never stored or read from the file. A workspace with a job still going is counted as before, and its file is removed. A moved head (a job added, removed or rewritten, or a parse.SCHEMA or workspace.SCHEMA change, which the heads carry), a changed root, a changed run record, a changed catalogue or another COUNTS_VERSION leaves the file unused, and the workspace is counted again and the file rewritten. Refresh (clear_cache) removes it with the job caches.

One file per workspace rather than a summary per job: it is the smallest thing the page needs, and it is keyed by what the in-memory counts already trust. It is plain JSON with a version, job-level only, so a later job index can replace it.

Touches

backend: app/nullius/workspace.py (stored_counts, store_counts; n_cached and clear_cache skip the file). Web: app/web/workspaces.py (_counts). Tests: app/tests/test_workspace.py, app/ui_tests/test_run_routes.py. Docs: docs/how-it-works.md (the file in the workspace's table).

Verified

  • Backend suite: 3215 tests in 169 modules, OK. Route suite: 719 tests in 61 modules, OK. Both in a rootless python:3.12 container.

  • New tests:

    • after a simulated restart, the counts of a closed workspace come back with workspace.job_from_dict and parse.parse_job patched to fail;
    • a moved head, a changed root, a changed run record, a changed catalogue and another COUNTS_VERSION each make the stored counts unused and the workspace counted again; the counts then equal a fresh count;
    • a workspace with a job still running is counted at each request, its file removed; the open workspace has no file;
    • the counts on /workspaces of the open, a closed and a running workspace are the same from the file as with no file at all;
    • stored_counts refuses another key, another version and malformed files; n_cached does not count the file; Refresh removes it.
  • The first /workspaces after a start, on profiles from the generator in Workspaces: counting a workspace's runs decodes every trial of every job #309 (W workspaces of 20 ended Harbor jobs of 50 trials), main and this branch alternated on the same profile, 3 fresh server processes each in one rootless python:3.12 container, median and range:

    Before the start main: first this branch: first Next, both
    20x20x50, caches current 1.01 s (0.98-1.20) 0.23 s (0.23-0.26) 0.02-0.04 s
    20x20x50, caches current, /runs asked first 0.94 s (0.89-0.97) 0.12 s (0.11-0.14) 0.02 s
    100x20x50, caches current 4.05 s (4.03-4.08) 0.26 s (0.25-0.26) 0.06 s
    100x20x50, caches current, /runs asked first 4.21 s (4.20-4.52) 0.15 s (0.15-0.19) 0.06-0.09 s
    20x20x50, no job caches 11.1 s (11.0-11.6) 11.3 s (10.8-22.0) 0.02 s
    20x20x50, job folders touched 5.6 s (5.4-10.7) 6.1 s (5.7-11.2) 0.02-0.08 s
    20x20x50, caches from before a parse.SCHEMA change 11.5 s (11.3-11.6) 11.0 s (10.9-12.1) 0.02 s

    The last three rows count every workspace again on both, as they should; one slow start in each is the machine, not the change.

  • cProfile of the first request with caches current at 20x20x50 (werkzeug ProfilerMiddleware, which slows it down):

    main
       ncalls  cumtime  function
           20    3.025  web/workspaces.py:62(_counts)
           20    2.925  nullius/workspace.py:485(load_jobs)
          400    2.678  nullius/workspace.py:414(_read_cache)
          400    1.313  nullius/workspace.py:352(job_from_dict)
    this branch
           20    0.170  web/workspaces.py:63(_counts)
            1    0.137  nullius/workspace.py:524(load_jobs)     the open workspace, as before
           20    0.026  nullius/workspace.py:314(heads)
    

    What the branch's first request still spends beyond the next one is Jinja compiling the page's templates (1.28 s of 1.55 s under the profiler), which the first page of any process pays, and the open workspace's own load. With /runs asked first, _counts is under 0.02 s.

Risk

The stored counts trust the same key as the in-memory ones, so they share its limit (workspace.heads): a file changed inside a trial folder of an ended job and nothing else is not seen, now across a restart as well. Refresh removes the file. A write that fails (a read-only workspace folder) fails the page, as a job cache write does. The file is new on disk: an older version never reads it, and only miscounts it as one more cached job in n_cached. Undone by a revert; the leftover counts.json files are harmless and Refresh removes them.

The Workspaces page counted each closed workspace by decoding every job
cache with all its trials, after each start. A closed workspace whose
jobs had all ended now keeps its runs and checks in jobs/counts.json,
under a digest of the key the in-memory counts use (its roots, their
heads, the run records' states), with the catalogue's signature added.
A start reads that file instead of the trials while the key matches;
the open workspace and one with a job still going are counted as
before and never stored.
@harenome
harenome force-pushed the perf/workspace-counts-summary branch from e4e9df3 to 1f50930 Compare November 23, 2026 13:21
@harenome
harenome merged commit 262567b into ThinkFlowLab:main Nov 23, 2026
2 checks passed
@harenome
harenome deleted the perf/workspace-counts-summary branch November 23, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Workspaces: counting a workspace's runs decodes every trial of every job

1 participant