-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
factor out constants for option default values #968
Comments
We should also discuss ways to prevent such constants from being mutated. E.g. the pattern used for Vector2.ZERO, Object.freeze,... |
It is unfortunate to move values further away from where they are used, but where a significant amount of memory would be saved, this pattern should be used. |
This pattern should also be used in cases where the same font should be used. For example, SOM repeats this in both screens that have a thermometer: this.compositeThermometerNode = new CompositeThermometerNode( multipleParticleModel, modelViewTransform, {
font: new PhetFont( 20 ), So if I want to change the thermometer font, I have to do it in 2 places, and keep them in sync. |
We discussed this in a recent dev meeting, but for some reason I don't see the conclusions here. My recollection is:
If anyone objects, please comment here for 1/17/19 dev meeting. If there are no objects by that meeting, I'm going to close this issue. |
1/17/19 dev meeting: No objections to above. Closing. |
Look for opportunities to factor out constants for option default values, especially in common code, but also in sim-specific code. This is not necessary for values of type
{number}
,{boolean}
, or{string}
. (JavaScript interns strings.)phetsims/tandem#71 (the memory increase due to IO types) is an example of what can happen when default option values are created for each instance of an object. In that case, memory footprint ballooned by 50-100%.
So where possible, default values should be factored out into constants.
Example:
In the above, every instance of
Particle
will allocate a Vector2 for the default value of optionlocation
. If there are a large number of Particles, this could impact memory footprint, GC, and CPU usage. To avoid this:Note that this is not strictly necessary for classes where you expect to have a small number of instances. But applying this pattern is good habit to develop.
The text was updated successfully, but these errors were encountered: