From 632b46904378954dc0bf6e552e00b2e4d0bdc381 Mon Sep 17 00:00:00 2001 From: Vittorio Distefano Date: Tue, 1 Dec 2026 10:39:02 +0100 Subject: [PATCH 1/2] perf(solver): size a cartesian cursor's row and pair lists from the last step The cartesian cursor rebuilt its row and pair lists from empty on every step and grew them by reallocation. Whether a reallocation extends in place depends on what the heap holds next to it: after #289 shrank the grouped leaf cursor, the cartesian gate case's reallocations moved instead of extending, and its simulated data-L1 misses rose +112% against v0.31.4 with equal allocation counts. The composition stream state keeps each cartesian node's last row and pair counts, and the next step's cursor allocates both lists once at that size. cartesian, one layout against v0.31.4: sim D1 misses 1353503 (1619900), instructions -0.9%, allocated bytes 2491972 (5193284), peak 278866 (481042), identical order hash. --- .../selector/types/composite/cartesian.rs | 19 +++++++++++++++++-- .../builder/selector/types/composite/state.rs | 19 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/crates/solverforge-solver/src/builder/selector/types/composite/cartesian.rs b/crates/solverforge-solver/src/builder/selector/types/composite/cartesian.rs index 66d1f41c..36850e4d 100644 --- a/crates/solverforge-solver/src/builder/selector/types/composite/cartesian.rs +++ b/crates/solverforge-solver/src/builder/selector/types/composite/cartesian.rs @@ -38,6 +38,13 @@ struct CartesianPair { tabu_signature: MoveTabuSignature, } +/// How many rows and pairs a cartesian cursor held when it closed. +#[derive(Clone, Copy, Default)] +pub(super) struct CartesianSizes { + rows: usize, + pairs: usize, +} + /// Cartesian payloads own selected child carriers; graph edges are stable IDs. /// Right cursors open only after left preview application, using the same arena. pub(super) struct SelectorCompositionCartesianCursor @@ -64,17 +71,25 @@ where left: CompositionNodeId, right: CompositionNodeId, context: MoveStreamContext, + sizes: CartesianSizes, ) -> Self { Self { require_hard_improvement, left, right, - rows: Vec::new(), + rows: Vec::with_capacity(sizes.rows), active_row: None, - pairs: Vec::new(), + pairs: Vec::with_capacity(sizes.pairs), context, } } + + pub(super) fn sizes(&self) -> CartesianSizes { + CartesianSizes { + rows: self.rows.len(), + pairs: self.pairs.len(), + } + } pub(super) fn left(&self) -> CompositionNodeId { self.left } diff --git a/crates/solverforge-solver/src/builder/selector/types/composite/state.rs b/crates/solverforge-solver/src/builder/selector/types/composite/state.rs index 8befbf2b..55bba6a8 100644 --- a/crates/solverforge-solver/src/builder/selector/types/composite/state.rs +++ b/crates/solverforge-solver/src/builder/selector/types/composite/state.rs @@ -1,6 +1,6 @@ //! Indexed mutable cursor arena over one immutable declaration arena. -use super::cartesian::SelectorCompositionCartesianCursor; +use super::cartesian::{CartesianSizes, SelectorCompositionCartesianCursor}; use super::{ ComposedFlat, CompositionNodeId, SelectorComposition, SelectorCompositionNode, SequentialMoveCarrier, StatefulComposedFlat, @@ -20,6 +20,10 @@ use union::SelectorCompositionUnionCursor; /// duplicate state graph; their edges remain in the immutable declaration. pub(super) struct SelectorCompositionStreamState { pub(super) leaves: Vec>, + /// Per cartesian node, the row and pair counts its last cursor reached, + /// so the next step's cursor allocates its row and pair lists once at + /// that size instead of regrowing them pull by pull. + pub(super) cartesian_sizes: Vec, } pub(super) fn new_stream_state( @@ -39,6 +43,7 @@ where _ => None, }) .collect(), + cartesian_sizes: vec![CartesianSizes::default(); selector.nodes.len()], } } @@ -115,6 +120,11 @@ where } } drop(this.flats); + for (id, node) in this.nodes.iter().enumerate() { + if let Some(CursorNode::Cartesian(cursor)) = node { + this.stream_state.cartesian_sizes[id] = cursor.sizes(); + } + } drop(this.nodes); this.stream_state } @@ -193,7 +203,11 @@ where self.validate_cursor(right, director); self.open_node(left, resources, director, context); CursorNode::Cartesian(SelectorCompositionCartesianCursor::new( - require, left, right, context, + require, + left, + right, + context, + self.stream_state.cartesian_sizes[id.index()], )) } }; @@ -218,6 +232,7 @@ where } } CursorNode::Cartesian(cursor) => { + self.stream_state.cartesian_sizes[id.index()] = cursor.sizes(); self.close_node(cursor.left()); self.close_node(cursor.right()); } From 3a55af5aad56784eda624db5859206b31757f89d Mon Sep 17 00:00:00 2001 From: Vittorio Distefano Date: Tue, 1 Dec 2026 10:39:02 +0100 Subject: [PATCH 2/2] perf(solver): keep list kernels' candidate tables across steps Every list neighborhood step opened its slot kernels with empty candidate stores and an empty union table and regrew them one candidate at a time; k-opt and the full list neighborhoods enumerate thousands of candidates per step, so most of their data-L1 misses were reallocation copies of those tables. After #289 changed the heap layout the k_opt gate case moved more of them and failed sim_d1_misses at +5.5% against v0.31.4. The list leaf's stream state keeps each slot kernel's candidate-ID table and the slot union's discovered table; the step's cursor takes them on open and the local-search leaf hands them back on close, as it already does for its own table. One layout against v0.31.4 (sim D1 misses, instructions): k_opt 177635 / 388051, -2.4%; list_change 2871081 / 5943680, -1.6%; union 6954045 / 14367383, -1.7%. Allocation smoke (4 iterations, identical order hashes): list_change 60 allocations / 27432 bytes / 6762 peak (188 / 33583016 / 4201482); k_opt 5492 / 222184 / 6658 (5588 / 2320488 / 269218). --- .../heuristic/selector/decorator/vec_union.rs | 18 +++++ .../heuristic/selector/list_kernel/change.rs | 2 +- .../selector/list_kernel/contiguous_ruin.rs | 2 +- .../heuristic/selector/list_kernel/group.rs | 4 +- .../selector/list_kernel/k_opt/full.rs | 2 +- .../selector/list_kernel/k_opt/nearby.rs | 2 +- .../selector/list_kernel/nearby_change.rs | 2 +- .../selector/list_kernel/nearby_swap.rs | 2 +- .../heuristic/selector/list_kernel/permute.rs | 2 +- .../selector/list_kernel/precedence/cursor.rs | 2 +- .../heuristic/selector/list_kernel/reverse.rs | 2 +- .../heuristic/selector/list_kernel/ruin.rs | 2 +- .../selector/list_kernel/sublist_change.rs | 2 +- .../selector/list_kernel/sublist_swap.rs | 2 +- .../heuristic/selector/list_kernel/swap.rs | 2 +- .../compiler/executor/list_leaf/cursor.rs | 69 ++++++++++++++++++- .../executor/list_leaf/cursor/slot.rs | 37 +++++++++- .../compiler/executor/list_leaf/tests.rs | 1 + .../executor/list_leaf/tests/stream_pools.rs | 67 ++++++++++++++++++ .../compiler/executor/local_search/cursor.rs | 20 ++++-- 20 files changed, 216 insertions(+), 26 deletions(-) create mode 100644 crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/tests/stream_pools.rs diff --git a/crates/solverforge-solver/src/heuristic/selector/decorator/vec_union.rs b/crates/solverforge-solver/src/heuristic/selector/decorator/vec_union.rs index 73a002b6..595f06ae 100644 --- a/crates/solverforge-solver/src/heuristic/selector/decorator/vec_union.rs +++ b/crates/solverforge-solver/src/heuristic/selector/decorator/vec_union.rs @@ -97,6 +97,24 @@ where } } + pub(crate) fn child_cursors_mut(&mut self) -> &mut [C] { + &mut self.cursors + } + + /// Reuses `pool`'s capacity for the discovered-candidate table. + pub(crate) fn with_discovered_pool(mut self, mut pool: Vec<(usize, CandidateId)>) -> Self { + pool.clear(); + self.discovered = pool; + self + } + + /// Hands the discovered-candidate table back, emptied, for the next open. + pub(crate) fn take_discovered_pool(&mut self) -> Vec<(usize, CandidateId)> { + let mut pool = std::mem::take(&mut self.discovered); + pool.clear(); + pool + } + fn push_discovered(&mut self, cursor_index: usize, child_index: CandidateId) -> CandidateId { let global_id = CandidateId::new(self.discovered.len()); self.discovered.push((cursor_index, child_index)); diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/change.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/change.rs index 0d109a53..d6081d0d 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/change.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/change.rs @@ -49,7 +49,7 @@ where S: PlanningSolution, E: ChangeEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, entities: Vec, route_lens: Vec, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/contiguous_ruin.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/contiguous_ruin.rs index 5ecd86c2..d6e9c144 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/contiguous_ruin.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/contiguous_ruin.rs @@ -83,7 +83,7 @@ where S: PlanningSolution, E: MultiRuinEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, probe: P, rng: SmallRng, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/group.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/group.rs index 02500416..14d8c0cd 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/group.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/group.rs @@ -156,7 +156,7 @@ where S: PlanningSolution, E: GroupChangeEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, groups: Vec, entity_lens: Vec, @@ -266,7 +266,7 @@ where S: PlanningSolution, E: GroupSwapEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, groups: Vec, owners: Vec>, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/k_opt/full.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/k_opt/full.rs index 20612d1f..808795c8 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/k_opt/full.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/k_opt/full.rs @@ -19,7 +19,7 @@ where S: PlanningSolution, E: KOptEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, entity_lens: Vec<(usize, usize)>, entity_offset: usize, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/k_opt/nearby.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/k_opt/nearby.rs index b0c3e88d..d1ab6c04 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/k_opt/nearby.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/k_opt/nearby.rs @@ -18,7 +18,7 @@ where E: KOptEmitter, P: KOptDistanceProbe, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, distance: P, entity_lens: Vec<(usize, usize)>, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/nearby_change.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/nearby_change.rs index 401fee7c..f04c2bf9 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/nearby_change.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/nearby_change.rs @@ -22,7 +22,7 @@ where E: ChangeEmitter, P: NearbyChangeProbe, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, probe: P, entities: Vec, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/nearby_swap.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/nearby_swap.rs index a4d32835..157dccfd 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/nearby_swap.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/nearby_swap.rs @@ -22,7 +22,7 @@ where E: SwapEmitter, P: NearbySwapProbe, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, probe: P, entities: Vec, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/permute.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/permute.rs index 49b2ff8e..2cf6197e 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/permute.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/permute.rs @@ -22,7 +22,7 @@ where S: PlanningSolution, E: PermuteEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, entities: Vec, route_lens: Vec, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/precedence/cursor.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/precedence/cursor.rs index b2b6dccf..f84707a0 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/precedence/cursor.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/precedence/cursor.rs @@ -24,7 +24,7 @@ where S: PlanningSolution, E: PrecedenceEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, blocks: Vec, route_graph: PrecedenceRouteGraph, context: MoveStreamContext, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/reverse.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/reverse.rs index d6bac517..d74ad180 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/reverse.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/reverse.rs @@ -20,7 +20,7 @@ where S: PlanningSolution, E: ReverseEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, entities: Vec, route_lens: Vec, 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 1efce820..da358cec 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/ruin.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/ruin.rs @@ -91,7 +91,7 @@ where S: PlanningSolution, E: RuinEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, rng: SmallRng, source_pool: RuinSourcePool, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/sublist_change.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/sublist_change.rs index 0ba475f3..c5be72ab 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/sublist_change.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/sublist_change.rs @@ -39,7 +39,7 @@ where S: PlanningSolution, E: SublistChangeEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, entities: Vec, route_lens: Vec, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/sublist_swap.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/sublist_swap.rs index 93698725..4490adf5 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/sublist_swap.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/sublist_swap.rs @@ -109,7 +109,7 @@ where S: PlanningSolution, E: SublistSwapEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, entities: Vec, route_lens: Vec, diff --git a/crates/solverforge-solver/src/heuristic/selector/list_kernel/swap.rs b/crates/solverforge-solver/src/heuristic/selector/list_kernel/swap.rs index 39ae82dc..97c7709c 100644 --- a/crates/solverforge-solver/src/heuristic/selector/list_kernel/swap.rs +++ b/crates/solverforge-solver/src/heuristic/selector/list_kernel/swap.rs @@ -42,7 +42,7 @@ where S: PlanningSolution, E: SwapEmitter, { - store: CandidateStore, + pub(crate) store: CandidateStore, emitter: E, entities: Vec, route_lens: Vec, diff --git a/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/cursor.rs b/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/cursor.rs index 853b5f4c..27f6aeb4 100644 --- a/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/cursor.rs +++ b/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/cursor.rs @@ -16,7 +16,7 @@ use solverforge_scoring::Director; use crate::builder::context::list_access::ListAccess; use crate::heuristic::selector::decorator::VecUnionMoveCursor; use crate::heuristic::selector::move_selector::{ - CandidateId, MoveCandidateRef, MoveCursor, MoveStreamContext, + CandidateId, CandidateStore, MoveCandidateRef, MoveCursor, MoveStreamContext, }; use crate::heuristic::selector::nearby_list_change::CrossEntityDistanceMeter; use crate::heuristic::selector::seed::scoped_seed; @@ -101,6 +101,15 @@ where ) }) .collect::>(); + let mut slot_cursors = slot_cursors; + for (cursor, positions) in slot_cursors + .iter_mut() + .zip(stream_state.slot_positions_mut(self.plan.slots.len())) + { + if let Some(store) = cursor.store_mut() { + *store = CandidateStore::with_position_pool(std::mem::take(positions)); + } + } let slot_count = slot_cursors.len(); let selection_order = match context.selection_order() { SelectionOrder::Random | SelectionOrder::Shuffled => { @@ -116,7 +125,8 @@ where selection_order, context, vec![1; slot_count], - ), + ) + .with_discovered_pool(std::mem::take(&mut stream_state.discovered)), } } } @@ -126,6 +136,11 @@ where /// interior mutability on the selector definition. pub(crate) struct RuntimeListNeighborhoodStreamState { ruin_rngs: Vec, + /// Each slot kernel's candidate-ID table, handed back when the step's + /// cursor closes so the next step's kernel does not regrow it. + slot_positions: Vec>>, + /// The slot union's discovered-candidate table, likewise. + discovered: Vec<(usize, CandidateId)>, } impl RuntimeListNeighborhoodStreamState { @@ -155,7 +170,55 @@ impl RuntimeListNeighborhoodStreamState { .collect(), _ => Vec::new(), }; - Self { ruin_rngs } + Self { + ruin_rngs, + slot_positions: plan.slots.iter().map(|_| Vec::new()).collect(), + discovered: Vec::new(), + } + } + + fn slot_positions_mut(&mut self, expected_slots: usize) -> &mut [Vec>] { + assert_eq!( + self.slot_positions.len(), + expected_slots, + "list stream state must belong to its frozen leaf" + ); + &mut self.slot_positions + } + + /// Capacity of the slot tables (summed) and of the union table held for + /// the next open. + #[cfg(test)] + pub(crate) fn pool_capacities(&self) -> (usize, usize) { + ( + self.slot_positions.iter().map(Vec::capacity).sum(), + self.discovered.capacity(), + ) + } + + /// Takes the slot kernels' candidate-ID tables back from a closing + /// cursor. + pub(crate) fn reclaim( + &mut self, + cursor: &mut RuntimeListNeighborhoodCursor<'_, S, V, DM, IDM>, + ) where + S: PlanningSolution + Clone + Send + Sync + 'static, + S::Score: Score, + V: Clone + PartialEq + Send + Sync + fmt::Debug + 'static, + DM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter, + IDM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter, + { + self.discovered = cursor.inner.take_discovered_pool(); + for (slot, positions) in cursor + .inner + .child_cursors_mut() + .iter_mut() + .zip(self.slot_positions.iter_mut()) + { + if let Some(store) = slot.store_mut() { + *positions = store.take_position_pool(); + } + } } fn ruin_rngs_mut(&mut self, expected_slots: usize) -> &mut [SmallRng] { diff --git a/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/cursor/slot.rs b/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/cursor/slot.rs index 4d702a44..8784520f 100644 --- a/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/cursor/slot.rs +++ b/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/cursor/slot.rs @@ -15,7 +15,7 @@ use crate::heuristic::selector::list_kernel::{ STATIC_SUBLIST_SWAP_ENTITY_SALT, STATIC_SWAP_SALTS, }; use crate::heuristic::selector::move_selector::{ - CandidateId, MoveCandidateRef, MoveCursor, MoveStreamContext, + CandidateId, CandidateStore, MoveCandidateRef, MoveCursor, MoveStreamContext, }; use crate::heuristic::selector::nearby_list_change::CrossEntityDistanceMeter; @@ -123,6 +123,41 @@ where } } +impl<'a, S, V, DM, IDM> RuntimeListSlotCursor<'a, S, V, DM, IDM> +where + S: PlanningSolution + Clone + Send + Sync + 'static, + S::Score: Score, + V: Clone + PartialEq + Send + Sync + std::fmt::Debug + 'static, + DM: Clone + Send + Sync + std::fmt::Debug + CrossEntityDistanceMeter, + IDM: Clone + Send + Sync + std::fmt::Debug + CrossEntityDistanceMeter, +{ + /// The kernel's candidate store, `None` for an empty slot. + pub(super) fn store_mut( + &mut self, + ) -> Option<&mut CandidateStore>> { + match self { + Self::Change(cursor) => Some(&mut cursor.store), + Self::NearbyChange(cursor) => Some(&mut cursor.store), + Self::Swap(cursor) => Some(&mut cursor.store), + Self::Permute(cursor) => Some(&mut cursor.store), + Self::Precedence(cursor) => Some(&mut cursor.store), + Self::NearbySwap(cursor) => Some(&mut cursor.store), + Self::SublistChange(cursor) => Some(&mut cursor.store), + Self::SublistSwap(cursor) => Some(&mut cursor.store), + Self::Reverse(cursor) => Some(&mut cursor.store), + Self::KOpt(cursor) => Some(&mut cursor.store), + Self::NearbyKOpt(cursor) => Some(&mut cursor.store), + Self::Ruin(cursor) => Some(&mut cursor.store), + Self::GroupRuin(cursor) => Some(&mut cursor.store), + Self::ContiguousRuin(cursor) => Some(&mut cursor.store), + Self::ContiguousGroupRuin(cursor) => Some(&mut cursor.store), + Self::GroupChange(cursor) => Some(&mut cursor.store), + Self::GroupSwap(cursor) => Some(&mut cursor.store), + Self::Empty => None, + } + } +} + impl<'a, S, V, DM, IDM> MoveCursor> for RuntimeListSlotCursor<'a, S, V, DM, IDM> where diff --git a/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/tests.rs b/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/tests.rs index efc8af7b..276333e4 100644 --- a/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/tests.rs +++ b/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/tests.rs @@ -8,4 +8,5 @@ mod leaf_adapter; mod moves; mod parity; mod ruin_access; +mod stream_pools; mod support; diff --git a/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/tests/stream_pools.rs b/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/tests/stream_pools.rs new file mode 100644 index 00000000..8948d21e --- /dev/null +++ b/crates/solverforge-solver/src/runtime/compiler/executor/list_leaf/tests/stream_pools.rs @@ -0,0 +1,67 @@ +//! A list leaf's stream state keeps its kernels' candidate tables across +//! steps. + +use crate::heuristic::selector::move_selector::{MoveCursor, MoveStreamContext}; + +use super::super::super::super::graph::ListLeafKind; +use super::super::{RuntimeListNeighborhoodPlan, RuntimeListNeighborhoodSelector}; +use super::support::{config, director, initial_plan, static_slot_for}; + +#[test] +fn closing_a_step_hands_the_candidate_tables_to_the_next() { + let selector = RuntimeListNeighborhoodSelector::new( + RuntimeListNeighborhoodPlan::from_compiled( + ListLeafKind::Change, + &config(ListLeafKind::Change), + vec![static_slot_for(ListLeafKind::Change)], + Some(7), + ) + .expect("test list plan compiles"), + ); + let score_director = director(initial_plan()); + let mut stream_state = selector.new_stream_state(); + + let mut cursor = selector.open_cursor_with_stream_state( + &mut stream_state, + &score_director, + MoveStreamContext::default(), + ); + let mut first = 0; + while cursor + .next_candidate(score_director.working_solution()) + .is_some() + { + first += 1; + } + assert!(first > 0, "list change emits candidates"); + stream_state.reclaim(&mut cursor); + drop(cursor); + let (slot_capacity, discovered_capacity) = stream_state.pool_capacities(); + assert!( + slot_capacity >= first, + "slot table kept {slot_capacity} of {first}" + ); + assert!( + discovered_capacity >= first, + "union table kept {discovered_capacity} of {first}" + ); + + let mut cursor = selector.open_cursor_with_stream_state( + &mut stream_state, + &score_director, + MoveStreamContext::default(), + ); + assert_eq!( + stream_state.pool_capacities(), + (0, 0), + "an open cursor holds the tables" + ); + let mut second = 0; + while cursor + .next_candidate(score_director.working_solution()) + .is_some() + { + second += 1; + } + assert_eq!(second, first, "a reused table yields the same stream"); +} diff --git a/crates/solverforge-solver/src/runtime/compiler/executor/local_search/cursor.rs b/crates/solverforge-solver/src/runtime/compiler/executor/local_search/cursor.rs index f413f379..c135673f 100644 --- a/crates/solverforge-solver/src/runtime/compiler/executor/local_search/cursor.rs +++ b/crates/solverforge-solver/src/runtime/compiler/executor/local_search/cursor.rs @@ -237,14 +237,20 @@ where .zip(stream_state.positions.iter_mut()) { *positions = leaf_cursor.store.take_position_pool(); - if let ( - RuntimeNeighborhoodLeafCursorInner::Grouped(grouped), - super::leaf::RuntimeNeighborhoodLeafStreamState::Grouped(pooled), - ) = (&mut leaf_cursor.inner, slot) - { - if let Some(grouped) = grouped.take() { - *pooled = grouped.into_pooled_assignment(); + match (&mut leaf_cursor.inner, slot) { + ( + RuntimeNeighborhoodLeafCursorInner::Grouped(grouped), + super::leaf::RuntimeNeighborhoodLeafStreamState::Grouped(pooled), + ) => { + if let Some(grouped) = grouped.take() { + *pooled = grouped.into_pooled_assignment(); + } } + ( + RuntimeNeighborhoodLeafCursorInner::List(cursor), + super::leaf::RuntimeNeighborhoodLeafStreamState::List(state), + ) => state.reclaim(cursor), + _ => {} } } }