Skip to content

perf(database): create the checkpoint programs directory once per save - #478

Open
dexhunter wants to merge 2 commits into
algorithmicsuperintelligence:mainfrom
dexhunter:perf/checkpoint-save-write-loop
Open

perf(database): create the checkpoint programs directory once per save#478
dexhunter wants to merge 2 commits into
algorithmicsuperintelligence:mainfrom
dexhunter:perf/checkpoint-save-write-loop

Conversation

@dexhunter

@dexhunter dexhunter commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Summary

ProgramDatabase.save() previously recreated the same programs/ directory for every program in a checkpoint. This change creates it once when the database is non-empty, hoists loop-invariant prompt lookups, and writes each encoded record to the buffered text stream in one call.

The fallback path in _save_program() is unchanged for its single-program caller. Empty checkpoints also keep their previous layout: they contain metadata.json without a programs/ directory.

Measurement

CPU time for one ProgramDatabase.save() call on a fresh 1,000-program population using the shipped defaults (num_islands=5, log_prompts=True):

Version CPU time
main at 411fb59 240.2696 ms
exact PR diff 181.2613 ms

That is a 24.6% reduction. The benchmark uses process CPU time under an exclusive host lock, and every accepted candidate must produce byte-identical checkpoints for empty, one-program, populated, and path-collision cases.

Autoresearch record

Supplementary autoresearch with Weco contributed the optimization direction. The review-required trajectory contains 11 distinct accepted scalar records, including the measured baseline and an exact replay of this PR diff:

https://dashboard.weco.ai/share/xNYBuZK0Xb0aglM7c-SRbnO3ZHLbDO6j

The fastest search result measured 169.1532 ms, but it used a more intrusive serialization path and was not selected. The PR keeps the smaller repository-compatible change.

Validation

  • Exact final diff passed the frozen evaluator at 181.2613 ms.
  • OPENAI_API_KEY=test-key-for-unit-tests uv run --extra dev python -m unittest discover tests: 430 tests passed.
  • Black and git diff --check: passed.
  • Hosted unit tests, integration tests, SAST, and CLA: passed.
  • Independent final review: passed.

_save_program called os.makedirs(programs_dir, exist_ok=True) for every
program written, so a checkpoint at the default population_size=1000 created
the same directory 1,000 times. Create it once in save() and pass it down.

Also lift the log_prompts and prompts_by_program lookups out of the
per-program loop, and write each record with a single f.write(json.dumps(...))
rather than json.dump(..., f).

The bytes written are unchanged: checkpoints from an identical seeded
200-program population compare equal by SHA-256 before and after, both with
recorded prompts and without.

One save() call at the shipped defaults goes from 241.1829 ms to 182.8334 ms
of CPU, a 24.0% reduction, measured as the median of 5 repetitions on freshly
generated populations.
Keep the programs directory absent when saving an empty database, matching the pre-optimization checkpoint structure.
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.

1 participant