danielgtaylor / danielgtaylor/python-betterproto

Optimize dataclasses with __slots__

Open
#50 8 comments 0 reactions 0 assignees View on GitHub
enhancement low priority medium
Dominant language
Python
Stars
1.8k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

[Adding \_\_slots\_\_ to a class](https://stackoverflow.com/questions/472000/usage-of-slots) improves memory usage and performance of attribute access, at the expense of not being able to set arbitrary attributes on the resulting objects.

[This project](https://github.com/starhel/dataslots/blob/master/dataslots/__init__.py) demonstrates how dataclasses can be augmented to use __slots__, whilst still having class attributes set (normally adding the __slots__ attribute to a class definition would cause an error with dataclassess that declare default values).

Making generated dataclasses use slots in this way would be a small breaking change, though I don't see how it could be too inconvenient to work with, and I expect the performance improvement would be worthwhile.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.