From 3bf4a8f8e692dd0f2159e9734e922d334aed1bdc Mon Sep 17 00:00:00 2001
From: David Sehnal <dsehnal@users.noreply.github.com>
Date: Tue, 23 May 2023 09:08:00 +0200
Subject: [PATCH] optimize computeInterUnitBonds (#830)

---
 CHANGELOG.md                                       |  2 ++
 .../structure/unit/bonds/inter-compute.ts          | 14 +++++++++-----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index ab5952c1a..0411c2d7f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@ Note that since we don't clearly distinguish between a public and private interf
 
 ## [Unreleased]
 
+- Optimize inter unit bond compute
+
 ## [v3.35.0] - 2023-05-14
 
 - Enable odd dash count (1,3,5)
diff --git a/src/mol-model/structure/structure/unit/bonds/inter-compute.ts b/src/mol-model/structure/structure/unit/bonds/inter-compute.ts
index 1a8603945..7186e8efa 100644
--- a/src/mol-model/structure/structure/unit/bonds/inter-compute.ts
+++ b/src/mol-model/structure/structure/unit/bonds/inter-compute.ts
@@ -243,10 +243,8 @@ function computeInterUnitBonds(structure: Structure, props?: Partial<InterBondCo
         ...p,
         validUnit: (props && props.validUnit) || (u => Unit.isAtomic(u)),
         validUnitPair: (props && props.validUnitPair) || ((s, a, b) => {
-            // In case both units have a struct conn record, ignore other criteria
-            if (hasCommonStructConnRecord(a, b)) {
-                return Structure.validUnitPair(s, a, b);
-            }
+            const isValidPair = Structure.validUnitPair(s, a, b);
+            if (!isValidPair) return false;
 
             const mtA = a.model.atomicHierarchy.derived.residue.moleculeType;
             const mtB = b.model.atomicHierarchy.derived.residue.moleculeType;
@@ -258,7 +256,13 @@ function computeInterUnitBonds(structure: Structure, props?: Partial<InterBondCo
             const notIonA = (!Unit.isAtomic(a) || mtA[a.residueIndex[a.elements[0]]] !== MoleculeType.Ion);
             const notIonB = (!Unit.isAtomic(b) || mtB[b.residueIndex[b.elements[0]]] !== MoleculeType.Ion);
             const notIon = notIonA && notIonB;
-            return Structure.validUnitPair(s, a, b) && (notWater || !p.ignoreWater) && (notIon || !p.ignoreIon);
+
+            const check = (notWater || !p.ignoreWater) && (notIon || !p.ignoreIon);
+            if (!check) {
+                // In case both units have a struct conn record, ignore other criteria
+                return hasCommonStructConnRecord(a, b);
+            }
+            return true;
         }),
     });
 }
-- 
GitLab