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

Warning in docs about frozen classes no longer applicable #816

Closed
A5rocks opened this issue May 16, 2021 · 2 comments · Fixed by #817
Closed

Warning in docs about frozen classes no longer applicable #816

A5rocks opened this issue May 16, 2021 · 2 comments · Fixed by #817

Comments

@A5rocks
Copy link

A5rocks commented May 16, 2021

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.
@Tinche
Copy link
Member

Tinche commented May 16, 2021

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

@hynek
Copy link
Member

hynek commented May 16, 2021

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

hynek added a commit that referenced this issue May 16, 2021
This is important because define/mutable have on_setattr=setters.validate on
default.

Fixes #816
hynek added a commit that referenced this issue May 16, 2021
This is important because define/mutable have on_setattr=setters.validate on
default.

Fixes #816
hynek added a commit that referenced this issue May 16, 2021
This is important because define/mutable have on_setattr=setters.validate on
default.

Fixes #816

Signed-off-by: Hynek Schlawack <hs@ox.cx>
hynek added a commit that referenced this issue May 16, 2021
This is important because define/mutable have on_setattr=setters.validate on
default.

Fixes #816

Signed-off-by: Hynek Schlawack <hs@ox.cx>
hynek added a commit that referenced this issue May 17, 2021
* Optimize the case of on_setattr=validate & no validators

This is important because define/mutable have on_setattr=setters.validate on
default.

Fixes #816

Signed-off-by: Hynek Schlawack <hs@ox.cx>

* Grammar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants