gh-137821: Convert _json module to use Argument Clinic#140778
Merged
corona10 merged 8 commits intopython:mainfrom Oct 30, 2025
Merged
gh-137821: Convert _json module to use Argument Clinic#140778corona10 merged 8 commits intopython:mainfrom
corona10 merged 8 commits intopython:mainfrom
Conversation
added 2 commits
October 30, 2025 15:30
Contributor
Author
encode_basestring_medium: Mean +- std dev: [base] 2.11 us +- 0.05 us -> [ac] 2.33 us +- 0.04 us: 1.11x slower
encode_basestring_medium_escapes: Mean +- std dev: [base] 15.3 us +- 0.4 us -> [ac] 17.6 us +- 0.2 us: 1.15x slower
scanstring_medium: Mean +- std dev: [base] 158 ns +- 2 ns -> [ac] 110 ns +- 2 ns: 1.43x faster
Benchmark hidden because not significant (4): encode_basestring_ascii_medium, encode_basestring_ascii_medium_escapes, encode_basestring_ascii_medium_unicode, encode_basestring_medium_unicode
Geometric mean: 1.02x faster
import pyperf
import _json
# Test data - medium objects (typical API responses)
medium_string = "The quick brown fox jumps over the lazy dog. " * 20 # ~900 chars
medium_string_with_escapes = '''Line 1: "status": "success"
Line 2: Response received at 2024-01-15T10:30:00Z
\tError code: null
\tMessage: "Operation completed successfully"
''' * 50
medium_unicode = "User: 김철수 | Email: user@example.com | Status: ✓ Active | Location: 東京都 | Rating: ★★★★☆ | " * 30
medium_json_string = '"' + '{"id": 12345, "name": "John Doe", "email": "john@example.com"}' * 20 + '"'
def bench_encode_basestring_ascii(s):
return _json.encode_basestring_ascii(s)
def bench_encode_basestring(s):
return _json.encode_basestring(s)
def bench_scanstring(s):
# scanstring takes (string, end, strict=True)
# end is the index after the opening quote
return _json.scanstring(s, 1, True)
runner = pyperf.Runner()
# encode_basestring_ascii benchmarks
runner.bench_func("encode_basestring_ascii_medium", bench_encode_basestring_ascii, medium_string)
runner.bench_func("encode_basestring_ascii_medium_escapes", bench_encode_basestring_ascii, medium_string_with_escapes)
runner.bench_func("encode_basestring_ascii_medium_unicode", bench_encode_basestring_ascii, medium_unicode)
# encode_basestring benchmarks
runner.bench_func("encode_basestring_medium", bench_encode_basestring, medium_string)
runner.bench_func("encode_basestring_medium_escapes", bench_encode_basestring, medium_string_with_escapes)
runner.bench_func("encode_basestring_medium_unicode", bench_encode_basestring, medium_unicode)
# scanstring benchmarks
runner.bench_func("scanstring_medium", bench_scanstring, medium_json_string) |
Contributor
Author
|
I've checked the memory leak test. > ./python.exe -m test test_json -R3:3
Using random seed: 2482685647
0:00:00 load avg: 5.30 Run 1 test sequentially in a single process
0:00:00 load avg: 5.30 [1/1] test_json
beginning 6 repetitions. Showing number of leaks (. for 0 or less, X for 10 or more)
123:456
XX. ...
0:00:30 load avg: 4.92 [1/1] test_json passed in 30.6 sec
== Tests result: SUCCESS ==
1 test OK.
Total duration: 30.6 sec
Total tests: run=203 skipped=3
Total test files: run=1/1
Result: SUCCESS |
corona10
approved these changes
Oct 30, 2025
Member
corona10
left a comment
There was a problem hiding this comment.
LGTM, parsing overhead does not exists and it becomes better!
corona10
reviewed
Oct 30, 2025
corona10
reviewed
Oct 30, 2025
Misc/NEWS.d/next/Core_and_Builtins/2025-10-30-15-33-07.gh-issue-137821.8_Iavt.rst
Outdated
Show resolved
Hide resolved
…e-137821.8_Iavt.rst
corona10
reviewed
Oct 30, 2025
Misc/NEWS.d/next/Core_and_Builtins/2025-10-30-15-33-07.gh-issue-137821.8_Iavt.rst
Outdated
Show resolved
Hide resolved
…e-137821.8_Iavt.rst
corona10
reviewed
Oct 30, 2025
Misc/NEWS.d/next/Core_and_Builtins/2025-10-30-15-33-07.gh-issue-137821.8_Iavt.rst
Outdated
Show resolved
Hide resolved
…e-137821.8_Iavt.rst
Member
There was a problem hiding this comment.
This should be moved to Library.
Member
There was a problem hiding this comment.
@StanFromIreland I will change it to following PR.
(Need some improvement)
|
Member
|
For the record, with #140780 |
StanFromIreland
pushed a commit
to StanFromIreland/cpython
that referenced
this pull request
Dec 6, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
_jsonto Argument Clinic #137821