GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Small speedup for dataclass __eq__ and __repr__ by rhettinger · Pull Request #104904 · python/cpython

Conversation

@rhettinger

@rhettinger rhettinger commented May 24, 2023

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
rhettinger added 3 commits May 14, 2023 17:36
@rhettinger
Faster __repr__ with str.__add__ moved inside the f-string.
88a816d
@rhettinger
Almost there
bce2e72
@rhettinger
Faster __eq__
cfa5a06
@rhettinger rhettinger added performance Performance or resource usage skip issue skip news labels May 24, 2023
@rhettinger rhettinger assigned ericvsmith May 24, 2023
@rhettinger
rhettinger requested a review from ericvsmith as a code owner May 24, 2023 22:59
@bedevere-bot bedevere-bot added the awaiting core review label May 24, 2023
carljm
carljm approved these changes May 24, 2023
View reviewed changes

@carljm carljm left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks good!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels May 24, 2023
@hauntsaninja
Merge branch 'main' into faster_dataclasses
30439ce
@ericvsmith

ericvsmith commented May 25, 2023

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericvsmith
ericvsmith merged commit 18cfc1e into python:main May 30, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label May 30, 2023
carljm added a commit to carljm/cpython that referenced this pull request May 30, 2023
@carljm
Merge branch 'main' into superbadgetattro
24e1159
* 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

hroncok commented Mar 12, 2024

Copy link
Copy Markdown
Contributor

I belive I found a regression: #116647

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland erlend-aasland removed the skip issue label Mar 12, 2024
This was referenced Jun 17, 2024
Python 3.13.0b2: 4 tests failuires python-attrs/cattrs#547
Closed
dataclass(a=nan) == dataclass(a=nan) change in truthnes between 3.12 and 3.13 #120645
Closed
@terryjreedy

terryjreedy commented Jun 17, 2024

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gesslerpd gesslerpd mentioned this pull request Aug 7, 2025
gh-137658: Fix dataclass order method behaviors to align with the equality semantics #137497
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@carljm carljm carljm approved these changes
@ericvsmith ericvsmith Awaiting requested review from ericvsmith ericvsmith is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@ericvsmith ericvsmith

Labels

performance Performance or resource usage skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@rhettinger @ericvsmith @hroncok @terryjreedy @carljm @erlend-aasland @bedevere-bot @hauntsaninja
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.