8000 Speed up JSON parsing with readinto by tannewt · Pull Request #3728 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

Speed up JSON parsing with readinto #3728

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

Merged
merged 2 commits into from
Nov 24, 2020
Merged

Conversation

tannewt
Copy link
Member
@tannewt tannewt commented Nov 20, 2020

Get a chunk of data from readinto instead of a single byte. This
speeds up the parsing by reducing the number of function calls.

Fixes #3703

Get a chunk of data from readinto instead of a single byte. This
speeds up the parsing by reducing the number of function calls.

Fixes micropython#3703
@tannewt tannewt added this to the 6.1.0 milestone Nov 20, 2020
@tannewt tannewt requested a review from jepler November 20, 2020 01:16
@tannewt
Copy link
Member Author
tannewt commented Nov 24, 2020

@jepler build is green. Please review.

Copy link
@jepler jepler left a comment

Choose a reason for hiding this comment

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

This looks fine. There is a small chance of changed behavior, namely that if the calling code depends on being able to do something else with the data "after" the json object, up to READ_CHUNK_SIZE-1 bytes of it will have been already consumed and it won't work.

Specifically, with the current implementation and using a file, not a socket, you can have a file with the content {}\n{} and call json.load() twice on it to get an empty object {} each time.

If this proves to be a problem in practice, this optimization may have to be revisited. I wonder if there's some way to make sockets conform to the stream protocol.

@jepler jepler merged commit 22af5a1 into adafruit:main Nov 24, 2020
@tannewt
Copy link
Member Author
tannewt commented Nov 24, 2020

Ah, true! I think the alternative to that would be to provide the full json string yourself. That's what most uses already do.

tannewt added a commit that referenced this pull request Dec 23, 2020
Speed up JSON parsing with readinto (#3728 for 6.0.x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize JSON data reading for chonky sites
3 participants
0