Skip to content

Commit 2edcecb

Browse files
committed
pythongh-159702: Add fast-path for float in _json encoder
Optimize json encoder for floats: up to 1.3x faster. Avoid creating a temporary Unicode object when writing the JSON representation of a float: decode directly the ASCII-encoded string (PyOS_double_to_string()) into the writer.
1 parent 34fe59b commit 2edcecb

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`json`: Optimize the JSON encoder for floats to make it up to 1.3x
2+
faster. Patch by Victor Stinner.

‎Modules/_json.c‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,52 @@ encoder_encode_float(PyEncoderObject *s, PyObject *obj)
15321532
return PyFloat_Type.tp_repr(obj);
15331533
}
15341534

1535+
static int
1536+
encoder_write_float(PyEncoderObject *s, PyUnicodeWriter *writer, PyObject *obj)
1537+
{
1538+
/* Write the JSON representation of a PyFloat into writer. */
1539+
double i = PyFloat_AS_DOUBLE(obj);
1540+
if (!isfinite(i)) {
1541+
if (!s->allow_nan) {
1542+
PyErr_Format(
1543+
PyExc_ValueError,
1544+
"Out of range float values are not JSON compliant: %R",
1545+
obj
1546+
);
1547+
return -1;
1548+
}
1549+
1550+
const char *str;
1551+
size_t len;
1552+
if (i > 0) {
1553+
str = "Infinity";
1554+
len = strlen("Infinity");
1555+
}
1556+
else if (i < 0) {
1557+
str = "-Infinity";
1558+
len = strlen("-Infinity");
1559+
}
1560+
else {
1561+
str = "NaN";
1562+
len = strlen("NaN");
1563+
}
1564+
return PyUnicodeWriter_WriteASCII(writer, str, len);
1565+
}
1566+
1567+
// Similar to float_repr(), but writes directly into writer
1568+
char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(obj),
1569+
'r', 0,
1570+
Py_DTSF_ADD_DOT_0,
1571+
NULL);
1572+
if (!buf) {
1573+
PyErr_NoMemory();
1574+
return -1;
1575+
}
1576+
int res = PyUnicodeWriter_WriteASCII(writer, buf, strlen(buf));
1577+
PyMem_Free(buf);
1578+
return res;
1579+
}
1580+
15351581
static int
15361582
encoder_write_string(PyEncoderObject *s, PyUnicodeWriter *writer, PyObject *obj)
15371583
{
@@ -1596,10 +1642,7 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
15961642
return _steal_accumulate(writer, encoded);
15971643
}
15981644
else if (PyFloat_Check(obj)) {
1599-
PyObject *encoded = encoder_encode_float(s, obj);
1600-
if (encoded == NULL)
1601-
return -1;
1602-
return _steal_accumulate(writer, encoded);
1645+
return encoder_write_float(s, writer, obj);
16031646
}
16041647
else if (PyList_Check(obj) || PyTuple_Check(obj)) {
16051648
if (_Py_EnterRecursiveCall(" while encoding a JSON object"))

0 commit comments

Comments
 (0)