Skip to content

Warning in docs about frozen classes no longer applicable #816

Closed

Description

@A5rocks

If you check the docs on frozen classes (https://www.attrs.org/en/stable/how-does-it-work.html#immutability), you'll note it says "You should avoid instantiating lots of frozen slotted classes [...] in performance-critical code."

However... with attr.define/attr.frozen both take the same amount of time.

In [1]: import attr

In [2]: @attr.frozen
   ...: class SlottedFrozen:
   ...:     foo: int
   ...:     bar: int
   ...:     baz: str
   ...:     bing: float = 5.0
   ...: 

In [3]: @attr.define
   ...: class SlottedUnfrozen:
   ...:     foo: int
   ...:     bar: int
   ...:     baz: str
   ...:     bing: float = 5.0
   ...: 

In [4]: @attr.s(slots=True, frozen=True, auto_attribs=True)
   ...: class OldSlottedFrozen:
   ...:     foo: int
   ...:     bar: int
   ...:     baz: str
   ...:     bing: float = 5.0
   ...: 

In [5]: @attr.s(slots=True, auto_attribs=True)
   ...: class OldSlottedUnfrozen:
   ...:     foo: int
   ...:     bar: int
   ...:     baz: str
   ...:     bing: float = 5.0
   ...: 

In [6]: %timeit SlottedFrozen(7, 42, "hi!")
553 ns ± 10.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [7]: %timeit SlottedUnfrozen(7, 42, "hi!")
566 ns ± 17.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [8]: %timeit OldSlottedFrozen(7, 42, "hi!")
553 ns ± 8.64 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [9]: %timeit OldSlottedUnfrozen(7, 42, "hi!")
296 ns ± 3.09 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

What's up? Well, turns out the new API is setting on_setattr=attr.setters.validate. This turns the __init__ from this: (old)

In [14]: print(inspect.getsource(OldSlottedUnfrozen.__init__))
def __init__(self, foo, bar, baz, bing=attr_dict['bing'].default):
    self.foo = foo
    self.bar = bar
    self.baz = baz
    self.bing = bing

To this: (new)

In [15]: print(inspect.getsource(SlottedUnfrozen.__init__))
def __init__(self, foo, bar, baz, bing=attr_dict['bing'].default):
    _setattr = _cached_setattr.__get__(self, self.__class__)
    _setattr('foo', foo)
    _setattr('bar', bar)
    _setattr('baz', baz)
    _setattr('bing', bing)

Possible solutions:

  • Mark that the note no longer applies
  • Make a fast path -- only set on_setattr=attr.setters.validate in new API if there is a validator.

Activity

  1. Tinche commented on May 16, 2021

    @Tinche
    Member

    Ooph, I was not aware of this. A fast path would be great here.

  2. hynek commented on May 16, 2021

    @hynek
    Member

    Yeah fast path is definitely the way here. I'm surprised there isn't one – I suspect I just forgot to implement it.

  3. added 4 commits that reference this issue on May 16, 2021
    a6075ba
    097a99f
    774b248
    5c32023
  4. added a commit that references this issue on May 17, 2021
    20c2d4f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions