[issue29736] Optimize builtin types constructor

STINNER Victor report at bugs.python.org
Mon Mar 6 09:56:14 EST 2017


New submission from STINNER Victor:

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%)  |
+-----------------------------------------------+--------+---------------------+

----------
files: bench.py
messages: 289111
nosy: haypo
priority: normal
severity: normal
status: open
title: Optimize builtin types constructor
type: performance
versions: Python 3.7
Added file: http://bugs.python.org/file46705/bench.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29736>
_______________________________________


More information about the Python-bugs-list mailing list