Skip to content

Commit ee1f8ee

Browse files
authored
gh-162350: Add AC vectorcall for _csv.Dialect (#162499)
Replace the hand-written `dialect_new` with an Argument Clinic + vectorcall. Move `_csv_Dialect___replace___impl()` from calling `dialect_new()` to `dialect_new_helper()` to avoid temporary tuple. Error message for too many arguments is now `TypeError: Dialect() takes at most 9 arguments (N given)` instead of `TypeError: function takes at most 9 arguments (N given)`. Benchmark: ``` +-------------------------------------------------+---------+-----------------------+ | Benchmark | main | patch | +=================================================+=========+=======================+ | _csv.Dialect() | 240 ns | 198 ns: 1.22x faster | +-------------------------------------------------+---------+-----------------------+ | _csv.Dialect().__replace__(**{'delimiter':','}) | 1.79 us | 1.55 us: 1.15x faster | +-------------------------------------------------+---------+-----------------------+ | csv.reader(f) | 409 ns | 381 ns: 1.07x faster | +-------------------------------------------------+---------+-----------------------+ | csv.reader(f, delimiter=';') | 803 ns | 557 ns: 1.44x faster | +-------------------------------------------------+---------+-----------------------+ | csv.writer(g) | 399 ns | 371 ns: 1.08x faster | +-------------------------------------------------+---------+-----------------------+ | parse_small() | 5.45 us | 5.07 us: 1.07x faster | +-------------------------------------------------+---------+-----------------------+ | Geometric mean | (ref) | 1.14x faster | +-------------------------------------------------+---------+-----------------------+ Benchmark hidden because not significant (1): parse_large() ``` ```python import pyperf SETUP = """ import _csv, csv, io f = io.StringIO() g = io.StringIO() small_data = "a,b,c\\n" + "1,2,3\\n" * 20 # ~140 bytes large_data = "a,b,c\\n" + "1,2,3\\n" * 50000 # ~350 KB def parse_small(): for row in csv.reader(io.StringIO(small_data), delimiter=','): pass def parse_large(): for row in csv.reader(io.StringIO(large_data), delimiter=','): pass """ CASES = [ ("_csv.Dialect()", "_csv.Dialect()"), ("_csv.Dialect().__replace__(**{'delimiter':','})", "_csv.Dialect().__replace__(**{'delimiter':','})"), ("csv.reader(f)", "csv.reader(f)"), ("csv.reader(f, delimiter=';')", "csv.reader(f, delimiter=';')"), ("csv.writer(g)", "csv.writer(g)"), ("parse_small()", "parse_small()"), ("parse_large()", "parse_large()"), ] runner = pyperf.Runner() for name, stmt in CASES: runner.timeit(name, stmt=stmt, setup=SETUP) ```
1 parent a92a4d1 commit ee1f8ee

6 files changed

Lines changed: 273 additions & 62 deletions

File tree

‎Include/internal/pycore_global_objects_fini_generated.h‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_global_strings.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,14 @@ struct _Py_global_strings {
437437
STRUCT_FOR_ID(default)
438438
STRUCT_FOR_ID(defaultaction)
439439
STRUCT_FOR_ID(delete)
440+
STRUCT_FOR_ID(delimiter)
440441
STRUCT_FOR_ID(denominator)
441442
STRUCT_FOR_ID(depth)
442443
STRUCT_FOR_ID(desired_access)
443444
STRUCT_FOR_ID(detect_types)
444445
STRUCT_FOR_ID(deterministic)
445446
STRUCT_FOR_ID(device)
447+
STRUCT_FOR_ID(dialect)
446448
STRUCT_FOR_ID(dict)
447449
STRUCT_FOR_ID(difference_update)
448450
STRUCT_FOR_ID(digest)
@@ -456,6 +458,7 @@ struct _Py_global_strings {
456458
STRUCT_FOR_ID(doc)
457459
STRUCT_FOR_ID(done)
458460
STRUCT_FOR_ID(dont_inherit)
461+
STRUCT_FOR_ID(doublequote)
459462
STRUCT_FOR_ID(dst)
460463
STRUCT_FOR_ID(dst_dir_fd)
461464
STRUCT_FOR_ID(duration_sec)
@@ -475,6 +478,7 @@ struct _Py_global_strings {
475478
STRUCT_FOR_ID(env)
476479
STRUCT_FOR_ID(error_rate)
477480
STRUCT_FOR_ID(errors)
481+
STRUCT_FOR_ID(escapechar)
478482
STRUCT_FOR_ID(event)
479483
STRUCT_FOR_ID(eventmask)
480484
STRUCT_FOR_ID(exc)
@@ -638,6 +642,7 @@ struct _Py_global_strings {
638642
STRUCT_FOR_ID(line)
639643
STRUCT_FOR_ID(line_buffering)
640644
STRUCT_FOR_ID(lineno)
645+
STRUCT_FOR_ID(lineterminator)
641646
STRUCT_FOR_ID(little)
642647
STRUCT_FOR_ID(lo)
643648
STRUCT_FOR_ID(locale)
@@ -771,7 +776,9 @@ struct _Py_global_strings {
771776
STRUCT_FOR_ID(qualname)
772777
STRUCT_FOR_ID(query)
773778
STRUCT_FOR_ID(queuetype)
779+
STRUCT_FOR_ID(quotechar)
774780
STRUCT_FOR_ID(quotetabs)
781+
STRUCT_FOR_ID(quoting)
775782
STRUCT_FOR_ID(random_func)
776783
STRUCT_FOR_ID(raw)
777784
STRUCT_FOR_ID(read)
@@ -837,6 +844,7 @@ struct _Py_global_strings {
837844
STRUCT_FOR_ID(sizehint)
838845
STRUCT_FOR_ID(skip_file_prefixes)
839846
STRUCT_FOR_ID(skip_non_matching_threads)
847+
STRUCT_FOR_ID(skipinitialspace)
840848
STRUCT_FOR_ID(skipkeys)
841849
STRUCT_FOR_ID(sleep)
842850
STRUCT_FOR_ID(sock)

‎Include/internal/pycore_runtime_init_generated.h‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_unicodeobject_generated.h‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Modules/_csv.c‎

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -457,19 +457,6 @@ Dialect_dealloc(PyObject *self)
457457
Py_DECREF(tp);
458458
}
459459

460-
static char *dialect_kws[] = {
461-
"dialect",
462-
"delimiter",
463-
"doublequote",
464-
"escapechar",
465-
"lineterminator",
466-
"quotechar",
467-
"quoting",
468-
"skipinitialspace",
469-
"strict",
470-
NULL
471-
};
472-
473460
static _csvstate *
474461
_csv_state_from_type(PyTypeObject *type, const char *name)
475462
{
@@ -486,44 +473,54 @@ _csv_state_from_type(PyTypeObject *type, const char *name)
486473
return module_state;
487474
}
488475

476+
/*[clinic input]
477+
@vectorcall
478+
@classmethod
479+
_csv.Dialect.__new__ as dialect_new
480+
dialect: object = NULL
481+
delimiter: object = NULL
482+
doublequote: object = NULL
483+
escapechar: object = NULL
484+
lineterminator: object = NULL
485+
quotechar: object = NULL
486+
quoting: object = NULL
487+
skipinitialspace: object = NULL
488+
strict: object = NULL
489+
[clinic start generated code]*/
490+
489491
static PyObject *
490-
dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
492+
dialect_new_impl(PyTypeObject *type, PyObject *dialect, PyObject *delimiter,
493+
PyObject *doublequote, PyObject *escapechar,
494+
PyObject *lineterminator, PyObject *quotechar,
495+
PyObject *quoting, PyObject *skipinitialspace,
496+
PyObject *strict)
497+
/*[clinic end generated code: output=123393bdc71ddaa6 input=469f47d5890022f7]*/
491498
{
492-
DialectObj *self;
499+
DialectObj *self = NULL;
493500
PyObject *ret = NULL;
494-
PyObject *dialect = NULL;
495-
PyObject *delimiter = NULL;
496-
PyObject *doublequote = NULL;
497-
PyObject *escapechar = NULL;
498-
PyObject *lineterminator = NULL;
499-
PyObject *quotechar = NULL;
500-
PyObject *quoting = NULL;
501-
PyObject *skipinitialspace = NULL;
502-
PyObject *strict = NULL;
503-
504-
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
505-
"|OOOOOOOOO", dialect_kws,
506-
&dialect,
507-
&delimiter,
508-
&doublequote,
509-
&escapechar,
510-
&lineterminator,
511-
&quotechar,
512-
&quoting,
513-
&skipinitialspace,
514-
&strict))
515-
return NULL;
501+
502+
/* vectorcall passes borrowed references; hold strong references before
503+
get_dialect_from_registry() can run arbitrary Python. */
504+
Py_XINCREF(delimiter);
505+
Py_XINCREF(doublequote);
506+
Py_XINCREF(escapechar);
507+
Py_XINCREF(lineterminator);
508+
Py_XINCREF(quotechar);
509+
Py_XINCREF(quoting);
510+
Py_XINCREF(skipinitialspace);
511+
Py_XINCREF(strict);
516512

517513
_csvstate *module_state = _csv_state_from_type(type, "dialect_new");
518514
if (module_state == NULL) {
519-
return NULL;
515+
goto err;
520516
}
521517

522518
if (dialect != NULL) {
523519
if (PyUnicode_Check(dialect)) {
524520
dialect = get_dialect_from_registry(dialect, module_state);
525-
if (dialect == NULL)
526-
return NULL;
521+
if (dialect == NULL) {
522+
goto err;
523+
}
527524
}
528525
else
529526
Py_INCREF(dialect);
@@ -536,25 +533,17 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
536533
quotechar == NULL &&
537534
quoting == NULL &&
538535
skipinitialspace == NULL &&
539-
strict == NULL)
540-
return dialect;
536+
strict == NULL) {
537+
ret = Py_NewRef(dialect);
538+
goto err;
539+
}
541540
}
542541

543542
self = (DialectObj *)type->tp_alloc(type, 0);
544543
if (self == NULL) {
545-
Py_CLEAR(dialect);
546-
return NULL;
544+
goto err;
547545
}
548546
self->lineterminator = NULL;
549-
550-
Py_XINCREF(delimiter);
551-
Py_XINCREF(doublequote);
552-
Py_XINCREF(escapechar);
553-
Py_XINCREF(lineterminator);
554-
Py_XINCREF(quotechar);
555-
Py_XINCREF(quoting);
556-
Py_XINCREF(skipinitialspace);
557-
Py_XINCREF(strict);
558547
if (dialect != NULL) {
559548
#define DIALECT_GETATTR(v, n) \
560549
do { \
@@ -676,13 +665,10 @@ static PyObject *
676665
_csv_Dialect___replace___impl(DialectObj *self, PyObject *changes)
677666
/*[clinic end generated code: output=8f692c2c63a61b50 input=154beb565fbabd4c]*/
678667
{
679-
PyObject *newargs = PyTuple_Pack(1, self);
680-
if (newargs == NULL) {
681-
return NULL;
682-
}
683-
PyObject *result = dialect_new(Py_TYPE(self), newargs, changes);
684-
Py_DECREF(newargs);
685-
return result;
668+
PyObject *const args[1] = {(PyObject *)self};
669+
return dialect_new_helper(Py_TYPE(self), args, 1,
670+
changes ? PyDict_GET_SIZE(changes) : 0,
671+
changes, NULL);
686672
}
687673

688674
static struct PyMethodDef dialect_methods[] = {
@@ -719,6 +705,7 @@ static PyType_Slot Dialect_Type_slots[] = {
719705
{Py_tp_members, Dialect_memberlist},
720706
{Py_tp_getset, Dialect_getsetlist},
721707
{Py_tp_new, dialect_new},
708+
{Py_tp_vectorcall, dialect_vectorcall},
722709
{Py_tp_methods, dialect_methods},
723710
{Py_tp_dealloc, Dialect_dealloc},
724711
{Py_tp_clear, Dialect_clear},

0 commit comments

Comments
 (0)