Skip to content

Commit

Permalink
Simplify pruning, see #319
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jan 28, 2019
1 parent 62ff503 commit c1dcfbf
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions js/common/model/TemporalMask.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,18 @@ define( require => {
}

/**
* Remove delta values that are so old they can no longer impact the model, to avoid memory leaks.
* Remove delta values that are so old they can no longer impact the model, to avoid memory leaks and too much CPU
* @param {number} maxDistance - the furthest a point can be from a source
* @param {number} numberOfSteps - integer number of times the wave has been stepped on the lattice
* @public
*/
prune( maxDistance, numberOfSteps ) {
for ( let k = 0; k < this.deltas.length; k++ ) {
const delta = this.deltas[ k ];

const steps = numberOfSteps - delta.numberOfSteps;

// max numberOfSteps is across the diagonal of the lattice, but don't remove the last elements or the wave could
// clear, see https://github.com/phetsims/wave-interference/issues/319
if ( this.deltas.length > 3 &&

// d = vt, t=d/v
( steps > maxDistance / Lattice.WAVE_SPEED ||

// too many deltas
this.deltas.length > 10 )
) {
this.deltas.splice( k, 1 );
k--;
}
// Save enough deltas so that even if the user toggles the source on and off rapidly, the effect will be further
// from the source. But don't save so many deltas that performance is degraded.
// See https://github.com/phetsims/wave-interference/issues/319
while ( this.deltas.length > 10 ) {
this.deltas.shift(); // remove oldest deltas first
}
}

Expand Down

0 comments on commit c1dcfbf

Please sign in to comment.