Skip to content

Add more __slots__ #1764

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

Merged
merged 6 commits into from
Sep 11, 2023
Merged

Conversation

gran4
Copy link
Contributor

@gran4 gran4 commented May 9, 2023

Sorry for delay

Copy link
Member

@pushfoo pushfoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hesitant to add __slots__ where it isn't crucial, especially in the UI stack. The primary benefit of slots is saving memory on types which will have a lot of instances, such as Sprite instances. We put __slots__ on BasicSprite rather than Sprite since it's a performance-optimized, limited-capability version of Sprite.

@gran4
Copy link
Contributor Author

gran4 commented May 10, 2023

I tried to add slots were it is useful, and not obstructive. The purpose is to stop bad design by people and optimize it as well. I don't think people should be adding attributes to scene anyways or to anything in paths.py.

P.S: Maybe I could take it out of property.py.(Lets be honest, no one should be adding values to a list or a dict anyways so it is more for readability and to warning)

P.P.S: I don't think you should be adding any variable to GUI anyways

Copy link
Member

@eruvanos eruvanos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Property changes within the GUI code looks fine, we already had them in some places and the additional one is fine.

@eruvanos
Copy link
Member

@pvcraven any feelings about the slots in Scene? otherwise we can merge

Copy link
Member

@pushfoo pushfoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tl;dr After further thought, using slots on Scene seems very risky

Scene seems like a good target for subclassing to customize behavior. Using slots on Scene may make such customization much harder. Examples include:

  1. Requiring __slots__ to be declared on each subclass, which will confuse beginners
  2. Requiring __weakref__ to be included in __slots__ for weak ref compatibility, which could interfere with chunking / room loading implementations
  3. Requiring __dict__ to be in slots to add additional values, which would negate memory savings
  4. Interference with mixins, which could be useful for different game modes (Parallax vs Non-Parallax after DragonMoffon & I take another look at the Background API)

Disadvantages 1-3 are paraphrased from Fluent Python

As to GUI components, I haven't yet formed a strong opinion. However,

Lets be honest, no one should be adding values to a list or a dict anyways so it is more for readability and to warning

I don't understand what gran4 meant by this. It reads like an attempt to force a more functional style, despite arcade not using that approach. Also, the existence of UserList and UserDict in the Python standard library contradict this idea.

For the A* components, using __slots__ seems fine since:

  1. It's intentionally a limited implementation
  2. Pathing objects are likely to be created for multiple entities, so seeking memory savings there is probably a good idea.

Copy link
Member

@eruvanos eruvanos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change that?

@gran4 gran4 requested review from pushfoo and eruvanos May 28, 2023 15:28
Copy link
Member

@pushfoo pushfoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much cleaner than before. I have only one change request.

@pvcraven
Copy link
Member

I'm not sure what slots gives us by adding it to scene. What problem are we solving?

For the AStar, can we point to speed/memory improvements with using slots? I need to see stats showing the issue and the solution.

@gran4
Copy link
Contributor Author

gran4 commented Jun 4, 2023

I'm not sure what slots gives us by adding it to scene. What problem are we solving?

For the AStar, can we point to speed/memory improvements with using slots? I need to see stats showing the issue and the solution.

I agree, but wrong PR.

@einarf
Copy link
Member

einarf commented Jun 9, 2023

I'd say keep slots in types that have many instances. The GUI ones probably makes sense, but do we really end up with that many barrier lists etc?

@gran4 gran4 requested a review from pushfoo August 6, 2023 20:11
@einarf
Copy link
Member

einarf commented Sep 11, 2023

Keeping the slots on the gui properties

@einarf einarf merged commit 57afec7 into pythonarcade:development Sep 11, 2023
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

Successfully merging this pull request may close these issues.

5 participants