-
Notifications
You must be signed in to change notification settings - Fork 0
Implement High-Performance Incremental Energy Engine with Zero-Clone Optimization #39
Description
Description:
Implements a transformative refactoring of the core optimization engine to achieve an order-of-magnitude performance increase. This overhaul replaces the current model of full-system re-evaluation with a hyper-efficient Incremental Energy Manager that utilizes a zero-clone, "try-revert" transaction model.
The primary goal is to eliminate the two main performance bottlenecks identified in complex system simulations: massive system call overhead (sys time) from frequent memory allocations (system.clone()) and redundant O(n) calculations for unchanged parts of the system. This will be achieved by maintaining a live energy state and calculating only the energy delta of conformational changes, enabling rapid exploration of the conformational space.
Tasks:
-
Phase 1: Architect the Incremental Energy Manager (
engine/energy.rs)- Create a new module
engine/energy.rsand a public structIncrementalEnergyManager. - Implement the
new()constructor for the manager. This includes a one-time, O(n²) pre-computation to build aninteraction_cachethat stores the sum of interaction energies for each active sidechain with all other active sidechains. - Unit Test: Write a comprehensive unit test for
IncrementalEnergyManager::new()that verifies both the initialcurrent_scoreand the per-residueinteraction_cachevalues against a manually calculated "golden" dataset.
- Create a new module
-
Phase 2: Implement the Zero-Clone "Try-Revert" Mechanism
- In
engine/placement.rs:- Introduce a new RAII-based struct
PlacementTransaction. - Implement an
apply()method onPlacementTransactionthat performs an in-place rotamer placement on the manager'sworking_state. It must record the original rotamer index before modification. - Implement the
Droptrait forPlacementTransactionto automatically revert any applied changes, ensuring the system state is restored when the transaction goes out of scope.
- Introduce a new RAII-based struct
- In
engine/energy.rs:- Implement
IncrementalEnergyManager::start_transaction()to create and return aPlacementTransaction. - Refactor
IncrementalEnergyManager::calculate_delta_for_swapto be&mut self. Inside, use aPlacementTransactionto temporarily apply the new rotamer, calculate the energy delta against the now-modified system, and let the transaction automatically revert the change. This must completely eliminatesystem.clone()from this critical path.
- Implement
- In
-
Phase 3: Implement Efficient State & Cache Updates
- In
engine/energy.rs:- Implement
IncrementalEnergyManager::apply_swap. This method will:- Update the
current_scorewith the pre-calculated delta in O(1). - Permanently apply the new rotamer to
working_state.system. - Perform an efficient O(n) update of the
interaction_cache, recalculating interaction terms only for the atom pairs involving the changed residue.
- Update the
- Implement
- Integration Test: Write a crucial test for
calculate_delta_for_swapandapply_swap. It must verify that after applying a swap, the manager's newcurrent_scoreis numerically identical to a full, non-incremental rescoring of the newworking_state.system.
- In
-
Phase 4: Integrate into Workflows (
workflows/place.rs)- In
engine/state.rs: Add a new methodOptimizationState::submit_solution(score, state)to allow submitting solutions from the energy manager. - In
workflows/place.rs:- Refactor
final_refinement:- Initialize
IncrementalEnergyManageronce before the main loop. - Replace all full system rescoring with calls to
energy_manager.calculate_delta_for_swapto find the best move. - Use
energy_manager.apply_swapto accept a move. - Use
state.submit_solution()to update the candidate solutions.
- Initialize
- Refactor
resolve_clashes:- Initialize
IncrementalEnergyManagerat the start. - After
doublet_optimization::runreturns the best rotamer pair, use the manager'scalculate_delta...andapply_swapmethods sequentially to apply the two-residue change and update the energy state. - Use
state.submit_solution()to record the new state.
- Initialize
- Refactor
- In