From 178a1258d6ce37baa461aa2831bbdaf92addc0fc Mon Sep 17 00:00:00 2001 From: Vittorio Distefano Date: Wed, 11 Nov 2026 01:20:05 +0100 Subject: [PATCH 1/2] perf(solver): simulate the ruin shuffle without a permutation Restoring only the touched permutation positions removed the per-batch reset, but the shuffle still read random positions of a 4,096-entry array that the reset used to keep in L1, so simulated data-L1 misses rose 7.4% in the gate's ruin case. The partial Fisher-Yates shuffle now records only the positions a swap displaced in a map sized by the largest ruin count; every other position holds its own index. Batches and random draws are unchanged (the reference test still compares them with the full-reset shuffle), each batch costs O(ruin count) time and memory, and no entity-sized array is built. --- .../scalar_neighborhood/cursor/ruin.rs | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin.rs b/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin.rs index b7030ef9..02213789 100644 --- a/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin.rs +++ b/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use rand::rngs::SmallRng; use rand::RngExt; use solverforge_config::RecreateHeuristicType; @@ -33,11 +35,11 @@ where /// Draws `moves_per_step` batches of distinct entity indexes, each the prefix /// of a partial Fisher-Yates shuffle of the identity permutation. /// -/// The permutation is restored to the identity after each batch by rewriting -/// only the positions the shuffle touched: the prefix, and the original -/// positions of the values the batch took. A value that enters the prefix -/// never moves again, so every other position still holds its own index. -/// Restoring costs O(ruin count) per batch instead of O(entity count). +/// The shuffle is simulated without the permutation: `displaced` records only +/// the positions whose value a swap has changed, and every other position +/// holds its own index. A batch therefore costs O(ruin count) time and memory, +/// reads no entity-sized array, and yields exactly the batches and random +/// draws of shuffling a full identity permutation. fn ruin_batches( entity_count: usize, min_ruin_count: usize, @@ -47,7 +49,7 @@ fn ruin_batches( ) -> Vec> { let min = min_ruin_count.min(entity_count); let max = max_ruin_count.min(entity_count); - let mut permutation: Vec = (0..entity_count).collect(); + let mut displaced: HashMap = HashMap::with_capacity(max); (0..moves_per_step) .map(|_| { if entity_count == 0 { @@ -58,16 +60,16 @@ fn ruin_batches( } else { rng.random_range(min..=max) }; + displaced.clear(); + let mut batch = Vec::with_capacity(ruin_count); for index in 0..ruin_count { let swap_index = rng.random_range(index..entity_count); - permutation.swap(index, swap_index); - } - let batch = permutation[..ruin_count].to_vec(); - for (index, entity) in permutation[..ruin_count].iter_mut().enumerate() { - *entity = index; - } - for &entity in &batch { - permutation[entity] = entity; + let at_index = displaced.get(&index).copied().unwrap_or(index); + let at_swap = displaced.get(&swap_index).copied().unwrap_or(swap_index); + batch.push(at_swap); + // Position `index` is never read again: later swaps draw from + // later positions only. + displaced.insert(swap_index, at_index); } batch }) From 07739344f0501dc384895a447fb9fb0dd8f5aee5 Mon Sep 17 00:00:00 2001 From: Vittorio Distefano Date: Wed, 11 Nov 2026 01:20:05 +0100 Subject: [PATCH 2/2] perf(solver): keep the list ruin count choice inlined Adding a non-generic ruin batch function changed LLVM's inlining in the list kernel: RuinCursor::choose_ruin_count stopped being inlined, which re-laid-out the large RuntimeListSlotCursor dispatch that every list neighborhood runs through (nearby_swap +0.37% instructions, +8.9% branch misses on every layout). The four-line function is now always inlined, as v0.26.0 compiled it; the dispatch matches v0.26.0's machine code apart from padding and instruction counts are equal again. Closes #248 --- .../src/heuristic/selector/list_kernel/ruin.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/ruin.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/ruin.rs index 5984423a..86669e28 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/ruin.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/ruin.rs @@ -74,6 +74,7 @@ where } } + #[inline(always)] fn choose_ruin_count(&mut self, eligible_len: usize) -> usize { let min = self.min_ruin_count.min(eligible_len); let max = self.max_ruin_count.min(eligible_len);