Skip to content

refactor(perception): Accelerate Ring Perception with Bond-Aware Adjacency#16

Merged
TKanX merged 5 commits intomainfrom
feature/15-accelerate-ring-perception-with-bond-aware-adjacency
Dec 9, 2025
Merged

refactor(perception): Accelerate Ring Perception with Bond-Aware Adjacency#16
TKanX merged 5 commits intomainfrom
feature/15-accelerate-ring-perception-with-bond-aware-adjacency

Conversation

@TKanX
Copy link
Copy Markdown
Member

@TKanX TKanX commented Dec 9, 2025

Summary:

Significantly optimizes the ring perception algorithm by pre-calculating and caching bond identifiers within the adjacency list. Previously, the cycle detection logic performed a linear search (O(E)) to find the bond connecting two atoms during every step of the graph traversal. By introducing a NeighborBond struct and an adjacency_with_bonds cache in AnnotatedMolecule, this lookup becomes O(1). Additionally, the BFS traversal now reuses scratch memory (RingSearchWorkspace) to eliminate repeated allocations.

Changes:

  • Optimized Graph Representation:

    • Introduced NeighborBond struct to bundle neighbor ID, bond ID, and bond order.
    • Added adjacency_with_bonds to AnnotatedMolecule, pre-populated during initialization.
    • Updated AnnotatedMolecule::new to build this enhanced adjacency list efficiently.
  • Accelerated Cycle Detection:

    • Refactored shortest_path_bfs to use the cached bond_id from adjacency_with_bonds directly, removing the expensive O(E) bond lookup loop.
    • Implemented RingSearchWorkspace to reuse queue, visited, and parent buffers across all BFS runs, drastically reducing heap allocations.
    • Updated enumerate_cycle_candidates to utilize the new workspace.
  • Verified Correctness:

    • Added unit tests verifying the correctness of adjacency_with_bonds population.
    • Added a test case for a fused ring system to ensure the optimized BFS still correctly identifies minimal cycle bases.

@TKanX TKanX self-assigned this Dec 9, 2025
Copilot AI review requested due to automatic review settings December 9, 2025 12:36
@TKanX TKanX added enhancement ✨ New feature or request performance ⚡ Performance improvements and code optimizations labels Dec 9, 2025
@TKanX TKanX linked an issue Dec 9, 2025 that may be closed by this pull request
10 tasks
Copy link
Copy Markdown
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 optimizes the ring perception algorithm by eliminating expensive bond lookups during cycle detection. The optimization introduces a bond-aware adjacency list (adjacency_with_bonds) that pre-caches bond identifiers, transforming a critical O(E) lookup into O(1), and adds workspace reuse to avoid repeated allocations during BFS traversal.

Key Changes:

  • Introduced NeighborBond struct bundling neighbor ID, bond ID, and bond order for direct access
  • Added adjacency_with_bonds field to AnnotatedMolecule with synchronized initialization
  • Implemented RingSearchWorkspace to reuse BFS buffers across all ring searches

Reviewed changes

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

File Description
src/perception/model.rs Adds NeighborBond struct and adjacency_with_bonds field; updates AnnotatedMolecule::new to populate both adjacency representations; adds unit tests verifying the new adjacency structure
src/perception/rings.rs Introduces RingSearchWorkspace for buffer reuse; refactors shortest_path_bfs to use cached bond IDs from adjacency_with_bonds; updates enumerate_cycle_candidates to accept and reuse workspace; adds test for fused ring systems
docs/02_perception.md Documents the optimization approach in the ring detection section

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/02_perception.md
Comment thread src/perception/model.rs
@TKanX TKanX merged commit 8304a03 into main Dec 9, 2025
8 checks passed
@TKanX TKanX deleted the feature/15-accelerate-ring-perception-with-bond-aware-adjacency branch December 9, 2025 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.

Accelerate Ring Perception with Bond-Aware Adjacency

2 participants