Skip to content

Speed up Compound.add and Compound.remove on large compounds - #1388

Merged
chrisjonesBSU merged 5 commits into
mosdef-hub:developfrom
joelaforet:perf/compound-bond-graph
Nov 20, 2026
Merged

chrisjonesBSU merged 5 commits into
mosdef-hub:developfrom
joelaforet:perf/compound-bond-graph

Conversation

@joelaforet

Copy link
Copy Markdown

PR Summary:

Three small changes in Compound that remove a copy of the whole bond graph from add and remove. None of them change end behavior.

  • Compound.add merged the child's bond graph into the root's with nx.compose, which builds a new graph containing every node and edge of the root on each call. BondGraph is an nx.Graph, so self.root.bond_graph.update(child_graph) gives the same graph in place. This is changed in both the single-compound and the list branch of add.
  • _remove iterated a removed particle's neighbors over self.root.bond_graph.copy() so it could delete bonds during the loop. Listing the neighbors first, list(graph.neighbors(part)), is enough.
  • _prune_ghost_ports checked each port's anchor against a list of all particles. It now uses a set.

Timings on develop (a862a5a) and on this branch, building a compound from ethanes added one at a time and then removing one hydrogen per ethane one at a time:

atoms add, develop add, this PR remove, develop remove, this PR
4,000 1.99 s 0.10 s 3.59 s 1.30 s
16,000 35.4 s 0.37 s 95.7 s 21.0 s
Benchmark script
import time
import mbuild as mb
from mbuild.lib.molecules import Ethane

def build(n):
    eth = Ethane()
    host = mb.Compound()
    t = time.perf_counter()
    for i in range(n):
        c = mb.clone(eth)
        c.translate([0, 0, 0.5 * i])
        host.add(c)
    t_add = time.perf_counter() - t
    hydrogens = list(host.particles_by_name("H"))[:n]
    t = time.perf_counter()
    for h in hydrogens:
        host.remove(h)
    return t_add, time.perf_counter() - t

for n in (500, 2000):
    print(n * 8, build(n))

I ran into this while building large proteins with many attached groups (#1355), where each add or remove scaled with the size of the whole structure.

Two tests are added. One checks that adding compounds, either one at a time or as a list, keeps every bond and its order in the root bond graph. The other checks that removing one bonded particle removes only that particle's bond.

PR Checklist


  • Includes appropriate unit test(s)
  • Appropriate docstring(s) are added/updated (no public API change)
  • Code is (approximately) PEP8 compliant
  • Issue(s) raised/addressed? Related to Biomolecule loading and modification #1355

Compound.add composed the root bond graph with nx.compose, which copies the
whole root graph on every add; merging the child's graph in place with
Graph.update gives the same graph without the copy. _prune_ghost_ports tested
port anchors for membership in a list of all particles; a set makes each test
O(1). Together these cut glycan placement on the BG505 trimer (56k atoms,
63 attaches) by about 12%.
…e bond graph

Compound._remove copied the whole root bond graph to iterate one part's
neighbors while deleting its bonds; listing the neighbors first is enough.
On a 56k-atom protein each copy cost ~0.1 s, paid on every remove.
…emove

Adding compounds one at a time or as a list keeps every bond and its
order in the root bond graph, and removing one bonded particle drops
only that particle's bond.
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.74%. Comparing base (8fa187b) to head (167f464).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1388      +/-   ##
===========================================
+ Coverage    82.71%   82.74%   +0.03%     
===========================================
  Files           72       72              
  Lines         8794     8794              
===========================================
+ Hits          7274     7277       +3     
+ Misses        1520     1517       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chrisjonesBSU chrisjonesBSU left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good to me. There are some doc strings that describe the old behavior, and the reason for the change. This kind of info is relevant to this specific PR, but to someone reading them in the future they won't make sense without context.

Comment thread mbuild/compound.py Resolved
Comment thread mbuild/compound.py Resolved
@chrisjonesBSU
chrisjonesBSU merged commit d794ac5 into mosdef-hub:develop Nov 20, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants