Skip to content

Speed up long runs without changing results (ref #101) - #118

Merged
TLmaK0 merged 1 commit into
developfrom
issue-101-long-run-perf
Nov 25, 2026
Merged

TLmaK0 merged 1 commit into
developfrom
issue-101-long-run-perf

Conversation

@TLmaK0

@TLmaK0 TLmaK0 commented Nov 25, 2026

Copy link
Copy Markdown
Owner

What changes

This speeds up the per-generation cost of long runs without changing any result (every random draw happens in the same order):

  • Genome neuron ids cached. Genome keeps its sorted neuron ids up to date as genes are added, so neurons() and has_neuron() no longer rebuild or scan the gene list. Genes are never removed and never change their ends.
  • Compatibility distance walks both neuron lists together, with no allocation and no sort.
  • FirstCompatible speciation stops at the first compatible species (lowest id) instead of measuring the distance to every species.
  • Crossover walks both sorted gene lists together and builds the child's genes in one go, instead of binary searches and a sorted insert per gene.
  • Add-connection counts the new connections and finds the drawn one without listing all ~n² pairs (new_connection_count / nth_new_connection). It picks the same pair for the same draw.
  • Gene::cmp compares (in, out) tuples directly. The order is the same.
  • Interspecies mating candidates (species_parents) are copied without their compiled phenotype or CTRNN state (Organism::clone_for_mating).
  • New example long_run_profile: the Cerrar #19 y #45 con test de regresión #73 scenario with per-generation timing of evolve and evaluate, genome sizes, and a digest of every generation.

How it was verified

  • Identity: tests/long_run_identity_tests.rs checks a digest of 300 generations of the Cerrar #19 y #45 con test de regresión #73 scenario, recorded on develop before the change. It is checked on Linux x86_64 only because fitness goes through exp; elsewhere it checks reproducibility. The 1000-generation digest from long_run_profile is the same before and after (87e3c9f6901f412c, and 475adde81536aafc with pop 60).
  • Equivalence of add-connection: a new unit test compares the count and every position against the old full listing, over odd genomes and 300 mutated ones.
  • Banco A (xor_bench 50 500 init reset=1, seeds 1/101/201/301): per-run results are bit-identical to develop, sorted and with timings stripped. Medians are 52/53/53/51, unchanged.
  • Gates: cargo fmt -- --check OK. cargo clippy --release --all-targets gives 56 warnings, the same as develop, none new. cargo build --release --examples OK. cargo test --release --no-fail-fast all green (133 lib tests).

Closing criterion

Develop and branch binaries were run alternately on a heavily loaded machine, best of 3 runs each:

scenario develop branch speed-up
#73: pop 30, 1000 gens 5.36 / 5.95 / 7.60 s 1.94 / 1.57 / 2.73 s 2.8× / 3.8× / 2.8×
pop 60, 1000 gens 22.15 s 7.04 s 3.1×

Evolve alone is about 5× faster, and evaluation is now about half of the remaining time. The full profile is posted on the issue. Target ≥ 2×: met.

cargo run --release --example long_run_profile -- repeat=3

ref #101
Part of epic #55

🤖 Generated with Claude Code

Profile of the #73 regression scenario (seed 19, 30 organisms, 1000
generations; new example long_run_profile) showed evolve dominating and
growing with the genome (~400 genes / ~150 neurons at the end):
speciation spent its time rebuilding and sorting neuron lists in every
compatibility distance, crossover scanned every gene for each neuron
(has_neuron) and inserted genes one by one, and add-connection listed
every possible pair.

- Genome keeps its sorted neuron ids up to date as genes are added, so
  neurons()/has_neuron() no longer rebuild/scan the gene list.
- Compatibility distance walks both neuron lists together, no allocation.
- FirstCompatible speciation stops at the first compatible species (by
  id) instead of measuring the distance to all of them.
- Crossover walks both sorted gene lists together and builds the child's
  genes in one go instead of binary searches and sorted inserts.
- Add-connection counts the new connections and finds the drawn one
  without listing all ~n^2 pairs (same pair for the same draw).
- Gene::cmp compares (in, out) tuples directly (same order).
- Interspecies mating candidates are copied without their compiled
  phenotype (Organism::clone_for_mating).

Every random draw happens in the same order, so results are identical:
tests/long_run_identity_tests.rs checks a digest of 300 generations
recorded before the change (Linux x86_64), and Banco A seeds
1/101/201/301 give the same per-run results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TLmaK0
TLmaK0 merged commit 09dc8c0 into develop Nov 25, 2026
8 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.

1 participant