From 373e3bba1e1833778a8b38db17fbaef3d7c3c971 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Mon, 30 Sep 2024 20:09:08 +0200 Subject: [PATCH] Use orjson to de-/serialize json-rpc messages (#2513) --- .github/workflows/main.yml | 2 +- dependencies.json | 1 + plugin/core/transports.py | 8 ++++++++ tox.ini | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 268c9241a..a6d162b57 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -41,7 +41,7 @@ jobs: python-version: '3.8' - run: sudo apt update - run: sudo apt install --no-install-recommends -y x11-xserver-utils - - run: pip3 install mypy==1.7.1 flake8==5.0.4 pyright==1.1.381 --user + - run: pip3 install mypy==1.7.1 flake8==5.0.4 pyright==1.1.381 orjson==3.10.7 --user - run: echo "$HOME/.local/bin" >> $GITHUB_PATH - run: mypy stubs - run: flake8 plugin tests diff --git a/dependencies.json b/dependencies.json index 89cc5ab0c..0b7b7aae1 100644 --- a/dependencies.json +++ b/dependencies.json @@ -3,6 +3,7 @@ ">=4096": [ "bracex", "mdpopups", + "orjson", "typing_extensions", "wcmatch" ] diff --git a/plugin/core/transports.py b/plugin/core/transports.py index b0e91f89c..1ea3cbbf7 100644 --- a/plugin/core/transports.py +++ b/plugin/core/transports.py @@ -17,6 +17,10 @@ import time import weakref +try: + import orjson +except ImportError: + orjson = None T = TypeVar('T') T_contra = TypeVar('T_contra', contravariant=True) @@ -80,6 +84,8 @@ def read_data(self, reader: IO[bytes]) -> dict[str, Any] | None: @staticmethod def _encode(data: dict[str, Any]) -> bytes: + if orjson: + return orjson.dumps(data) return json.dumps( data, ensure_ascii=False, @@ -90,6 +96,8 @@ def _encode(data: dict[str, Any]) -> bytes: @staticmethod def _decode(message: bytes) -> dict[str, Any]: + if orjson: + return orjson.loads(message) return json.loads(message.decode('utf-8')) diff --git a/tox.ini b/tox.ini index c00122615..2297f8e58 100644 --- a/tox.ini +++ b/tox.ini @@ -23,6 +23,7 @@ deps = mypy==1.7.1 flake8==5.0.4 pyright==1.1.381 + orjson==3.10.7 commands = # mypy disabled for main code as it doesn't currently support cyclic definitions - https://github.com/python/mypy/issues/731 mypy stubs