-
Notifications
You must be signed in to change notification settings - Fork 343
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
Add more __slots__ #1764
Conversation
There was a problem hiding this 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
.
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 |
There was a problem hiding this 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.
@pvcraven any feelings about the slots in Scene? otherwise we can merge |
There was a problem hiding this 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:
- Requiring
__slots__
to be declared on each subclass, which will confuse beginners - Requiring
__weakref__
to be included in__slots__
for weak ref compatibility, which could interfere with chunking / room loading implementations - Requiring
__dict__
to be in slots to add additional values, which would negate memory savings - 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:
- It's intentionally a limited implementation
- Pathing objects are likely to be created for multiple entities, so seeking memory savings there is probably a good idea.
There was a problem hiding this 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?
There was a problem hiding this 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.
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. |
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? |
Keeping the slots on the gui properties |
Sorry for delay