Skip to content

Commit

Permalink
ClassBuilder: eval only once (#1407)
Browse files Browse the repository at this point in the history
* ClassBuilder: eval only once

* avoid Attribute.evolve

* speed up `_make_attr_tuple_class`

* speed up `_add_method_dunders`

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* optimize _CountingAttrs more

* Fix up `_add_method_dunders`

* avoid a hot splitlines

* restore old type_hash

* fix scriptlines handling

* restore contextlib.suppress

* Revert "fix scriptlines handling"

This reverts commit 4e0af4b.

* Revert "avoid a hot splitlines"

This reverts commit 532faed.

* add some hints

* fix tests

* Refactor `_Attributes`

* Reflow

* Reflow docs

* Microoptimization

* Rework type hints a little

* Fix arg name

* Add changelog entry

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Tinche and pre-commit-ci[bot] authored Feb 15, 2025
1 parent e048efc commit 74007f6
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 204 deletions.
1 change: 1 addition & 0 deletions changelog.d/1407.change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speed up class creation by 30%-50% by compiling methods only once and using a variety of other techniques.
4 changes: 2 additions & 2 deletions src/attr/_cmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import functools
import types

from ._make import _make_ne
from ._make import __ne__


_operation_names = {"eq": "==", "lt": "<", "le": "<=", "gt": ">", "ge": ">="}
Expand Down Expand Up @@ -71,7 +71,7 @@ def cmp_using(
if eq is not None:
has_eq_function = True
body["__eq__"] = _make_operator("eq", eq)
body["__ne__"] = _make_ne()
body["__ne__"] = __ne__

if lt is not None:
num_order_functions += 1
Expand Down
Loading

0 comments on commit 74007f6

Please sign in to comment.