Skip to content

Combine dataclass exec calls #109870

Description

@ssweber

Feature or enhancement

Proposal:

As mentioned here, dataclass definition can possibly be sped up by combining the multiple exec calls into one.

I have two versions, and I found that this results in either a 1.1-1.22 1.26, or 1.37 speedup versus main (old time / new time)

version 1 combines the exec calls by passing a list of name/txt/locals from each function definition.
version 2 simplifies the final exec call to avoid __create_fn__, but I'm not experienced to know if that is safe.
(see below for open pr)

@ericvsmith Let me know if this is a suitable change, and if so, which version we should open a pr based on.

Thank you!

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

https://discuss.python.org/t/improving-dataclasses-startup-performance/15442/18

Linked PRs

Activity

  1. ssweber commented on Sep 25, 2023

    @ssweber
    Author

    I measured the difference with this benchmark which includes order=True.

    Since we are combining exec calls, the speed up over main will be less if order=False

  2. ssweber commented on Sep 25, 2023

    @ssweber
    Author

    This could be seen as complementary to Caching dataclass, but this proposal shouldn’t use more memory than the status quo. (we arnt storing a cache in the class)

  3. added
    performancePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directory
    on Sep 26, 2023
  4. ssweber commented on Sep 26, 2023

    @ssweber
    Author

    I updated my numbers. I had missed _hash_add, and my simplified exec without the __create_fn__ call was naive and didn't work 😅

    The new numbers 1.1-1.18 speedup, depending on how many methods are generated (eg, order=True, frozen=True, etc).

    I didn't take the time to refactor _hash_add to be included with the other functions, so its exec is still separate. If it was refactored to be included, we could once again bump those numbers up a bit.

  5. ericvsmith commented on Sep 30, 2023

    @ericvsmith
    Member

    @ssweber : As I said elsewhere, I think this is a good idea. Thanks for the work on it! I plan on looking at this during the core dev sprint next week.

  6. ssweber commented on Oct 1, 2023

    @ssweber
    Author

    Glad I can help in some way! I cleaned up and created a pull request. I tried to not change to much, mainly just to prove the modest speedup.

  7. ericvsmith commented on Oct 13, 2023

    @ericvsmith
    Member

    I have a PR that's almost ready for this. I got about a 15% speedup for creating the most basic of dataclasses, which adds 3 methods: __init__, __repr__, and __eq__. I expect the speedup to be faster if adding comparison functions, etc.

  8. self-assigned this
    on Oct 13, 2023
  9. ssweber commented on Oct 15, 2023

    @ssweber
    Author

    It's like 1.22 when you include order/frozen! 😄 love the new FuncBuilder class.

  10. added a commit that references this issue on Mar 25, 2024
  11. added a commit that references this issue on Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions