From 55b2dd8cf93605384aa6536b35cef58fe6fb8f0d Mon Sep 17 00:00:00 2001 From: denz1994 Date: Mon, 9 Mar 2020 13:03:28 -0400 Subject: [PATCH] Use mutable form of Vector2.add() instead of Vector2.plus() (memory optimization) https://github.com/phetsims/build-a-molecule/issues/115 --- js/common/model/Atom2.js | 4 ++-- js/common/model/Kit.js | 8 ++++---- js/common/model/LewisDotModel.js | 2 +- js/common/model/Molecule.js | 3 +-- js/common/view/MoleculeBondNode.js | 2 +- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/js/common/model/Atom2.js b/js/common/model/Atom2.js index 09ed9c5a..89a7089b 100644 --- a/js/common/model/Atom2.js +++ b/js/common/model/Atom2.js @@ -133,8 +133,8 @@ class Atom2 extends Atom { } translatePositionAndDestination( delta ) { - this.positionProperty.value = this.positionProperty.value.plus( delta ); - this.destinationProperty.value = this.destinationProperty.value.plus( delta ); + this.positionProperty.value = this.positionProperty.value.add( delta ); + this.destinationProperty.value.add( delta ); } setPositionAndDestination( point ) { diff --git a/js/common/model/Kit.js b/js/common/model/Kit.js index 0ade7849..ea726677 100644 --- a/js/common/model/Kit.js +++ b/js/common/model/Kit.js @@ -488,8 +488,8 @@ class Kit { foundOverlap = true; // get perturbed centers. this is so that if two molecules have the exact same centers, we will push them away - const aCenter = aBounds.center.plus( new Vector2( phet.joist.random.nextDouble() - 0.5, phet.joist.random.nextDouble() - 0.5 ) ); - const bCenter = bBounds.center.plus( new Vector2( phet.joist.random.nextDouble() - 0.5, phet.joist.random.nextDouble() - 0.5 ) ); + const aCenter = aBounds.center.add( new Vector2( phet.joist.random.nextDouble() - 0.5, phet.joist.random.nextDouble() - 0.5 ) ); + const bCenter = bBounds.center.add( new Vector2( phet.joist.random.nextDouble() - 0.5, phet.joist.random.nextDouble() - 0.5 ) ); // delta from center of A to center of B, scaled to half of our push amount. const delta = bCenter.minus( aCenter ).normalized().times( pushAmount ); @@ -648,7 +648,7 @@ class Kit { // cause all atoms in the molecule to move to that location const delta = bestLocation.idealLocation.minus( bestLocation.b.positionProperty.value ); this.getMolecule( bestLocation.b ).atoms.forEach( atomInMolecule => { - atomInMolecule.setPositionAndDestination( atomInMolecule.positionProperty.value.plus( delta ) ); + atomInMolecule.setPositionAndDestination( atomInMolecule.positionProperty.value.add( delta ) ); } ); // we now will bond the atom @@ -695,7 +695,7 @@ class BondingOption { this.b = b; // The location the atom should be placed - this.idealLocation = a.positionProperty.value.plus( direction.vector.times( a.covalentRadius + b.covalentRadius ) ); + this.idealLocation = a.positionProperty.value.add( direction.vector.times( a.covalentRadius + b.covalentRadius ) ); } } diff --git a/js/common/model/LewisDotModel.js b/js/common/model/LewisDotModel.js index f40b7ae0..e30b9c3d 100644 --- a/js/common/model/LewisDotModel.js +++ b/js/common/model/LewisDotModel.js @@ -168,7 +168,7 @@ class LewisDotModel { // if this atom isn't excluded if ( otherDot.atom !== excludedAtom ) { - success = this.mapMolecule( coordinates.plus( direction.vector ), otherDot.atom, atom, coordinateMap ); + success = this.mapMolecule( coordinates.add( direction.vector ), otherDot.atom, atom, coordinateMap ); // if we had a failure mapping that one, bail out if ( !success ) { diff --git a/js/common/model/Molecule.js b/js/common/model/Molecule.js index 533739d3..8a8022c6 100644 --- a/js/common/model/Molecule.js +++ b/js/common/model/Molecule.js @@ -46,9 +46,8 @@ class Molecule extends MoleculeStructure { */ shiftDestination( delta ) { this.atoms.forEach( atom => { - // TODO: memory: consider alternate mutable form atom.destination.add( delta ) atom.isSeparatingProperty.value = true; - atom.destinationProperty.value = atom.destinationProperty.value.plus( delta ); + atom.destinationProperty.value.add( delta ); } ); } } diff --git a/js/common/view/MoleculeBondNode.js b/js/common/view/MoleculeBondNode.js index f579e92a..35df029d 100644 --- a/js/common/view/MoleculeBondNode.js +++ b/js/common/view/MoleculeBondNode.js @@ -150,7 +150,7 @@ class MoleculeBondNode extends Node { if ( orientation.magnitude > 0 ) { orientation.normalize(); } - const location = orientation.times( this.a.covalentRadius ).plus( this.a.positionProperty.value ); + const location = orientation.times( this.a.covalentRadius ).add( this.a.positionProperty.value ); this.setTranslation( BAMConstants.MODEL_VIEW_TRANSFORM.modelToViewPosition( location ) ); }; this.a.positionProperty.link( this.positionListener );