Browse Source

Improved StructureElement.Loci.size performance

- inlined code
- important for marking large cellpack models
Alexander Rose 3 years ago
parent
commit
2482ef92af
3 changed files with 19 additions and 3 deletions
  1. 1 0
      CHANGELOG.md
  2. 6 0
      src/mol-data/int/tuple.ts
  3. 12 3
      src/mol-model/structure/structure/element/loci.ts

+ 1 - 0
CHANGELOG.md

@@ -10,6 +10,7 @@ Note that since we don't clearly distinguish between a public and private interf
 - Add ``Mesh`` processing helper ``.smoothEdges``
 - Smooth border of molecular-surface with ``includeParent`` enabled
 - Hide ``includeParent`` option from gaussian-surface visuals (not particularly useful)
+- Improved ``StructureElement.Loci.size`` performance (for marking large cellpack models)
 
 ## [v2.2.2] - 2021-08-11
 

+ 6 - 0
src/mol-data/int/tuple.ts

@@ -36,6 +36,12 @@ namespace IntTuple {
         return _float64[0] as any;
     }
 
+    /** snd - fst */
+    export function diff(t: IntTuple) {
+        _float64[0] = t as any;
+        return _int32[1] - _int32[0];
+    }
+
     export function fst(t: IntTuple): number {
         _float64[0] = t as any;
         return _int32[0];

+ 12 - 3
src/mol-model/structure/structure/element/loci.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2017-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2017-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author David Sehnal <david.sehnal@gmail.com>
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
@@ -22,9 +22,10 @@ import { NumberArray } from '../../../../mol-util/type-helpers';
 import { StructureProperties } from '../properties';
 import { BoundaryHelper } from '../../../../mol-math/geometry/boundary-helper';
 import { Boundary } from '../../../../mol-math/geometry/boundary';
+import { IntTuple } from '../../../../mol-data/int/tuple';
 
 // avoiding namespace lookup improved performance in Chrome (Aug 2020)
-const osSize = OrderedSet.size;
+const itDiff = IntTuple.diff;
 
 /** Represents multiple structure element index locations */
 export interface Loci {
@@ -74,7 +75,15 @@ export namespace Loci {
 
     export function size(loci: Loci) {
         let s = 0;
-        for (const u of loci.elements) s += osSize(u.indices);
+        // inlined for max performance, crucial for marking large cellpack models
+        // `for (const u of loci.elements) s += OrderedSet.size(u.indices);`
+        for (const { indices } of loci.elements) {
+            if (typeof indices === 'number') {
+                s += itDiff(indices as IntTuple);
+            } else {
+                s += (indices as SortedArray).length;
+            }
+        }
         return s;
     }