classification
Title: improve performance of _Py_bytes_capitalize()
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, sir-sigurd, vstinner
Priority: normal Keywords: patch

Created on 2018-09-06 18:12 by sir-sigurd, last changed 2018-09-07 07:05 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9083 merged sir-sigurd, 2018-09-06 18:13
Messages (3)
msg324707 - (view) Author: Sergey Fedoseev (sir-sigurd) * Date: 2018-09-06 18:12
Currently _Py_bytes_capitalize() checks case before using conversion table, performance can be improved by using conversion table unconditionally. Benchmarks:

$ python -m perf timeit --compare-to ~/tmp/cpython-master-venv/bin/python -s "b = b'0'*10000000" "b.capitalize()"
/home/sergey/tmp/cpython-master-venv/bin/python: ..................... 7.26 ms +- 0.03 ms
/home/sergey/tmp/cpython-dev-venv/bin/python: ..................... 7.27 ms +- 0.04 ms
Mean +- std dev: [/home/sergey/tmp/cpython-master-venv/bin/python] 7.26 ms +- 0.03 ms -> [/home/sergey/tmp/cpython-dev-venv/bin/python] 7.27 ms +- 0.04 ms: 1.00x slower (+0%)
Not significant!

$ python -m perf timeit --compare-to ~/tmp/cpython-master-venv/bin/python -s "b = b'a'*10000000" "b.capitalize()"
/home/sergey/tmp/cpython-master-venv/bin/python: ..................... 7.25 ms +- 0.03 ms
/home/sergey/tmp/cpython-dev-venv/bin/python: ..................... 7.26 ms +- 0.03 ms
Mean +- std dev: [/home/sergey/tmp/cpython-master-venv/bin/python] 7.25 ms +- 0.03 ms -> [/home/sergey/tmp/cpython-dev-venv/bin/python] 7.26 ms +- 0.03 ms: 1.00x slower (+0%)

$ python -m perf timeit --compare-to ~/tmp/cpython-master-venv/bin/python -s "b = b'A'*10000000" "b.capitalize()"
/home/sergey/tmp/cpython-master-venv/bin/python: ..................... 18.1 ms +- 0.0 ms
/home/sergey/tmp/cpython-dev-venv/bin/python: ..................... 7.31 ms +- 0.13 ms
Mean +- std dev: [/home/sergey/tmp/cpython-master-venv/bin/python] 18.1 ms +- 0.0 ms -> [/home/sergey/tmp/cpython-dev-venv/bin/python] 7.31 ms +- 0.13 ms: 2.47x faster (-60%)
msg324717 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2018-09-07 04:54
New changeset 593bb30e82eded7f2ec02f7d1aa49742e6962113 by Benjamin Peterson (Sergey Fedoseev) in branch 'master':
closes bpo-34599: Improve performance of _Py_bytes_capitalize(). (GH-9083)
https://github.com/python/cpython/commit/593bb30e82eded7f2ec02f7d1aa49742e6962113
msg324721 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-07 07:05
Nice optimization Sergey Fedoseev, thanks!
History
Date User Action Args
2018-09-07 07:05:40vstinnersetnosy: + vstinner
messages: + msg324721
2018-09-07 04:54:54benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg324717

resolution: fixed
stage: patch review -> resolved
2018-09-06 18:13:12sir-sigurdsetkeywords: + patch
stage: patch review
pull_requests: + pull_request8541
2018-09-06 18:12:14sir-sigurdcreate