Skip to content

perf(runs): note a moved head in the job index, and write a job's cache trial by trial - #317

Open
harenome wants to merge 1 commit into
ThinkFlowLab:mainfrom
harenome:perf/job-cache-write
Open

harenome wants to merge 1 commit into
ThinkFlowLab:mainfrom
harenome:perf/job-cache-write

Conversation

@harenome

Copy link
Copy Markdown
Collaborator

Closes #311

What

An ended job is answered by its head. When only its folder's time moved (an entry added or removed beside the trials), load_job decoded the cache, found the signature current and wrote the whole job again to store the new head. The head is now noted in the workspace's jobs/index.json, beside the signature it was seen with: the entry of an ended job gains signature and head. load_jobs already reads that file once and writes it once, so a moved head costs one small write for the workspace and none for the job. The next process answers the job by the head in the index when the cache's signature is the one noted there, without stating its trials. The cache file keeps the head it was written with, and either answers.

A cache write no longer goes through dataclasses.asdict, which deep-copied every trial, nor through json.dump, whose encoder is pure Python. Each trial is encoded by json.dumps (the C encoder) and written in turn, so one trial's text is in memory at a time, as before. job_to_dict reads the fields without copying them: no field of a Job or Trial holds a dataclass.

The cache file's shape is unchanged (its keys are the same; trials now comes last in job), so workspace.SCHEMA stays as it is and caches written before are read as they are.

Touches

backend: app/nullius/workspace.py (job_to_dict, _write_cache, _note_in_index, _noted_head, load_job). Tests: app/tests/test_workspace.py.

Verified

  • Backend suite: 3220 tests in 169 modules, OK. Route suite: 719 tests in 61 modules, OK. Both in a rootless python:3.12 container, on main with perf(web): count a closed workspace's runs from counts kept on disk #314.

  • New tests:

    • an ended job whose folder's mtime moved writes no job cache (the writer patched to fail), its file left byte for byte and mtime as it was, and the process after answers it with workspace._signatures patched to fail; the index holds the job's signature and head;
    • a head noted with another signature answers nothing: the trials are stated;
    • an ended job that gained a trial writes its cache again, once;
    • every job of the test fixtures equals job_from_dict(job_to_dict(job)), and its cache read back, written with asdict patched to fail;
    • a cache written by the previous writer (asdict and json.dump) is read by its head and equals the job;
    • the existing test that the cache is written a piece at a time now writes a job of 2000 trials (about 3 MB) and checks the peak stays under half the file.
  • 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, timed with curl inside it, median and range:

    Before the start main: first this branch: first Next, both
    20x20x50, caches current 0.88 s (0.87-0.97) 0.88 s (0.87-0.95) 0.02 s
    20x20x50, each job folder's mtime moved 5.15 s (5.05-5.24) 1.30 s (1.24-1.33) 0.02 s
    20x20x50, no job caches 6.04 s (5.85-6.08) 2.99 s (2.95-3.14) 0.02 s
    20x20x50, caches from before a parse.SCHEMA change 7.00 s (6.49-7.03) 3.84 s (3.83-3.95) 0.02 s
    100x20x50, caches current 4.47 s (4.13-4.60) 4.48 s (4.30-4.50) 0.06-0.07 s
    100x20x50, each job folder's mtime moved 26.7 s (25.3-34.4) 6.07 s (6.06-6.73) 0.06-0.07 s
    100x20x50, no job caches 29.8 s (29.2-31.7) 14.9 s (14.7-14.9) 0.06 s

    A moved head now takes 1.48 times the start with current caches at 20x20x50 (1.35 at 100x20x50). What it still spends beyond that is stating each job's trials once to confirm its signature, as the head alone cannot. The start with no caches is about twice as fast. The issue's 15.0 s for that start was measured before perf(runs): compute the catalogue signature once per load, not once per trial #313.

  • cProfile of the first request at 20x20x50 (werkzeug ProfilerMiddleware, which slows it down), measured before perf(web): count a closed workspace's runs from counts kept on disk #314 merged, so line numbers are those of that build:

    each job folder's mtime moved, main
       ncalls  cumtime  function
          400   22.513  nullius/workspace.py:433(load_job)
          400   17.261  nullius/workspace.py:427(_write_cache)
          420    9.773  json/__init__.py:120(dump)
          400    7.327  dataclasses.py:1308(asdict)
    this branch
          400    1.695  nullius/workspace.py:467(load_job)
          400    0.859  nullius/workspace.py:439(_read_cache)
          400    0.801  nullius/workspace.py:269(_signatures)
                         no _write_cache
    
    no job caches, main
          400   22.780  nullius/workspace.py:433(load_job)
          400   18.068  nullius/workspace.py:427(_write_cache)
    this branch
          400    6.018  nullius/workspace.py:467(load_job)
          400    4.068  nullius/runs/parse.py:888(parse_job)
          400    1.048  nullius/workspace.py:452(_write_cache)
    

Risk

The head in the index is trusted only with the signature it was noted with, and the head is taken before the signature, as the cache's own head is: a job changed after its head was taken moves its head again. It shares the head's existing limit: a file changed inside a trial folder of an ended job and nothing else is not seen; Refresh reads every job again. A lost or older index (an older version rewriting it without the new keys) costs one statement of the trials per job, never a wrong answer. The cache's format is unchanged, so an upgrade costs nothing and a downgrade reads the caches as written. The index's new keys are ignored by its other readers. Undone by a revert; the extra keys in index.json are harmless and Refresh removes the file.

…he trial by trial

An ended job whose folder's time moved while its files did not was decoded,
found current, and written again in full only to store its new head. Its head
is now noted in jobs/index.json beside the signature it was seen with, which
load_jobs already writes once, and the next process answers the job by it
without stating its trials. The cache file is left as it was.

A cache write no longer deep-copies every trial with dataclasses.asdict, nor
encodes through json.dump's Python encoder: each trial is encoded by the C
encoder and written in turn, so one trial's text is in memory at a time. The
file's shape is unchanged; caches written before are read as they are.

This branch has not been deployed

No deployments
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: a job's cache is written again in full when only its folder's time moved, and every write serialises all trials

1 participant