Skip to content

refactor: dedup the reentrant-mutation guard message - #21

Merged
josephjohncox merged 1 commit into
mainfrom
chore/cleanup-perf
Jul 22, 2026
Merged

refactor: dedup the reentrant-mutation guard message#21
josephjohncox merged 1 commit into
mainfrom
chore/cleanup-perf

Conversation

@josephjohncox

Copy link
Copy Markdown
Owner

Summary

Code cleanup / style / deduplication pass over the perf work landed in #19 and #20.

After a full audit of the session's additions, the only safe, no-coupling, no-perf-impact duplication worth removing was the reentrant-mutation guard message, repeated verbatim at all five reentrancy-guard sites. It is now a single module constant _REENTRANT_MUTATION_MESSAGE. The message is built only on the error path, so the hot path is unchanged (scalar unsynchronized still ~4.6–5.0 M ops/s).

Deliberately left as-is (with rationale)

The audit found that most apparent duplication is intentional and justified; consolidating it would add cost or risk for no real benefit:

  • Hot-path lock + reentrancy-guard scaffolding (repeated across the four collapsed add/discard/release/reserve authoritative paths). Factoring it into a context manager re-introduces the with __enter__/__exit__ calls that perf: fast native RangeSet mutation with selectable locking #20 measured as the dominant per-op cost; a plain helper or decorator adds an equivalent call frame. The duplication is the measured price of the ~5 M ops/s scalar path and is documented in the code.
  • Per-experiment evidence verifiers (_gate, _provenance, verify_artifacts, render_markdown, _git_metadata, run_matrix — each with a distinct implementation per file). These encode each experiment's own schema, gates, and provenance and are intentionally self-contained for tamper resistance.
  • Shared pure primitives (_percentile, _checksum, _reject_*, _bootstrap). Only _percentile is byte-identical, and its four copies live in three different packages (tests/performance/experiments/, tests/performance/, and scripts/). Extracting it would couple a standalone CI verifier script to the tests package — worse than an eight-line pure function. The remaining variants differ in error-message text that tamper tests match on, so unifying them risks those tests for no shipped-code benefit.

Validation

  • Full suite: 1402 passed, 21 skipped (no regressions).
  • Ruff check + format clean; mypy treemendous clean; git diff --check clean.
  • Hot-path perf sanity check confirms the scalar mutation throughput is unchanged.

The identical 'reentrant mutation on the same RangeSet is not allowed'
RuntimeError text was repeated at all five reentrancy-guard sites (the four
collapsed authoritative add/discard/release/reserve hot paths and the
allocate guard). Hoist it to a module constant. The message is only
constructed on the error path, so the hot path is unaffected.

Left intentionally as-is after review: the per-op lock+reentrancy-guard
scaffolding (inlined for the measured hot-path win; a context manager or
helper re-adds the call overhead just removed) and the self-contained
per-experiment evidence verifiers (distinct schemas/gates; tamper-resistant
by design).
@josephjohncox
josephjohncox merged commit 5f142e0 into main Jul 22, 2026
16 checks passed
@josephjohncox josephjohncox mentioned this pull request Jul 22, 2026
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