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-118299: Re-use old indent in json.loads() #118300

Closed
wants to merge 6 commits into from

Conversation

nineteendo
Copy link
Contributor

@nineteendo nineteendo commented Apr 25, 2024

Benchmark

macOS

script
# speedup-json.dumps.sh
echo depth 1
              main/python.exe -m timeit -s "import json; data = []" "json.dumps(data, indent=1)"
speedup-json.dumps/python.exe -m timeit -s "import json; data = []" "json.dumps(data, indent=1)"
echo depth 10
              main/python.exe -m timeit -s "import json; nest = lambda depth: [] if depth < 1 else [nest(depth - 1)]; data = nest(10)" "json.dumps(data, indent=1)"
speedup-json.dumps/python.exe -m timeit -s "import json; nest = lambda depth: [] if depth < 1 else [nest(depth - 1)]; data = nest(10)" "json.dumps(data, indent=1)"
echo depth 100
              main/python.exe -m timeit -s "import json; nest = lambda depth: [] if depth < 1 else [nest(depth - 1)]; data = nest(100)" "json.dumps(data, indent=1)"
speedup-json.dumps/python.exe -m timeit -s "import json; nest = lambda depth: [] if depth < 1 else [nest(depth - 1)]; data = nest(100)" "json.dumps(data, indent=1)"
depth 1
50000 loops, best of 5: 4.49 usec per loop # before
50000 loops, best of 5: 4.53 usec per loop # after
# -> no difference
depth 10
20000 loops, best of 5: 19.2 usec per loop # before
20000 loops, best of 5: 17 usec per loop # after
# -> 1.13x faster
depth 100
1000 loops, best of 5: 318 usec per loop # before
1000 loops, best of 5: 291 usec per loop # after
# -> 1.09x faster

Windows

script
::speedup-json.dumps.bat
@echo off
echo depth 1
call               main\python -m timeit -s "import json; data = []" "json.dumps(data, indent=1)"
call speedup-json.dumps\python -m timeit -s "import json; data = []" "json.dumps(data, indent=1)"
echo depth 10
call               main\python -m timeit -s "import json; nest = lambda depth: [] if depth < 1 else [nest(depth - 1)]; data = nest(10)" "json.dumps(data, indent=1)"
call speedup-json.dumps\python -m timeit -s "import json; nest = lambda depth: [] if depth < 1 else [nest(depth - 1)]; data = nest(10)" "json.dumps(data, indent=1)"
echo depth 100
call               main\python -m timeit -s "import json; nest = lambda depth: [] if depth < 1 else [nest(depth - 1)]; data = nest(100)" "json.dumps(data, indent=1)"
call speedup-json.dumps\python -m timeit -s "import json; nest = lambda depth: [] if depth < 1 else [nest(depth - 1)]; data = nest(100)" "json.dumps(data, indent=1)"
depth 1
50000 loops, best of 5: 4.56 usec per loop # before
50000 loops, best of 5: 4.56 usec per loop # after
# -> no difference
depth 10
10000 loops, best of 5: 21 usec per loop # before
10000 loops, best of 5: 19 usec per loop # after
# -> 1.11x faster
depth 100
500 loops, best of 5: 398 usec per loop # before
1000 loops, best of 5: 372 usec per loop # after
# -> 1.07x faster

Copy link
Member

@ericvsmith ericvsmith left a comment

Choose a reason for hiding this comment

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

This seems like too complex of a change, unless the performance impact justifies a more thorough code review. Have you measured the performance improvement?

And why rename the function parameter names? That's not making this easier to review. Renaming does not seem important. At best it should be a separate commit, although I doubt it's worth doing.

@nineteendo nineteendo marked this pull request as ready for review April 26, 2024 06:33
@nineteendo
Copy link
Contributor Author

Have you measured the performance improvement?

Yes, see the description of the pull request. If there are other cases you would like me to benchmark, let me know.

And why rename the function parameter names?

We're no longer passing the indent level, we're passing the newline indent now. I can keep the names, but it'll make the code more confusing for future readers.

@serhiy-storchaka
Copy link
Member

The difference is so small that it does not worth a NEWS entry. In most cases there is no visible difference.

I did not not expect significal difference, algorithmically it has the same complexity, and computing the indentation string has minor contribution in the total time. I proposed this approach for the C code because it may simplify it, and the saving in computing the indentation for the closing bracket may have larger impact. It is less important in Python.

I would wait with until we finish with C implementation. If it will end with this approach, it will be a small argument of using it in the Python implementation too.

@nineteendo nineteendo marked this pull request as draft April 26, 2024 12:24
@nineteendo nineteendo marked this pull request as ready for review May 6, 2024 14:29
@nineteendo
Copy link
Contributor Author

Can we use it for the Python implementation now #118105 is merged?

@serhiy-storchaka
Copy link
Member

#118105 has been merged, but the story does not end here. See #118636 which uses a different approach.

@nineteendo
Copy link
Contributor Author

Do you mean we will also be able to use caching in Python with improved performance? Aren't strings always copied when passed around?

@serhiy-storchaka
Copy link
Member

Do you mean we will also be able to use caching in Python with improved performance?

I have doubts. Even this PR is too complicated for such small gain (taking into account that normally the C implementation is used). More complex caching scheme increases the cost. On other hand, the complicated C code can be translated to simple Python code, and the benefit/ratio cost can be very good. We cannot say until we try.

Aren't strings always copied when passed around?

Objects are never copied when passed as arguments in Python. And strings are never copied, they are immutable.

@nineteendo
Copy link
Contributor Author

nineteendo commented May 7, 2024

Even this PR is too complicated for such small gain

OK, I'll close it then, or did I misunderstand you?

@nineteendo nineteendo closed this May 7, 2024
@nineteendo nineteendo deleted the speedup-json.dumps branch May 7, 2024 13:53
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.

None yet

3 participants