This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Optimize textio write buffering
Type: performance Stage: resolved
Components: IO Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: methane
Priority: normal Keywords: patch

Created on 2019-04-29 06:12 by methane, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bm_textio.py methane, 2019-04-29 06:12
Pull Requests
URL Status Linked Edit
PR 13002 merged methane, 2019-04-29 06:18
PR 13359 mangrisano, 2019-05-16 19:34
Messages (2)
msg341044 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-04-29 06:12
textio uses list of textio for internal buffering.  There are two inefficiency:

* When textio is line buffered and all written strings are line (it's very common), list object is allocated and freed.
* We convert texts into bytes, and call b''.join(list_of_bytes).  But when texts are ASCII and codecs are ASCII-compat, we can skip temporary bytes objects.

Attached patch is benchmark for buffered and line buffered write.

Faster (6):
- write_ascii_32k: 101 ns +- 1 ns -> 73.1 ns +- 0.4 ns: 1.39x faster (-28%)
- write_ascii_8k: 102 ns +- 1 ns -> 73.4 ns +- 0.4 ns: 1.38x faster (-28%)
- write_ascii_linebuffered: 815 ns +- 12 ns -> 731 ns +- 3 ns: 1.12x faster (-10%)
- write_unicode_linebuffered: 840 ns +- 11 ns -> 789 ns +- 15 ns: 1.06x faster (-6%)
- write_unicode_8k: 124 ns +- 1 ns -> 122 ns +- 1 ns: 1.01x faster (-1%)
- write_unicode_32k: 124 ns +- 1 ns -> 122 ns +- 1 ns: 1.01x faster (-1%)
msg342627 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-05-16 06:03
New changeset bfba8c373e362d48d4ee0e0cf55b8d9c169344ae by Inada Naoki in branch 'master':
bpo-36748: optimize TextIOWrapper.write() for ASCII string (GH-13002)
https://github.com/python/cpython/commit/bfba8c373e362d48d4ee0e0cf55b8d9c169344ae
History
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 80929
2019-05-16 19:34:02mangrisanosetpull_requests: + pull_request13274
2019-05-16 06:20:43methanesetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-16 06:03:27methanesetmessages: + msg342627
2019-04-29 06:18:42methanesetkeywords: + patch
stage: patch review
pull_requests: + pull_request12923
2019-04-29 06:12:38methanecreate