hikari-py/hikari

Pottential speedup: ujson or orjson

davfsa opened this issue · 4 comments

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

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

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)

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.

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]