classification
Title: Optimize builtin types constructor
Type: performance Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2017-03-06 14:56 by vstinner, last changed 2017-08-10 23:45 by vstinner. This issue is now closed.

Files
File name Uploaded Description Edit
bench.py vstinner, 2017-03-06 14:56
Pull Requests
URL Status Linked Edit
PR 519 closed vstinner, 2017-03-06 14:56
Messages (10)
msg289111 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-03-06 14:56
Attached PR replaces PyArg_ParseTupleAndKeywords() with
_PyArg_ParseTupleAndKeywordsFast() to optimize the constructor of the
builtin types:

* bool: bool_new()
* bytes: bytes_new()
* complex: complex_new()
* float: float_new()
* int: long_new()
* list: list_init()
* str: unicode_new()
* tuple: tuple_new()

When using keywords, the speedup is between 1.55x faster and 1.92x faster.

When using only positional arguments, the speedup is between 1.07x faster and 1.14x faster.

Results of attached bench.py:

+-----------------------------------------------+--------+---------------------+
| Benchmark                                     | ref    | changed             |
+===============================================+========+=====================+
| complex(real=0.0, imag=0.0)                   | 452 ns | 1.92x faster (-48%) |
+-----------------------------------------------+--------+---------------------+
| bytes("x", encoding="ascii", errors="strict") | 498 ns | 1.88x faster (-47%) |
+-----------------------------------------------+--------+---------------------+
| str(b"x", encoding="ascii")                   | 340 ns | 1.55x faster (-35%) |
+-----------------------------------------------+--------+---------------------+
| list([None])                                  | 208 ns | 1.14x faster (-12%) |
+-----------------------------------------------+--------+---------------------+
| int(0)                                        | 113 ns | 1.11x faster (-10%) |
+-----------------------------------------------+--------+---------------------+
| float(1.0)                                    | 110 ns | 1.10x faster (-9%)  |
+-----------------------------------------------+--------+---------------------+
| str("x")                                      | 115 ns | 1.10x faster (-9%)  |
+-----------------------------------------------+--------+---------------------+
| tuple((None,))                                | 111 ns | 1.10x faster (-9%)  |
+-----------------------------------------------+--------+---------------------+
| bytes(b"x")                                   | 126 ns | 1.10x faster (-9%)  |
+-----------------------------------------------+--------+---------------------+
| bool(True)                                    | 107 ns | 1.09x faster (-8%)  |
+-----------------------------------------------+--------+---------------------+
| complex(0.0, 0.0)                             | 176 ns | 1.07x faster (-7%)  |
+-----------------------------------------------+--------+---------------------+
msg289113 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-06 15:04
The half of constructors no longer use PyArg_ParseTupleAndKeywords().

I would wait until constructors be converted to Argument Clinic. There are ready patches for some of these builtin types, patches for other are in proggress.
msg289134 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-03-06 21:13
> I would wait until constructors be converted to Argument Clinic. There are ready patches for some of these builtin types, patches for other are in proggress.

Do you have links for these changes? It's painful to search for Argument Clinic changes, the issue title doesn't say anything except "derby <number>" :-/
msg289137 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-06 21:48
float, list: issue20185.
msg289156 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-07 09:46
Actually there is an issue with using Argument Clinic for constructors. I wrote a patch that fixes it and converts constructors of basic types to Argument Clinic a half year ago, but it was not finished and published due to focusing on 3.6. I'll update and publish it soon.
msg289840 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-19 06:53
Victor, could you please rebase your PR? And maybe rerun benchmarks?
msg289841 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-03-19 07:33
Ok, will do.
msg289844 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-19 09:39
And include bytearray.
msg289931 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-03-21 10:28
The following types were patched to use Argument Clinic (use now _PyArg_ParseTupleAndKeywordsFast or don't accept keyword arguments anymore):

* complex
* float (don't accept keywords anymore)
* list
* tuple

The following types still uses PyArg_ParseTupleAndKeywords:

* bytes
* bytearray
* int (PyLong)
* str (unicode_new)

bool doesn't accept keywords anymore and uses now PyArg_UnpackTuple().
msg300130 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-08-10 23:45
I lost interest in this issue. I closed my PR, now I close this old issue.
History
Date User Action Args
2017-08-10 23:45:29vstinnersetstatus: open -> closed
resolution: out of date
messages: + msg300130

stage: resolved
2017-03-21 10:28:48vstinnersetmessages: + msg289931
2017-03-19 09:39:16serhiy.storchakasetmessages: + msg289844
2017-03-19 07:33:25vstinnersetmessages: + msg289841
2017-03-19 06:53:20serhiy.storchakasetmessages: + msg289840
2017-03-07 09:46:35serhiy.storchakasetmessages: + msg289156
2017-03-06 21:48:56serhiy.storchakasetmessages: + msg289137
2017-03-06 21:13:09vstinnersetmessages: + msg289134
2017-03-06 15:04:28serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg289113
2017-03-06 14:56:44vstinnersetpull_requests: + pull_request429
2017-03-06 14:56:14vstinnercreate