Skip to content

Commit 4a58ff7

Browse files
cmaloneyclaude
andcommitted
gh-162350: Use AC vectorcall for _json.make_encoder
json.dumps() builds a new encoder on every call, so this is per-call overhead for small payloads. @vectorcall requires an immutable type: setting attributes on _json.make_encoder now raises TypeError. Bad separators are now named ("argument 'key_separator'") instead of "argument 5". pyperformance, PGO+LTO: | Benchmark | main | branch | |------------|:-------:|:---------------------:| | json_dumps | 4.24 ms | 4.00 ms: 1.06x faster | pyperf microbenchmarks, PGO+LTO: | Benchmark | main | branch | |------------------------------------------|:-------:|:---------------------:| | json.dumps(1) | 456 ns | 409 ns: 1.12x faster | | json.dumps(small, separators=(',', ':')) | 1.22 us | 1.13 us: 1.07x faster | | json.dumps(small) | 850 ns | 794 ns: 1.07x faster | | json.dumps(small, sort_keys=True) | 1.31 us | 1.23 us: 1.07x faster | Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 91c4c6b commit 4a58ff7

6 files changed

Lines changed: 215 additions & 17 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
@@ -313,6 +313,7 @@ struct _Py_global_strings {
313313
STRUCT_FOR_ID(all_interpreters)
314314
STRUCT_FOR_ID(all_threads)
315315
STRUCT_FOR_ID(allow_code)
316+
STRUCT_FOR_ID(allow_nan)
316317
STRUCT_FOR_ID(alphabet)
317318
STRUCT_FOR_ID(any)
318319
STRUCT_FOR_ID(append)
@@ -460,6 +461,7 @@ struct _Py_global_strings {
460461
STRUCT_FOR_ID(element_factory)
461462
STRUCT_FOR_ID(emptyerror)
462463
STRUCT_FOR_ID(encode)
464+
STRUCT_FOR_ID(encoder)
463465
STRUCT_FOR_ID(encoding)
464466
STRUCT_FOR_ID(end)
465467
STRUCT_FOR_ID(end_col_offset)
@@ -563,6 +565,7 @@ struct _Py_global_strings {
563565
STRUCT_FOR_ID(in_fd)
564566
STRUCT_FOR_ID(include_aliases)
565567
STRUCT_FOR_ID(incoming)
568+
STRUCT_FOR_ID(indent)
566569
STRUCT_FOR_ID(index)
567570
STRUCT_FOR_ID(indexgroup)
568571
STRUCT_FOR_ID(inf)
@@ -596,6 +599,7 @@ struct _Py_global_strings {
596599
STRUCT_FOR_ID(isolation_level)
597600
STRUCT_FOR_ID(istext)
598601
STRUCT_FOR_ID(item)
602+
STRUCT_FOR_ID(item_separator)
599603
STRUCT_FOR_ID(items)
600604
STRUCT_FOR_ID(iter)
601605
STRUCT_FOR_ID(iterable)
@@ -605,6 +609,7 @@ struct _Py_global_strings {
605609
STRUCT_FOR_ID(jump)
606610
STRUCT_FOR_ID(keepends)
607611
STRUCT_FOR_ID(key)
612+
STRUCT_FOR_ID(key_separator)
608613
STRUCT_FOR_ID(keyfile)
609614
STRUCT_FOR_ID(keys)
610615
STRUCT_FOR_ID(kind)
@@ -638,6 +643,7 @@ struct _Py_global_strings {
638643
STRUCT_FOR_ID(loop)
639644
STRUCT_FOR_ID(manual_reset)
640645
STRUCT_FOR_ID(mapping)
646+
STRUCT_FOR_ID(markers)
641647
STRUCT_FOR_ID(mask)
642648
STRUCT_FOR_ID(match)
643649
STRUCT_FOR_ID(max_length)
@@ -827,9 +833,11 @@ struct _Py_global_strings {
827833
STRUCT_FOR_ID(sizehint)
828834
STRUCT_FOR_ID(skip_file_prefixes)
829835
STRUCT_FOR_ID(skip_non_matching_threads)
836+
STRUCT_FOR_ID(skipkeys)
830837
STRUCT_FOR_ID(sleep)
831838
STRUCT_FOR_ID(sock)
832839
STRUCT_FOR_ID(sort)
840+
STRUCT_FOR_ID(sort_keys)
833841
STRUCT_FOR_ID(source)
834842
STRUCT_FOR_ID(source_traceback)
835843
STRUCT_FOR_ID(spam)

‎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/_json.c‎

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525

2626
/*[clinic input]
2727
module _json
28+
class _json.make_encoder "PyEncoderObject *" ""
2829
[clinic start generated code]*/
29-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=549fa53592c925b2]*/
30+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f3373cb3d646c5f1]*/
3031

3132
typedef struct _PyScannerObject {
3233
PyObject_HEAD
@@ -98,8 +99,6 @@ scanner_dealloc(PyObject *self);
9899
static int
99100
scanner_clear(PyObject *self);
100101

101-
static PyObject *
102-
encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
103102
static void
104103
encoder_dealloc(PyObject *self);
105104
static int
@@ -1306,21 +1305,30 @@ static PyType_Spec PyScannerType_spec = {
13061305
.slots = PyScannerType_slots,
13071306
};
13081307

1308+
/*[clinic input]
1309+
@vectorcall
1310+
@classmethod
1311+
_json.make_encoder.__new__ as encoder_new
1312+
1313+
markers: object
1314+
default as defaultfn: object
1315+
encoder: object
1316+
indent: object
1317+
key_separator: unicode
1318+
item_separator: unicode
1319+
sort_keys: bool
1320+
skipkeys: bool
1321+
allow_nan: bool
1322+
[clinic start generated code]*/
1323+
13091324
static PyObject *
1310-
encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1325+
encoder_new_impl(PyTypeObject *type, PyObject *markers, PyObject *defaultfn,
1326+
PyObject *encoder, PyObject *indent,
1327+
PyObject *key_separator, PyObject *item_separator,
1328+
int sort_keys, int skipkeys, int allow_nan)
1329+
/*[clinic end generated code: output=46071d17c87596ef input=d3e48f5426deb209]*/
13111330
{
1312-
static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1313-
13141331
PyEncoderObject *s;
1315-
PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1316-
PyObject *item_separator;
1317-
int sort_keys, skipkeys, allow_nan;
1318-
1319-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOUUppp:make_encoder", kwlist,
1320-
&markers, &defaultfn, &encoder, &indent,
1321-
&key_separator, &item_separator,
1322-
&sort_keys, &skipkeys, &allow_nan))
1323-
return NULL;
13241332

13251333
if (markers != Py_None && !PyDict_Check(markers)) {
13261334
PyErr_Format(PyExc_TypeError,
@@ -2042,14 +2050,15 @@ static PyType_Slot PyEncoderType_slots[] = {
20422050
{Py_tp_clear, encoder_clear},
20432051
{Py_tp_members, encoder_members},
20442052
{Py_tp_new, encoder_new},
2053+
{Py_tp_vectorcall, encoder_vectorcall},
20452054
{0, 0}
20462055
};
20472056

20482057
static PyType_Spec PyEncoderType_spec = {
20492058
.name = "_json.Encoder",
20502059
.basicsize = sizeof(PyEncoderObject),
20512060
.itemsize = 0,
2052-
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
2061+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE,
20532062
.slots = PyEncoderType_slots
20542063
};
20552064

0 commit comments

Comments
 (0)