Skip to content

Commit

Permalink
Allow use of orjson instead of json
Browse files Browse the repository at this point in the history
The difference in speed seems negligible at start up, which is when
most time is taken for actually parsing JSON. I could potentially be
missing something but profiling didn't point to any discernable
difference.
  • Loading branch information
Rapptz committed Jul 8, 2021
1 parent e0a9365 commit 88d825a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
5 changes: 2 additions & 3 deletions discord/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import asyncio
from collections import namedtuple, deque
import concurrent.futures
import json
import logging
import struct
import sys
Expand Down Expand Up @@ -421,7 +420,7 @@ async def received_message(self, msg):
msg = self._zlib.decompress(self._buffer)
msg = msg.decode('utf-8')
self._buffer = bytearray()
msg = json.loads(msg)
msg = utils.from_json(msg)

log.debug('For Shard ID %s: WebSocket Event: %s', self.shard_id, msg)
self._dispatch('socket_response', msg)
Expand Down Expand Up @@ -882,7 +881,7 @@ async def poll_event(self):
# This exception is handled up the chain
msg = await asyncio.wait_for(self.ws.receive(), timeout=30.0)
if msg.type is aiohttp.WSMsgType.TEXT:
await self.received_message(json.loads(msg.data))
await self.received_message(utils.from_json(msg.data))
elif msg.type is aiohttp.WSMsgType.ERROR:
log.debug('Received %s', msg)
raise ConnectionClosed(self.ws, shard_id=None) from msg.data
Expand Down
2 changes: 1 addition & 1 deletion discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def json_or_text(response: aiohttp.ClientResponse) -> Union[Dict[str, Any]
text = await response.text(encoding='utf-8')
try:
if response.headers['content-type'] == 'application/json':
return json.loads(text)
return utils.from_json(text)
except KeyError:
# Thanks Cloudflare
pass
Expand Down
23 changes: 21 additions & 2 deletions discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@

from .errors import InvalidArgument

try:
import orjson
except ModuleNotFoundError:
HAS_ORJSON = False
else:
HAS_ORJSON = True


__all__ = (
'oauth_url',
'snowflake_time',
Expand Down Expand Up @@ -468,8 +476,19 @@ def _bytes_to_base64_data(data: bytes) -> str:
return fmt.format(mime=mime, data=b64)


def to_json(obj: Any) -> str:
return json.dumps(obj, separators=(',', ':'), ensure_ascii=True)
if HAS_ORJSON:

def to_json(obj: Any) -> str: # type: ignore
return orjson.dumps(obj).decode('utf-8')

from_json = orjson.loads # type: ignore

else:

def to_json(obj: Any) -> str:
return json.dumps(obj, separators=(',', ':'), ensure_ascii=True)

from_json = json.loads


def _parse_ratelimit_header(request: Any, *, use_clock: bool = False) -> float:
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
'sphinx==4.0.2',
'sphinxcontrib_trio==1.1.2',
'sphinxcontrib-websupport',
],
'speed': [
'orjson>=3.5.4',
]
}

Expand Down

0 comments on commit 88d825a

Please sign in to comment.