cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-95382: Use cache for indentations in the JSON encoder

Open serhiy-storchaka opened this issue 1 year ago • 0 comments

It is a continuation of #118105 which keeps a cache of newline+indentation and comma+newline+indentation at every level.

For example:

$ ./python -m timeit -s 'import json; a = [[[{"key": "value"}]*10]*10]*10' 'json.dumps(a, indent=2)'
Before: 200 loops, best of 5: 1.79 msec per loop
After:  200 loops, best of 5: 1.25 msec per loop
$ ./python -m timeit -s 'import json; a = [[[list(range(10))]*10]*10]*10' 'json.dumps(a, indent=2)'
Before: 50 loops, best of 5: 5.45 msec per loop
After:  50 loops, best of 5: 4.78 msec per loop

The effect is the strongest if there are many deep narrow trees growing from the root:

$ ./python -m timeit -s 'import json; a = [[[["nested"]]]]*1000' 'json.dumps(a, indent=2)'
100 loops, best of 5: 3.39 msec per loop
200 loops, best of 5: 1.85 msec per loop

And the weakest (up to no difference) if the tree contains few large flat lists or dicts.

  • Issue: gh-95382

serhiy-storchaka avatar May 06 '24 08:05 serhiy-storchaka