Repository navigation
Commit 38235bd
authored
perf(runs): note a moved head in the job index, and write a job's cache trial by trial (#317)
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 #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 #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 #313.
- cProfile of the first request at 20x20x50 (werkzeug
`ProfilerMiddleware`, which slows it down), measured before #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.1 parent aa3f41b commit 38235bd
2 files changed
Lines changed: 133 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
30 | 33 | | |
31 | 34 | | |
32 | 35 | | |
| |||
346 | 349 | | |
347 | 350 | | |
348 | 351 | | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
349 | 356 | | |
350 | | - | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
351 | 366 | | |
352 | 367 | | |
353 | 368 | | |
| |||
377 | 392 | | |
378 | 393 | | |
379 | 394 | | |
380 | | - | |
| 395 | + | |
| 396 | + | |
381 | 397 | | |
382 | 398 | | |
383 | 399 | | |
| |||
386 | 402 | | |
387 | 403 | | |
388 | 404 | | |
389 | | - | |
390 | | - | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
391 | 415 | | |
392 | 416 | | |
393 | 417 | | |
394 | 418 | | |
395 | 419 | | |
396 | 420 | | |
| 421 | + | |
| 422 | + | |
397 | 423 | | |
398 | 424 | | |
399 | 425 | | |
| |||
465 | 491 | | |
466 | 492 | | |
467 | 493 | | |
468 | | - | |
469 | | - | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
470 | 505 | | |
471 | 506 | | |
472 | 507 | | |
| |||
482 | 517 | | |
483 | 518 | | |
484 | 519 | | |
485 | | - | |
| 520 | + | |
486 | 521 | | |
487 | 522 | | |
488 | | - | |
| 523 | + | |
| 524 | + | |
489 | 525 | | |
490 | 526 | | |
491 | | - | |
| 527 | + | |
492 | 528 | | |
493 | 529 | | |
494 | 530 | | |
495 | 531 | | |
496 | 532 | | |
497 | 533 | | |
498 | | - | |
| 534 | + | |
499 | 535 | | |
500 | 536 | | |
501 | 537 | | |
502 | 538 | | |
503 | | - | |
504 | | - | |
505 | 539 | | |
506 | 540 | | |
507 | | - | |
| 541 | + | |
508 | 542 | | |
509 | 543 | | |
510 | 544 | | |
| |||
517 | 551 | | |
518 | 552 | | |
519 | 553 | | |
520 | | - | |
| 554 | + | |
521 | 555 | | |
522 | 556 | | |
523 | 557 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
269 | | - | |
| 269 | + | |
| 270 | + | |
270 | 271 | | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
278 | 278 | | |
279 | 279 | | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
280 | 307 | | |
281 | 308 | | |
282 | 309 | | |
| |||
358 | 385 | | |
359 | 386 | | |
360 | 387 | | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
361 | 438 | | |
362 | 439 | | |
363 | 440 | | |
| |||
0 commit comments