Skip to content

gh-162350: Use AC for array.array constructor - #162419

Merged
cmaloney merged 5 commits into
python:mainfrom
skirpichev:array.array-vc/162350
Nov 28, 2026
Merged

cmaloney merged 5 commits into
python:mainfrom
skirpichev:array.array-vc/162350

Conversation

@skirpichev

@skirpichev skirpichev commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

This fixes it's signature and adds also support for Vectorcall protocol.

This fixes it's signature and adds also support for Vectorcall protocol.
@skirpichev

skirpichev commented Nov 22, 2026 •

Copy link
Copy Markdown
Member Author

Microbenchmark (default options, gcc 14.2):

Benchmark ref patch
array of 0 elts 855 ns 664 ns: 1.29x faster
array of 1 elts 1.03 us 850 ns: 1.21x faster
array of 2 elts 1.14 us 970 ns: 1.18x faster
array of 3 elts 1.26 us 1.08 us: 1.17x faster
array of 100 elts 11.7 us 11.5 us: 1.02x faster
Geometric mean (ref) 1.17x faster
Details
from array import array
import pyperf
runner = pyperf.Runner()
tp = 'i'
for xs in [[], [1], [1, 2], [1, 2, 3], list(range(100))]:
    s = f"array of {len(xs)} elts"
    runner.bench_func(s, array, tp, xs)
array of 0 elts: Mean +- std dev: [ref] 5.08 us +- 0.13 us -> [patch] 3.86 us +- 0.02 us: 1.32x faster
array of 1 elts: Mean +- std dev: [ref] 6.23 us +- 0.04 us -> [patch] 5.02 us +- 0.23 us: 1.24x faster
array of 2 elts: Mean +- std dev: [ref] 6.51 us +- 0.40 us -> [patch] 5.29 us +- 0.03 us: 1.23x faster
array of 3 elts: Mean +- std dev: [ref] 6.79 us +- 0.27 us -> [patch] 5.57 us +- 0.04 us: 1.22x faster
array of 100 elts: Mean +- std dev: [ref] 30.8 us +- 0.2 us -> [patch] 30.9 us +- 0.1 us: 1.00x slower

Geometric mean: 1.20x faster

No significant difference for scimark benchmark.

@cmaloney cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly outside these lines make_array currently exists and uses array_new which means an unnecessary tuple construction on pickle related code paths. Be nice to have the two make_array callers now just directly call array_new_impl (both have the right variables as locals already) which means make_array can go away entirely.

cpython/Modules/arraymodule.c

Lines 2170 to 2209 in 02fe78f

/* Forward declaration. */
static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
/*
* Internal: This function wraps the array constructor--i.e., array_new()--to
* allow the creation of array objects from C code without having to deal
* directly the tuple argument of array_new(). The typecode argument is a
* string, like "i" or "f" for example, representing an array
* type code. The items argument is a bytes or a list object from which
* contains the initial value of the array.
*
* On success, this functions returns the array object created. Otherwise,
* NULL is returned to indicate a failure.
*/
static PyObject *
make_array(PyTypeObject *arraytype, const char *typecode, PyObject *items)
{
PyObject *new_args;
PyObject *array_obj;
PyObject *typecode_obj;
assert(arraytype != NULL);
assert(items != NULL);
typecode_obj = PyUnicode_FromString(typecode);
if (typecode_obj == NULL)
return NULL;
new_args = _PyTuple_FromPairSteal(typecode_obj, Py_NewRef(items));
if (new_args == NULL) {
return NULL;
}
array_obj = array_new(arraytype, new_args, NULL);
Py_DECREF(new_args);
if (array_obj == NULL)
return NULL;
return array_obj;
}

Comment thread Modules/arraymodule.c Resolved
Comment thread Modules/arraymodule.c Resolved
Comment thread Modules/arraymodule.c Resolved
@cmaloney

cmaloney commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

A couple small changes with this but I think they're all reasonable:

Call Before After
array() array() takes at least 1 argument (0 given) array expected at least 1 argument, got 0
array('b',1,2) array() takes at most 2 arguments (3 given) array expected at most 2 arguments, got 3
array(typecode='b') array.array() takes no keyword arguments array() takes no keyword arguments
inspect.signature(array) ValueError (typecode, initializer=(), /)
__doc__ first line array(typecode [, initializer]) -> array Create array object.
@skirpichev

Copy link
Copy Markdown
Member Author

I'll run scimark benchmark on this, but not expect too much, maybe few percents.

Using AC for constructor seems to be a good idea anyway, due to removal of the internal helper and fixed signature.

@cmaloney

Copy link
Copy Markdown
Contributor

👍, I tried making the base rules for "what makes a good refactor" that: cases where it's just adding @vectorcall and setting the slot have a higher performance requirement than cases where it's refactoring towards Argument Clinic. As long as we're doing the rewrite/refactor also adding vectorcall at the same time is a small extra cost with a reasonable micro-benchmark performance improvement.

@cmaloney
cmaloney merged commit 49a344e into python:main Nov 28, 2026
58 checks passed
@skirpichev
skirpichev deleted the array.array-vc/162350 branch November 28, 2026 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants