Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Small speedup for dataclass __eq__ and __repr__#104904

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ericvsmith merged 4 commits intopython:mainfromrhettinger:faster_dataclasses
May 30, 2023

Conversation

rhettinger
Copy link
Contributor

  1. Have__eq__ do a series ofand comparisons rather than building a tuple and relying ontuple.__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.18586733With PR      0.08537712     0.08647508     0.070007080      0.18308112

Copy link
Member

@carljmcarljm 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.

@ericvsmithericvsmith merged commit18cfc1e intopython:mainMay 30, 2023
carljm added a commit to carljm/cpython that referenced this pull requestMay 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)
@rhettingerrhettinger deleted the faster_dataclasses branchJune 1, 2023 15:29
@hroncok
Copy link
Contributor

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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@carljmcarljmcarljm approved these changes

@ericvsmithericvsmithAwaiting requested review from ericvsmithericvsmith is a code owner

Assignees

@ericvsmithericvsmith

Labels
performancePerformance or resource usageskip news
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

8 participants
@rhettinger@ericvsmith@hroncok@terryjreedy@carljm@erlend-aasland@bedevere-bot@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp