Skip to content
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.

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

gh-95385 Fastpath for encoding dict to JSON #95374

Merged
merged 9 commits into from
Aug 6, 2022

Conversation

aivarsk
Copy link
Contributor

@aivarsk aivarsk commented Jul 28, 2022

When sorting of keys is not requested and we are encoding a dict
use PyDict_Next to iterate over keys and values.
When sorting is requested use PyList_GET_ITEM instead of PyIter_Next because we know we are working with a list.

@aivarsk aivarsk force-pushed the dict_to_json_fastpath branch 2 times, most recently from 4d40abf to e2929e7 Compare July 28, 2022 13:11
@aivarsk aivarsk changed the title gh-NNNNN Fastpath for encoding dict to JSON gh-95385 Fastpath for encoding dict to JSON Jul 28, 2022
@aivarsk
Copy link
Contributor Author

aivarsk commented Jul 28, 2022

The code is based on another PR (gh-95382) not merged yet just to avoid passing extra parameters around.

@aivarsk aivarsk marked this pull request as ready for review July 28, 2022 14:30
Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate the patch from removing indent_level params.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

When sorting of keys is not requsted and we are encoding a dict
use PyDict_Next to iterate over keys and values.
Leave the old path with PyMapping_Items that creates a list of
key-value tuples for cases when sorting is requested.
Don't use mapping items and iterator: we check and know we are
using a dict and list.
@aivarsk
Copy link
Contributor Author

aivarsk commented Jul 28, 2022

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@corona10: please review the changes made to this pull request.

Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like to provide a benchmark script and result for the following conditions?

  • json.dumps when sorting of key is required
  • json.dumps when sorting of key is not required (your suggestion)

@aivarsk
Copy link
Contributor Author

aivarsk commented Jul 29, 2022

This is without sorting of keys, the default behavior of json.dumps
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

@aivarsk
Copy link
Contributor Author

aivarsk commented Jul 29, 2022

With pyperformance sort_keys=True, I edited benchmarks/bm_json_dumps/run_benchmark.py for that

before changes

### json_dumps ###
Mean +- std dev: 14.0 ms +- 0.2 ms

after changes (PyList_GET_ITEM is a bit faster than PyIter_Next?)

### json_dumps ###
Mean +- std dev: 13.6 ms +- 0.2 ms

@corona10
Copy link
Member

@aivarsk Thank you, I will take a look at this PR through this weekend :)

@corona10 corona10 self-assigned this Jul 29, 2022
@mdboom mdboom added the performance Performance or resource usage label Aug 1, 2022
@corona10 corona10 added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 3, 2022
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @corona10 for commit 9137b1b 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 3, 2022
Modules/_json.c Outdated Show resolved Hide resolved
Modules/_json.c Outdated Show resolved Hide resolved
@aivarsk aivarsk requested a review from corona10 August 4, 2022 09:43
Modules/_json.c Outdated Show resolved Hide resolved
Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, I should check several possibilities of regression including leaks but not found :)

Overall looks good LGTM, but please follow PEP 7 if possible.
I just left nit comments :)

Modules/_json.c Outdated Show resolved Hide resolved
Modules/_json.c Outdated Show resolved Hide resolved
Modules/_json.c Outdated Show resolved Hide resolved
Modules/_json.c Outdated Show resolved Hide resolved
aivarsk and others added 4 commits August 5, 2022 23:03
Co-authored-by: Dong-hee Na <[email protected]>
Co-authored-by: Dong-hee Na <[email protected]>
Co-authored-by: Dong-hee Na <[email protected]>
Co-authored-by: Dong-hee Na <[email protected]>
@corona10 corona10 merged commit 15f4a35 into python:main Aug 6, 2022
iritkatriel pushed a commit to iritkatriel/cpython that referenced this pull request Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants