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

Pottential speedup: ujson or orjson #1453

Closed
davfsa opened this issue Jan 4, 2023 · 4 comments · Fixed by #1486
Closed

Pottential speedup: ujson or orjson #1453

davfsa opened this issue Jan 4, 2023 · 4 comments · Fixed by #1486
Labels
optimization Optimizations to the code

Comments

@davfsa
Copy link
Member

davfsa commented Jan 4, 2023

A potential speedup we could add to the mix is ujson, which after some quick testing, it seems to be a drop in replacement to the standard libraries implementation, but performs a lot better, which is already noticeable on startup, so could be even more significant for large bots, so might be worth looking into:

stdlib:
image

ujson:
image

Typehints are provided through typeshed, so shouldnt have an issue with typecheckers


If you want to provide feedback on this issue and test it yourself, you can easily do it by adding the following lines to your bot script (after installing ujson):

import ujson
from hikari.internal import data_binding

data_binding.dump_json = ujson.dumps
data_binding.load_json = ujson.loads
@davfsa davfsa added the optimization Optimizations to the code label Jan 4, 2023
@zunda-arrow
Copy link
Contributor

Have you looked into orjson? orjson is a larger library compared to ujson but its marginally faster in decoding benchmarks I think are more applicable to this project and its encoding speed is over 2x faster than ujson in all the benchmarks.

https://github.com/ultrajson/ultrajson#benchmarks

@davfsa
Copy link
Member Author

davfsa commented Jan 4, 2023

I did in fact look at it, but it seems to perform slower than ujson in our case. I re-ran the benchmarks provided by ujson here and did see orjson performing considerably better, but when it comes to a Discord workload, ujson shows better performance. I havent really looked into it (a bit why I opened this issue instead of a PR).

You can test it out yourself by replacing this specific line with send_bytes

await self._ws.send_str(pl)

If anyone can run performance tests on their bots, please feel free to share, they are greatly welcome!


PS: While looking into this, I stumbled on this issue from Sanic regarding the same results I encountered: sanic-org/sanic#1479 (comment)

@parafoxia
Copy link
Contributor

So I've experimented on Carberretta, and the differences are pretty minimal. I tried 3 times with each, and these were the average results:

  • Normal: 2.22s
  • ujson: 2.17s
  • orjson: 2.25s

I reckon some more data could be helpful, but there doesn't seem to be any major differences. The differences are smaller on a bot with more parts at the very least.

@davfsa davfsa changed the title Pottential speedup: ujson Pottential speedup: ujson or orjson Jan 20, 2023
@davfsa
Copy link
Member Author

davfsa commented Jan 20, 2023

Since orjson was brought up, I experimented a bit with it and came up this branch.

I am still not fully happy with the outcome and have some plans to improve it even more, bringing in support to be able to pass json.loads and json.dumps singnature compliant functions.

Regardless, any testing on that branch (specially in large bots, if possible) would be welcome and appreciated!

To install from the branch using pip, you can use:

pip install git+https://github.com/davfsa/hikari@task/orjson#egg=hikari[speedups]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimization Optimizations to the code
Projects
None yet
3 participants