Skip to content

perf(tui): restore steps -- 8.8% less time - #46

Merged
sql-hkr merged 1 commit into
sql-hkr:mainfrom
dexhunter:perf/checkpoint-seek-lookups
Oct 26, 2026
Merged

sql-hkr merged 1 commit into
sql-hkr:mainfrom
dexhunter:perf/checkpoint-seek-lookups

Conversation

@dexhunter

Copy link
Copy Markdown
Contributor

TL;DR

  • Reduce repeated attribute lookups and duplicate loop-history dictionary probes.
  • Optimize CheckpointManager.restore_exact_step and its detect_loop and record helpers for large checkpoint seeks.
  • The frozen workload based on test_goto_large_step_via_cache_and_dynamic_extension takes 8.8% less wall time: median paired ratio 0.912 across three fresh harness runs.
  • Median batch times across those runs: 195.306 ms before → 177.172 ms after; each batch contains three workload repetitions.
  • Validation: the requested four-file pytest group passes all 146 tests; the full suite passes all 287 tests; Ruff lint and formatting pass.

Problem and change

Large seeks extend the checkpoint cache one CPU instruction at a time. Each iteration repeatedly resolves CPU attributes and bound methods, while loop detection probes the same history dictionary twice for existing signatures.

Bind the CPU, checkpoint containers and frequently used methods locally, and use history.get to retrieve a previous step with one lookup. History values are integer step counts, so None distinguishes a missing entry from step zero. Execution order, loop checks, history updates, snapshot ordering, trace handling and public signatures remain unchanged. No dependencies or persistent caches are added.

Measurements

The workload retains the repository test's precompute-to-5,000, seek-to-4,500 and extend-to-20,000 sequence. It also includes seeded variations and boundary cases for backward seeks, tracing, snapshot caps, memory and stack instructions, halts, cycles, empty caches and invalid requests.

Measurements used Python 3.12.13 and the frozen evaluator on an isolated benchmark slot. Each run used nine interleaved baseline/candidate pairs, fresh seeded inputs, three workload repetitions per sample, a parent-process wall clock, and a separate pristine-baseline oracle. The timed batches include the workload's state observations; these are not isolated method-call timings or pytest runner durations.

Fresh run Baseline batch (ms) Candidate batch (ms) Median paired ratio Relative IQR
1 194.701 176.110 0.904301 1.07%
2 195.851 178.304 0.912983 1.43%
3 195.306 177.172 0.911658 0.54%

The headline reduction uses the median of the three paired-ratio scores, 0.911658. The absolute before/after values are separate medians of the three batch-time results, so their quotient need not equal the paired-ratio score. All three scores are below the evaluator's 0.95 improvement threshold, and each run passes its 5% relative-IQR noise gate and exact semantic checks.

Validation

Run from the changed repository copy:

PYTHONPATH=src python -m pytest tests/test_tui.py tests/test_control_flow.py tests/test_arithmetic.py tests/test_compiler.py
# 146 passed
PYTHONPATH=src python -m pytest
# 287 passed
ruff check src/ tests/
# All checks passed
ruff format --check src/ tests/
# 20 files already formatted

The pristine baseline also passes the full 287-test suite and both style checks. Existing tests and the frozen oracle cover this behavior-preserving optimization; no new functionality or tests are added.

Checklist

  • Code follows style guidelines.
  • All tests pass.
  • New tests added for new functionality (no new functionality).
  • Documentation updated (no user-facing change).
  • Commit message is clear and descriptive.
  • PR description explains changes and motivation.
  • Branch is up-to-date with main (publication check pending).
  • No unrelated changes included.

AI assistance

Codex gpt-6.1-sol generated the optimization candidates, tracked with Weco Observe. Automated source review, semantic checks, native tests and benchmarking informed selection. No human review is claimed.

Weco share link: https://dashboard.weco.ai/share/OVyh1gbcXZ7zmOO5XWrtis26cn8RDmxo

Reuse local CPU, checkpoint and method references during exact-step
restoration, and avoid duplicate loop-history dictionary lookups.

The frozen large-seek workload takes 8.8% less wall time across three
fresh paired runs. All 287 tests and Ruff checks pass.

Co-Authored-By: Aiden <aiden@weco.ai>

@sql-hkr sql-hkr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Clean and safe micro-optimization. Thanks for the performance improvement!

@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.48387% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/tiny8/tui.py 85.48% 9 Missing ⚠️

📢 Thoughts on this report? Let us know!

@sql-hkr
sql-hkr merged commit 8af241c into sql-hkr:main Oct 26, 2026
5 checks passed
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.

2 participants