GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-95385 Fastpath for encoding dict to JSON by aivarsk · Pull Request #95374 · python/cpython

gh-95385 Fastpath for encoding dict to JSON #95374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
corona10 merged 9 commits into python:main from aivarsk:dict_to_json_fastpath
Aug 6, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-95385 Fastpath for encoding dict to JSON #95374

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
9 commits
Select commit Hold shift + click to select a range
c7b5cc2
Fastpath for encoding dict to JSON
aivarsk Jul 27, 2022
9137b1b
NEWS
aivarsk Jul 28, 2022
751e042
explicit checks
aivarsk Aug 3, 2022
8ae1d21
smaller diff
aivarsk Aug 4, 2022
7677295
Use PyDict_Items
aivarsk Aug 5, 2022
a21f77a
Update Modules/_json.c
aivarsk Aug 5, 2022
4734d70
Update Modules/_json.c
aivarsk Aug 5, 2022
79e6880
Update Modules/_json.c
aivarsk Aug 5, 2022
fda2ee7
Update Modules/_json.c
aivarsk Aug 5, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2022-07-28-17-14-38.gh-issue-95385.6YlsDI.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Faster ``json.dumps()`` when sorting of keys is not requested (default).
166 changes: 88 additions & 78 deletions 166 Modules/_json.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Python.h"
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "structmember.h" // PyMemberDef
#include <stdbool.h> // bool


typedef struct _PyScannerObject {
Expand Down Expand Up @@ -1491,17 +1492,79 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer,
}
}

static int
encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *first,
Comment thread
corona10 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

PyObject *key, PyObject *value, Py_ssize_t indent_level)
{
PyObject *keystr = NULL;
PyObject *encoded;

if (PyUnicode_Check(key)) {
Py_INCREF(key);
keystr = key;
}
else if (PyFloat_Check(key)) {
keystr = encoder_encode_float(s, key);
}
else if (key == Py_True || key == Py_False || key == Py_None) {
/* This must come before the PyLong_Check because
True and False are also 1 and 0.*/
keystr = _encoded_const(key);
}
else if (PyLong_Check(key)) {
keystr = PyLong_Type.tp_repr(key);
}
else if (s->skipkeys) {
return 0;
}
else {
PyErr_Format(PyExc_TypeError,
"keys must be str, int, float, bool or None, "
"not %.100s", Py_TYPE(key)->tp_name);
return -1;
}

if (keystr == NULL) {
return -1;
}

if (*first) {
*first = false;
}
else {
if (_PyUnicodeWriter_WriteStr(writer, s->item_separator) < 0) {
Py_DECREF(keystr);
return -1;
}
}

encoded = encoder_encode_string(s, keystr);
Py_DECREF(keystr);
if (encoded == NULL) {
return -1;
}

if (_steal_accumulate(writer, encoded) < 0) {
return -1;
}
if (_PyUnicodeWriter_WriteStr(writer, s->key_separator) < 0) {
return -1;
}
if (encoder_listencode_obj(s, writer, value, indent_level) < 0) {
return -1;
}
return 0;
}

static int
encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer,
PyObject *dct, Py_ssize_t indent_level)
{
/* Encode Python dict dct a JSON term */
PyObject *kstr = NULL;
PyObject *ident = NULL;
PyObject *it = NULL;
PyObject *items;
PyObject *item = NULL;
Py_ssize_t idx;
PyObject *items = NULL;
PyObject *key, *value;
bool first = true;

if (PyDict_GET_SIZE(dct) == 0) /* Fast path */
return _PyUnicodeWriter_WriteASCIIString(writer, "{}", 2);
Expand Down Expand Up @@ -1535,84 +1598,34 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer,
*/
}

items = PyMapping_Items(dct);
if (items == NULL)
goto bail;
if (s->sort_keys && PyList_Sort(items) < 0) {
Py_DECREF(items);
goto bail;
}
it = PyObject_GetIter(items);
Py_DECREF(items);
if (it == NULL)
goto bail;
idx = 0;
while ((item = PyIter_Next(it)) != NULL) {
PyObject *encoded, *key, *value;
if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
if (s->sort_keys) {

items = PyDict_Items(dct);
Comment thread
corona10 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

if (items == NULL || PyList_Sort(items) < 0)
goto bail;
}
key = PyTuple_GET_ITEM(item, 0);
if (PyUnicode_Check(key)) {
Py_INCREF(key);
kstr = key;
}
else if (PyFloat_Check(key)) {
kstr = encoder_encode_float(s, key);
if (kstr == NULL)
goto bail;
}
else if (key == Py_True || key == Py_False || key == Py_None) {
/* This must come before the PyLong_Check because
True and False are also 1 and 0.*/
kstr = _encoded_const(key);
if (kstr == NULL)
goto bail;
}
else if (PyLong_Check(key)) {
kstr = PyLong_Type.tp_repr(key);
if (kstr == NULL) {

for (Py_ssize_t i = 0; i < PyList_GET_SIZE(items); i++) {
PyObject *item = PyList_GET_ITEM(items, i);

if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
goto bail;
}
}
else if (s->skipkeys) {
Py_DECREF(item);
continue;
}
else {
PyErr_Format(PyExc_TypeError,
"keys must be str, int, float, bool or None, "
"not %.100s", Py_TYPE(key)->tp_name);
goto bail;
}

if (idx) {
if (_PyUnicodeWriter_WriteStr(writer, s->item_separator))
key = PyTuple_GET_ITEM(item, 0);
value = PyTuple_GET_ITEM(item, 1);
if (encoder_encode_key_value(s, writer, &first, key, value, indent_level) < 0)
goto bail;
}
Py_CLEAR(items);

encoded = encoder_encode_string(s, kstr);
Py_CLEAR(kstr);
if (encoded == NULL)
goto bail;
if (_PyUnicodeWriter_WriteStr(writer, encoded)) {
Py_DECREF(encoded);
goto bail;
} else {
Py_ssize_t pos = 0;
while (PyDict_Next(dct, &pos, &key, &value)) {
Comment thread
corona10 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

if (encoder_encode_key_value(s, writer, &first, key, value, indent_level) < 0)
goto bail;
}
Py_DECREF(encoded);
if (_PyUnicodeWriter_WriteStr(writer, s->key_separator))
goto bail;

value = PyTuple_GET_ITEM(item, 1);
if (encoder_listencode_obj(s, writer, value, indent_level))
goto bail;
idx += 1;
Py_DECREF(item);
}
if (PyErr_Occurred())
goto bail;
Py_CLEAR(it);

if (ident != NULL) {
if (PyDict_DelItem(s->markers, ident))
Expand All @@ -1630,14 +1643,11 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer,
return 0;

bail:
Py_XDECREF(it);
Py_XDECREF(item);
Py_XDECREF(kstr);
Py_XDECREF(items);
Py_XDECREF(ident);
return -1;
}


static int
encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer,
PyObject *seq, Py_ssize_t indent_level)
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.