FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Small speedup for dataclass __eq__ and __repr__ by rhettinger · Pull Request #104904 · python/cpython · GitHub

/ cpython Public

Small speedup for dataclass __eq__ and __repr__ - #104904

Merged
ericvsmith merged 4 commits into
python:mainfrom
rhettinger:faster_dataclasses
May 30, 2023
Merged

Small speedup for dataclass __eq__ and __repr__#104904
ericvsmith merged 4 commits into
python:mainfrom
rhettinger:faster_dataclasses

Conversation

Copy link
Copy Markdown
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

carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good!

Copy link
Copy Markdown
Member

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

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 deleted the faster_dataclasses branch June 1, 2023 15:29

hroncok commented Mar 12, 2024

Copy link
Copy Markdown
Contributor

I belive I found a regression: #116647

Copy link
Copy Markdown
Member

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

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL