Skip to content

gh-109868: Skip deepcopy memo check for empty memo #109869

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
merged 2 commits into from
Sep 29, 2023

Conversation

eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Sep 25, 2023

In the copy.deepcopy method we can skip the initial memo check if the memo was just created.

Benchmark:

from copy import deepcopy
obj={'simple': 'dict'}
%timeit deepcopy(obj)
main: 1.91 µs ± 1.38 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
pr: 1.82 µs ± 8.18 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

@serhiy-storchaka
Copy link
Member

The difference is equal to one standard deviation. I am not sure that it is not random.

@eendebakpt
Copy link
Contributor Author

@serhiy-storchaka Here are some more detailed measurements:

import pyperf

setup = """
from copy import deepcopy
d1 = 10
d2 = [1, 2]
d3 = {'hello', 'there'}
d4 = ([1], [2])
"""

runner = pyperf.Runner()
for ii in [1, 2, 3, 4]:
	runner.timeit(name=f"d{ii}", stmt=f"deepcopy(d{ii})", setup=setup)

Results (with --rigorous option):

d1: Mean +- std dev: [main] 275 ns +- 5 ns -> [pr] 226 ns +- 6 ns: 1.22x faster
d2: Mean +- std dev: [main] 1.73 us +- 0.02 us -> [pr] 1.69 us +- 0.04 us: 1.02x faster
d3: Mean +- std dev: [main] 5.98 us +- 0.09 us -> [pr] 5.92 us +- 0.09 us: 1.01x faster
d4: Mean +- std dev: [main] 3.83 us +- 0.07 us -> [pr] 3.81 us +- 0.05 us: 1.01x faster

Geometric mean: 1.06x faster

The code change itself is very simple. It avoids a dict lookup, assignment to a local variable y and a comparison, which provides a small speedup. For a deepcopy of a larger object the performance improvement will indeed be below the noise.

The performance gain is small, but so is the change and I do not see any downsides to the change.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Well, the benefit is tiny, but the cost is virtually zero. I think nothing bad will happen if we accept it.

Thank you for your contribution @eendebakpt.

@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Debian root 3.x has failed when building commit 05079d9.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/all/#builders/345/builds/5956) and take a look at the build logs.
  4. Check if the failure is related to this commit (05079d9) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/all/#builders/345/builds/5956

Failed tests:

  • test_signal

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/test_signal.py", line 1287, in test_stress_delivery_dependent
    self.assertEqual(len(sigs), N, "Some signals were lost")
AssertionError: 5220 != 10000 : Some signals were lost

Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
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.

3 participants