Serialize JSON with orjson rather than simplejson #98
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.
I have made some tries lately with big data arrays and saw that the JSON serialization was taking a long time and blocking the server.
As a first easy fix, I would propose to replace simplejson with orjson, a much faster JSON serializer that natively serializes numpy.ndarray of supported dtypes.
The use of the
jsonize
function asdefault
ensures that unsupported dtypes (such as complex) are still serialized as before. This means complex data arrays will have to be serialized using the 'old' and slower method. However, the performance gain for supported numpy arrays is quite worthwhile: orjson goes approximately ten times faster than the current implementation using simplejson (see a simple comparison script at https://gist.github.com/loichuder/82f75e336b742d63ba2817ab271ddca6).From what I see, simplejson was added in #41 to parse NaN and Infinity as
null
. This is done naitvely in orjson. Note: #97 will have to be revised.