Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize JSON with orjson rather than simplejson #98

Closed
wants to merge 1 commit into from

Conversation

loichuder
Copy link
Collaborator

@loichuder loichuder commented May 3, 2021

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 as default 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.

@@ -329,7 +331,7 @@ def jsonize(v):
return [v.real, v.imag]
if isinstance(v, h5py.Empty):
return None
return v
raise TypeError("Cannot jsonize {}".format(type(v)))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I had to change jsonize a bit to fit the expected behaviour of default: https://github.com/ijl/orjson#default

@loichuder
Copy link
Collaborator Author

Closing in favour of #110

@loichuder loichuder closed this Sep 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant