botbowl icon indicating copy to clipboard operation
botbowl copied to clipboard

Implement __slots__ in model.py

Open mrbermell opened this issue 2 years ago • 1 comments

The special attribute slots allows you to explicitly state which instance attributes you expect your object instances to have, with the expected results:

  • faster attribute access.
  • space savings in memory.

Full stack overflow post here

By doing below (on every class that's often used), we can get a significant performance increase!

@immutable_after_init
class Square:
    __slots__ = 'x', 'y', '_out_of_bounds'   # only new thing
    x: int
    y: int
    _out_of_bounds: Optional[bool]

    def __init__(self, x: int, y: int, _out_of_bounds=None):
        self.x = x
        self.y = y
        self._out_of_bounds = _out_of_bounds
    ...

This is a good first issue for anyone who want to contribute, just make the update and make sure all tests pass! :relaxed:

mrbermell avatar May 17 '22 18:05 mrbermell

I would like to work on this issue.

IanWorley avatar May 17 '22 22:05 IanWorley