We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
copy.deepcopy
slice
Right now slice objects are deepcopied using __reduce__ defined as:
__reduce__
cpython/Objects/sliceobject.c
Lines 559 to 563 in 26ff436
It is one of last branches of deepcopy logic:
deepcopy
cpython/Lib/copy.py
Lines 120 to 157 in 26ff436
But, since slice is an immutable type without nested structures, we can optimize its deepcopy as:
d[slice] = _deepcopy_atomic
Before:
» pyperf timeit --setup 'from copy import deepcopy; s = slice(1,10,2)' 'deepcopy(s)' ..................... Mean +- std dev: 3.46 us +- 0.18 us
After:
» pyperf timeit --setup 'from copy import deepcopy; s = slice(1,10,2)' 'deepcopy(s)' ..................... Mean +- std dev: 277 ns +- 3 ns
Looks like a good speedup for just a single line! Noticed while working on #100815
PR is incoming.
The text was updated successfully, but these errors were encountered:
pythongh-100817: Speed up copy.deepcopy calls on slice objects
6fd389c
Closing, see #100818
Sorry, something went wrong.
copy
@sobolevn PR #100817 makes the deepcopy faster, which also has a positive effect on the speed of the slice deep copy:
Main:
$ ./python -m pyperf timeit --setup 'from copy import deepcopy; s = slice(1,10,2)' 'deepcopy(s)' ..................... Mean +- std dev: 2.96 us +- 0.05 us
PR 100817
$ ./python -m pyperf timeit --setup 'from copy import deepcopy; s = slice(1,10,2)' 'deepcopy(s)' ..................... Mean +- std dev: 2.47 us +- 0.07 us
pickle
test_slice
sobolevn
No branches or pull requests
slice
Right now
slice
objects are deepcopied using__reduce__
defined as:cpython/Objects/sliceobject.c
Lines 559 to 563 in 26ff436
It is one of last branches of
deepcopy
logic:cpython/Lib/copy.py
Lines 120 to 157 in 26ff436
But, since
slice
is an immutable type without nested structures, we can optimize itsdeepcopy
as:Before:
After:
Looks like a good speedup for just a single line!
Noticed while working on #100815
PR is incoming.
Linked PRs
copy.deepcopy
calls onslice
objects #100818The text was updated successfully, but these errors were encountered: