Skip to content
This repository was archived by the owner on Mar 10, 2026. It is now read-only.

refactor(core): Refactor Core Models with SlotMap for Stability and Mutability#8

Merged
TKanX merged 20 commits intomainfrom
feature/7-refactor-core-data-models-for-mutability-safety-and-performance
Jul 3, 2025
Merged

refactor(core): Refactor Core Models with SlotMap for Stability and Mutability#8
TKanX merged 20 commits intomainfrom
feature/7-refactor-core-data-models-for-mutability-safety-and-performance

Conversation

@TKanX
Copy link
Member

@TKanX TKanX commented Jul 3, 2025

Summary:

Executes a major refactoring of the core data models within scream-core, replacing the previous Vec-based storage and usize indexing with slotmap. This provides stable, generational IDs (AtomId, ResidueId, ChainId), which guarantees that references remain valid even after additions or deletions of other entities. This change fundamentally enables safe mutability, introducing methods to add and remove entities directly from the MolecularSystem. Consequently, the separate MolecularSystemBuilder has been removed, and its functionality is now integrated into MolecularSystem itself, leading to a more robust, performant, and ergonomic API.

Changes:

  • Stable ID Generation with slotmap:

    • Introduced the slotmap crate to manage Atom, Residue, and Chain collections, replacing the fragile Vec/usize index system.
    • Created stable, typed IDs: AtomId, ResidueId, and ChainId in a new ids.rs module.
  • Data Structure and API Refactoring:

    • Restructured MolecularSystem to use SlotMap for primary data storage, flattening the hierarchy for direct access to all entities.
    • Implemented methods for safe, direct mutation of the molecular system: add_atom_to_residue, add_bond, remove_atom, and remove_residue.
    • Removed the MolecularSystemBuilder and integrated its construction logic directly into MolecularSystem's public methods (add_chain, add_residue, etc.).
    • Updated Atom, Residue, Chain, and Bond models to use the new stable IDs, simplifying their structure.
  • Performance Enhancements:

    • Added a bond_adjacency SecondaryMap to MolecularSystem for efficient, cached lookups of an atom's bonded neighbors.
    • Changed file I/O operations to use BufWriter, improving write performance.
  • BGF Parser/Writer Update:

    • Completely refactored the BgfFile parser and writer (core::io::bgf) to work with the new slotmap-based data models and mutable API.
    • The parser now handles entity creation and bond connections more robustly, with improved error handling for missing atoms in CONECT records.

TKanX added 18 commits July 2, 2025 18:27
@TKanX TKanX self-assigned this Jul 3, 2025
Copilot AI review requested due to automatic review settings July 3, 2025 07:03
@TKanX TKanX added enhancement ✨ New feature or request performance ⚡ Performance improvements and code optimizations labels Jul 3, 2025
@TKanX TKanX linked an issue Jul 3, 2025 that may be closed by this pull request
18 tasks
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the core data models to use slotmap for stable, generational IDs and integrates the previous builder functionality directly into MolecularSystem, while updating the BGF parser/writer and improving I/O performance.

  • Replace Vec/usize indexing with typed AtomId, ResidueId, and ChainId via slotmap
  • Remove MolecularSystemBuilder, move entity-addition methods into MolecularSystem
  • Update BGF parser/writer to work with the new mutable API and use BufWriter

Reviewed Changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/scream-core/src/core/models/ids.rs Introduce AtomId, ResidueId, ChainId via slotmap
crates/scream-core/src/core/models/topology.rs Update Bond to use AtomId, add contains method
crates/scream-core/src/core/models/residue.rs Change Residue to track AtomIds, update mapping
crates/scream-core/src/core/models/chain.rs Change Chain to track ResidueIds, remove old maps
crates/scream-core/src/core/models/system.rs Migrate MolecularSystem to slotmap storage, add mutators
crates/scream-core/src/core/models/mod.rs Remove builder module, add ids
crates/scream-core/src/core/io/traits.rs Switch to BufWriter for write performance
crates/scream-core/src/core/io/bgf.rs Fully refactor BGF parser/writer for new model API
crates/scream-core/Cargo.toml Add slotmap dependency
Comments suppressed due to low confidence (2)

crates/scream-core/src/core/models/residue.rs:9

  • The comment says "Indices of atoms" but this field now holds AtomIds, not raw indices. Please update the comment to "IDs of atoms belonging to this residue".
    pub(crate) atoms: Vec<AtomId>,          // Indices of atoms belonging to this residue

crates/scream-core/src/core/models/chain.rs:73

  • After refactoring to ResidueId, existing tests for parsing and display of other ChainType variants have been removed. Consider adding back tests for DNA, RNA, Ligand, and Other variants to maintain coverage of all parsing/display branches.
mod tests {

@TKanX TKanX merged commit 1e7b625 into main Jul 3, 2025
2 checks passed
@TKanX TKanX deleted the feature/7-refactor-core-data-models-for-mutability-safety-and-performance branch July 3, 2025 07:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement ✨ New feature or request performance ⚡ Performance improvements and code optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor Core Data Models for Mutability, Safety, and Performance

2 participants