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

Small speedup for dataclass __eq__ and __repr__ #104904

Merged
merged 4 commits into from
May 30, 2023

Conversation

rhettinger
Copy link
Contributor

  1. Have__eq__ do a series of and comparisons rather than building a tuple and relying on tuple.__eq__.
  2. Let __repr__ put the type name in the f-string rather than calling string concatenation explicitly.

Old code:

 def __eq__(self,other):
  if other.__class__ is self.__class__:
   return (self.a,self.b,)==(other.a,other.b,)

 def __repr__(self):
  return self.__class__.__qualname__ + f"(a={self.a!r}, b={self.b!r})"

New code:

 def __eq__(self,other):
  if other.__class__ is self.__class__:
   return self.a==other.a and self.b==other.b

 def __repr__(self):
  return f"{self.__class__.__qualname__}(a={self.a!r}, b={self.b!r})"

Timings for a dataclass with two integer fields:

             Both Equal     First Equal    Neither Equal        Repr
             ==========     ===========    =============    ============
Baseline     0.09769874     0.10188975     0.10144600       0.18586733
With PR      0.08537712     0.08647508     0.070007080      0.18308112

Copy link
Member

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@ericvsmith
Copy link
Member

Those look great, @rhettinger! Thanks. I'll do a detailed code review then commit it.

@ericvsmith ericvsmith merged commit 18cfc1e into python:main May 30, 2023
carljm added a commit to carljm/cpython that referenced this pull request May 30, 2023
* main:
  CI: Temporarily skip paths with spaces to avoid error (python#105110)
  pythongh-105071: add missing versionadded directive (python#105097)
  pythongh-80064: Fix is_valid_wide_char() return type (python#105099)
  Small speedup for dataclass __eq__ and __repr__ (python#104904)
  pythongh-103921: Minor PEP-695 fixes to the `ast` module docs (python#105093)
  pythongh-105091: stable_abi.py: Remove "Unixy" check from --all on other platforms (pythonGH-105092)
@rhettinger rhettinger deleted the faster_dataclasses branch June 1, 2023 15:29
@hroncok
Copy link
Contributor

hroncok commented Mar 12, 2024

I belive I found a regression: #116647

@terryjreedy
Copy link
Member

Or you found that this resulted in a perhaps inadvertent bugfix ;-). Depends on Eric's intention for NANs in dataclasses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants