Skip to content
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 "not disposable" assertion #436

Closed
zepumph opened this issue Apr 19, 2023 · 19 comments
Closed

Factor out "not disposable" assertion #436

zepumph opened this issue Apr 19, 2023 · 19 comments
Assignees

Comments

@zepumph
Copy link
Member

zepumph commented Apr 19, 2023

There are 500 usages of this assertion in our project

assert && assert( false, 'dispose is not supported, exists for the lifetime of the sim' );

We now have a supertype that could write this for us. What about supporting an option to Disposable called isDisposable: boolean. @samreid and I were excited about the potential for factoring this nice assertion out for reuse. We ran into this question over in https://github.com/phetsims/phet-io/issues/1810

@pixelzoom, how would you feel about me moving this into common code?

@pixelzoom
Copy link
Contributor

Fine with me.

@pixelzoom pixelzoom assigned zepumph and unassigned pixelzoom Apr 19, 2023
@pixelzoom
Copy link
Contributor

You'll also need to update the "Memory Management" section of many implementation-notes.md files, for example https://github.com/phetsims/geometric-optics/blob/master/doc/implementation-notes.md#memory-management.

@samreid
Copy link
Member

samreid commented Apr 19, 2023

I observed that My Solar System has 48031 Disposable instances. However, when I added that attribute in Disposable, I saw the memory did not change appreciably:

image

This makes no sense to me since 48031 booleans * 4 bytes per boolean should be measurable.

@samreid
Copy link
Member

samreid commented Apr 19, 2023

Also note that some of the 518 occurrences of that string do not extend Disposable.

@zepumph
Copy link
Member Author

zepumph commented Apr 20, 2023

@samreid do you agree with the recommendation to use Disposable as a way to factor this out? If something doesn't extend Disposable, we could still use a factored out usage of the assertion like:

  /**
   * @public
   */
  dispose() {
    // assert && assert( false, 'dispose is not supported, exists for the lifetime of the sim' );
    Disposable.assertNotDisposable();
  }

But most cases use the option to Disposable.

@zepumph
Copy link
Member Author

zepumph commented Apr 20, 2023

Also, if memory is an issue, we can hide this behind assert so that we don't store that boolean in production. I would be surprised if we ever noticed adding a single boolean to the base class though.

@samreid
Copy link
Member

samreid commented Apr 20, 2023

Can you help me understand my arithmetic mistake?

This makes no sense to me since 48031 booleans * 4 bytes per boolean should be measurable.

It should be 192kb = 0.2MB, right?

@pixelzoom
Copy link
Contributor

@samreid said:

... 4 bytes per boolean

I'm not sure who to believe, but that's debatable. For example...

From https://shevchenkonik.com/blog/memory-size-of-boolean:

A boolean is actually 1 byte. But alignment may cause 4 bytes to be used on a 32-bit platform or 8 bytes on a 64-bit platform.

From https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/boolean-data-type:

Boolean variables are stored as 16-bit (2-byte) numbers,

@zepumph
Copy link
Member Author

zepumph commented Apr 20, 2023

I'm not saying your arithmetic is wrong, I'm saying that it will always be in the noise, and too hard to see that change given how the browser optimizes or implementing a memory heap.

@samreid
Copy link
Member

samreid commented Apr 20, 2023

OK factoring out sounds good to me

@samreid samreid assigned zepumph and unassigned zepumph and samreid Apr 20, 2023
zepumph added a commit to phetsims/scenery that referenced this issue Jun 22, 2023
zepumph added a commit to phetsims/acid-base-solutions that referenced this issue Jun 22, 2023
samreid added a commit to phetsims/tandem that referenced this issue Jun 22, 2023
samreid added a commit to phetsims/fourier-making-waves that referenced this issue Jun 22, 2023
samreid added a commit to phetsims/scenery that referenced this issue Jun 22, 2023
zepumph added a commit to phetsims/tandem that referenced this issue Jun 22, 2023
zepumph added a commit to phetsims/quadrilateral that referenced this issue Jun 22, 2023
zepumph added a commit to phetsims/utterance-queue that referenced this issue Jun 22, 2023
zepumph added a commit to phetsims/scenery-phet that referenced this issue Jun 22, 2023
zepumph added a commit to phetsims/scenery that referenced this issue Jun 22, 2023
pixelzoom added a commit to phetsims/fourier-making-waves that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/equality-explorer that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/gas-properties that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/graphing-quadratics that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/geometric-optics that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/hookes-law that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/models-of-the-hydrogen-atom that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/molecule-polarity that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/natural-selection that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/ph-scale that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/sun that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/reactants-products-and-leftovers that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/vector-addition that referenced this issue Jul 5, 2023
@zepumph
Copy link
Member Author

zepumph commented Jul 5, 2023

@zepumph did you intend to assign this to someone else? Or are we going to leave this issue in limbo, unreviewed?

It is marked for developer meeting discussion, written into the document, as stated in #436 (comment). I was going to take next steps from there.

(1) By searching for "dispose is not supported, exists for the lifetime of the sim", many cases have been missed. I identified an additional 37 cases (addressed in above commits) by searching for "assert && assert( false". I did not inspect the additional 142 cases in code that I'm not responsible for.

That is very helpful. I couldn't think of what else to search for and was going to ask about it during tomorrow's discussion. I will take a look at the other cases in non pixelzoom sims.

(2) In many places, Disposable.assertNotDisposable has been used where isDisposable: false should be used. I'm planning to fix this for my sims.

This is stated in my previous comment about how I didn't think it was worth converting old usages. This is more a pattern that sets us up well for the future. I was not interested in (nor did I think PhET was interested in paying for) me to convert all the Disposable descendants to the better option pattern.

(3) implementation-notes.md that describes this pattern is now incorrect/incomplete. The notes describe overriding dispose, but say nothing about use of isDisposable: false . I'm planning to fix this for my sims.

Sounds good to me! Thanks.

pixelzoom added a commit to phetsims/fourier-making-waves that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/molecule-polarity that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/equality-explorer that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/natural-selection that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/geometric-optics that referenced this issue Jul 5, 2023
pixelzoom added a commit to phetsims/vector-addition that referenced this issue Jul 5, 2023
@pixelzoom
Copy link
Contributor

All the sims that I'm responsible for are now using isDisposable: false where appropriate, and implementation-notes.md has been updated.

@zepumph
Copy link
Member Author

zepumph commented Jul 6, 2023

PSA complete. I'll take a look at the other usages of assert && assert( false . . . before closing

@zepumph
Copy link
Member Author

zepumph commented Jul 10, 2023

Searching for this assert && assert\( false.*(dispose|exists), there were only 9 usages to worry about.

zepumph added a commit to phetsims/number-line-common that referenced this issue Jul 10, 2023
zepumph added a commit to phetsims/collision-lab that referenced this issue Jul 10, 2023
zepumph added a commit to phetsims/circuit-construction-kit-common that referenced this issue Jul 10, 2023
@zepumph zepumph closed this as completed Jul 10, 2023
zepumph added a commit to phetsims/perennial that referenced this issue Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants