Skip to content

bpo-37587: _json: use _PyUnicodeWriter when scanning string - #15591

Merged
methane merged 5 commits into
python:masterfrom
methane:json-unicodewriter
Oct 17, 2019
Merged

bpo-37587: _json: use _PyUnicodeWriter when scanning string#15591
methane merged 5 commits into
python:masterfrom
methane:json-unicodewriter

Conversation

@methane

@methane methane commented Aug 29, 2019

Copy link
Copy Markdown
Member
$ ./python -m pyperf compare_to before.json patched.json
short-ascii: Mean +- std dev: [before] 23.4 ms +- 0.3 ms -> [patched] 23.8 ms +- 0.1 ms: 1.02x slower (+2%)
long-no-ascii: Mean +- std dev: [before] 47.1 us +- 0.2 us -> [patched] 14.1 us +- 0.2 us: 3.34x faster (-70%)

https://bugs.python.org/issue37587

@serhiy-storchaka

Copy link
Copy Markdown
Member

What about mostly-ASCII strings with rare non-ASCII inclusions?

@serhiy-storchaka

Copy link
Copy Markdown
Member

Also what about short ASCII-only strings?

@@ -0,0 +1,2 @@
Use ``_PyUnicodeWriter`` instead of list of unicode chunks in

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

_PyUnicodeWriter is a deep implementation detail. _json.scanstring is also an implementation detail. The end user should not be aware of them. Just describe the effect of the optimization. It is worth to add also a What's New entry.

@methane

methane commented Sep 10, 2019

Copy link
Copy Markdown
Member Author

Also what about short ASCII-only strings?

In the worst case, this pull request make it more than 2x slower.

$ ./python -m pyperf timeit -o patched.json -s 'import json; x=json.dumps(["a"]*1000000)' -- 'json.loads(x)'

$ ./python -m pyperf compare_to master.json patched.json
Mean +- std dev: [master] 24.6 ms +- 0.0 ms -> [patched] 61.0 ms +- 0.2 ms: 2.48x slower (+148%)

@methane

methane commented Sep 12, 2019

Copy link
Copy Markdown
Member Author
$ ./python -m pyperf compare_to before.json patched.json
short-ascii: Mean +- std dev: [before] 23.4 ms +- 0.3 ms -> [patched] 23.8 ms +- 0.1 ms: 1.02x slower (+2%)
long-no-ascii: Mean +- std dev: [before] 47.1 us +- 0.2 us -> [patched] 14.1 us +- 0.2 us: 3.34x faster (-70%)

The difference between 53.5 us and 47.1 us is came from compilers: 53.5 us on clang-7 and 47.1 us on gcc 8.3.0.

@methane
methane merged commit 9c11029 into python:master Oct 17, 2019
@methane
methane deleted the json-unicodewriter branch October 17, 2019 07:12
jacobneiltaylor pushed a commit to jacobneiltaylor/cpython that referenced this pull request Dec 5, 2019
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
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