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

[Python] Usage of __slots__ or @dataclass(slots=True) in python classes for reducing memory usage and performance improvement #56

Open
Martin15135215 opened this issue Oct 7, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@Martin15135215
Copy link

Martin15135215 commented Oct 7, 2023

I saw the Pycon 2016 talk from Nina Zakharenko about Memory Management in Python and at the 21:19 min, she introduced the dunker slots attribute to define the attributes. Because with that, you can't assign at runtime new attributes to the class instance. This reduces the memory usage and improves performance.

To reduce boilerplate, there is also the possibility to use @dataclass(slots=True) as shown in this video, see also the python documentation → https://docs.python.org/3.10/library/dataclasses.html

@adtzlr
Copy link
Owner

adtzlr commented Oct 20, 2023

You mean to use this in the handler-classes? Thanks, I didn't know that before! Also, I never used ``@dataclass()` (with or without slots) in my code. Thanks for that!

However, I have to admit that TrussPy was my first open-source coding project. There could be lots of anti-pattern and non-pythonic code inside. Due to its (over) complexity, I was careful in the past not to break anything because the performance is okay for these typically small systems, the results are fine and it is easy to use.

I'll leave this open - if I find some time, I'll give it a try.

@adtzlr adtzlr added the enhancement New feature or request label Nov 1, 2023
@Martin15135215
Copy link
Author

I've only recently picked up on this as well. I'm not sure whether there will be an increase in performance in this case. It is beneficial when thousands of instances of a class are created. I am unsure if this is the case here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants