-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-114264: Optimize performance of copy.deepcopy by adding a fast path for atomic types #114266
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
Merged
serhiy-storchaka
merged 23 commits into
python:main
from
eendebakpt:deepcopy_atomic_types
Jun 7, 2024
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9990245
Optimize performance of copy.deepcopy by adding a fast path for atomi…
eendebakpt 06f1980
match order with main
eendebakpt dd9fabf
📜🤖 Added by blurb_it.
blurb-it[bot] becbb59
format news entry
eendebakpt 339c2b9
put d variable back
eendebakpt 89ecad1
Merge branch 'deepcopy_atomic_types' of github.com:eendebakpt/cpython…
eendebakpt 9aacf87
Merge branch 'main' into deepcopy_atomic_types
eendebakpt 5197f94
Merge branch 'main' into deepcopy_atomic_types
eendebakpt 18914f3
Merge branch 'main' into deepcopy_atomic_types
eendebakpt 8f294c7
Merge branch 'main' into deepcopy_atomic_types
eendebakpt 3b57fc1
Merge branch 'main' into deepcopy_atomic_types
eendebakpt 53d63a7
Merge branch 'main' into deepcopy_atomic_types
eendebakpt eb8e6ba
improve performance of _deepcopy_list with a list comprehension
eendebakpt e468b6c
avoid generation of exception in _deepcopy_tuple
eendebakpt 2b4abe6
Merge branch 'main' into deepcopy_atomic_types
eendebakpt e3310f5
revert comprehension changes
eendebakpt 1186a5b
Merge branch 'deepcopy_atomic_types' of github.com:eendebakpt/cpython…
eendebakpt ce7273b
Merge branch 'main' into deepcopy_atomic_types
eendebakpt 4bc74d3
Update Lib/copy.py
eendebakpt d69d8da
Merge branch 'main' into deepcopy_atomic_types
eendebakpt c66b24d
Apply suggestions from code review
eendebakpt 142b0fc
Merge branch 'main' into deepcopy_atomic_types
eendebakpt d6fa670
Merge branch 'main' into deepcopy_atomic_types
eendebakpt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2024-01-18-21-44-23.gh-issue-114264.DBKn29.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve performance of :func:`copy.deepcopy` by adding a fast path for atomic types. |
Oops, something went wrong.
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.
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.
Adding a fast path here would cause a potential behavioral change if
memo
is involved. This should be mentioned in NEWS.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.
The safer way might be simply keep the original behavior - put the fast path after memo. This is a user observable change and it might matter. For example, if the user is using the memo dict to keep track of all the objects copied.
It would also be interested to see the benchmark if we keep the memo as it is - how much performance gain is from not updating memo?
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.
Putting the fast path after the memo results in the following (main vs. the variation):
Still worthwhile, but a smaller improvement.
I am trying to find a case where the behavior changes (so I can also add a test). But the atomic types are not added to the
memo
:The only case I could think of where behaviour changes is when users supply their own
memo
, and then the behavior change (different id for the same string) could be considered an implementation details:Are there any cases where behavior changes that I am missing?
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.
I wonder how the times will change if use
if copier is _deepcopy_atomic
instead ofif cls in _atomic_types
? You can also try to use different special value for example...
instead of_deepcopy_atomic
to avoid lookup in globals.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.
@serhiy-storchaka That is an interesting suggestion. A microbenchmark shows getting the copier is not much more expensive than the
cls in _atomic_types
check:Using a special value for the copier can be done after the memo check. This is quite close to current main and has performance
(implementation is: main...eendebakpt:deepcopy_atomic_types_v3)
Using the same approach before the memo check has performance:
(implementation: main...eendebakpt:deepcopy_atomic_types_v4)
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.
Ah you are right, I missed that. If that, then the impact is much smaller than I thought and I think it's okay to skip the memo part.
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.
I am okay with both solutions, provided the
memo
change is mentioned (if there is one). Users can use this function in surprising ways, and it's a de facto public API.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.
Thank you for satisfying my curiosity. I see that there is a non-trivial benefit from using
_atomic_types
.Could you please make benchmarks for a collection, containing a large number of non-atomic identical objects, e.g.
[[1]]*100
, for different variants of the code? I expect that some variants can have regression, but if it is not great, we can accept this.I also wonder whether the same approach (with an
_immutable_types
set) should be used incopy.copy()
. Even if it does not usememo
, there may be some benefit, and it could be better for uniformity of the code.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.
A more extensive benchmark script:
Comparison of the three alternative versions to main. This PR:
main...eendebakpt:deepcopy_atomic_types_v3
main...eendebakpt:deepcopy_atomic_types_v4
Some notes:
In this PR we avoid the cost of looking into the memo for atomic types which provides the main speedup. This comes at a performance degradation for objects containing many identical (same
id
) values. Based on the numbers above there is no peformance reason to pick version v4. For clarify or uniformity of the code it would not hurt a lot though to replace the pr with v4. I think one could construct benchmarks where v3 is faster than this pr (thinking about non-atomic types which have a custom__copy__
and recursive calls to deepcopy such as numpy arrays), but I expect differences to be small. Version v3 has the same performance on the "deepcopy repeating" as main (the 1.01x slower is a random fluctuation I believe, on average it will be 1.00x) and a small performance improvement for some other cases.I also considered aligning the implementations of
copy.copy
andcopy.deepcopy
(for uniformity of the code), but decided against this initially. My line of thought:memo
that can be skipped (which was the main performance benefit)copy.copy
on atomic types should be relative rare (and cheap anyway), so there is less to gain.list
,dict
orset
) there are no recursive calls to the components. This is in contrast withdeepcopy
where are call on a dataclass can recurse into atomic-types.I have not updated the news entry yet with a description of the behaviour changes, because I think the behavior changes are not really something one can notice with normal use of
deepcopy
.i) The first behavior change is that the number of invocations of
memo.get
is less. Butmemo.get
is a read-only operation (on normal dicts), so this is not something visible from the public interface.ii) In the example given above the memo passed is
memo= {id(s2): s}
. This memo is not really avalid
memo, since we require for each key-value pair in the memoid(value)==key
, which is not true in the example.If desired I can add this to the news entry though.
The results above are only micro benchmarks and it will depend on the application which version will perform best. Applications where I have found the deepcopy to take a significant amount of time are lmfit, qiskit and quantify-scheduler, but I cannot run those with current main (I could with 3.12 though).
There is another PR improving the speed of deepcopy (gh-72793: C implementation of parts of copy.deepcopy #91610). That approach converts the python implementation to C but is more complex and will take time to review (and might not be accepted).