So, a couple of days ago I read a blogpost on the PyPy Status Blog written by Maciej Fijalkowski about speeding up JSON encoding in PyPy. Fijal is one of the core developers of PyPy, and in his blogpost he explains how he edited the JSON encoder to better fit the PyPy style (remember that many python files in PyPy are still tuned for running on cPython, and can be improved with simple changes).
I don’t have any particular suggestions different from the ones presented by Fijal, just want to point out that JITViewer is really useful for understanding where the code could be improved, and that sometimes just being smart can save quite a bit of computation time.
I spent some time trying to improve the string concatenation of
decoder.py. The current version builds a list of chunks that are eventually joined together to build the concatenated string. This appears to be actually the fastest way (in this instance, at least): I tried the
__pypy__.builders.StringBuilder and the classic
+= concatenation, but they turn out to be less efficient (so, mission failed, here).
I instead changed the way
JSONObject builds its list of key-value elements: from Python 2.7 the decoder supports a new parameter,
object_pairs_hook, for preserving ordering and multiple instances of keys. For supporting it,
JSONObject builds a list of tuples storing each key-value parsed, converting it in a dictionary if no
object_pairs_hook is given; the point here is that building a list of tuples is much slower than building a dictionary, and we would probably prefer to avoid this if we actually don’t need a list of pairs. This simple improvements helped speeding up the decoder a bit, achieving some good results.
I tested the new code on PyPy 1.8.1-dev0 using two benchmarks: a modification of pypy/benchmarks/json_bench (which currently evaluates only encoding, and not decoding) and kracekumar/cerealization, here the results:
|PyPy 1.8.1-dev0||PyPy 1.8.1-dev0 MOD||Speedup|
|Results on pypy/benchmarks/json_bench|
|cPython 2.6.1||cPython 2.7.2||PyPy 1.8.1-dev0||PyPy 1.8.1-dev0 MOD|
|Load: json||99.1293950081||13.0056598186||10.1231429577||7.314661026 (+38.39%)|
|Results on kracekumar/celeralization|
Code is available on BitBucket.