Closed
Labels
3.12only security fixesperformancePerformance or resource usagetype-featureA feature request or enhancement
Description
JSON encoder can produce output sorted by key (json.dumps(sort_keys=True)) The default value for sort_keys is False. The code always creates a list of dictionary items, sorts them if needed, and then iterates through them.
However, if sorting is not requested we can use PyDict_Next to avoid allocating a list of tuples.
pyperformance on my machine before changes:
### json_dumps ###
Mean +- std dev: 10.6 ms +- 0.1 ms
pyperformance after changes:
### json_dumps ###
Mean +- std dev: 9.00 ms +- 0.08 ms
Metadata
Metadata
Assignees
Labels
3.12only security fixesperformancePerformance or resource usagetype-featureA feature request or enhancement
Projects
- StatusShow more project fieldsDone
Activity
aivarsk commentedon Jul 29, 2022
Patched pyperformance to use
json.dumps(sort_keys=True)before changes
after changes
Probably because the new code does not create an iterator from the list and PyList_GET_ITEM is a bit faster than PyIter_Next (extra incref/decref)?
gh-95385 Fastpath for encoding dict to JSON (gh-95374)
pythongh-95385 Fastpath for encoding dict to JSON (pythongh-95374)
serhiy-storchaka commentedon Dec 15, 2022
It caused a regression for OrderedDict. See #100272.