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..76503cdd0 100644 --- a/plugin/core/transports.py +++ b/plugin/core/transports.py @@ -7,7 +7,6 @@ from queue import Queue from typing import Any, Callable, Dict, Generic, IO, Protocol, TypeVar import http -import json import os import shutil import socket @@ -17,6 +16,11 @@ import time import weakref +try: + import orjson +except ImportError: + import json + orjson = None T = TypeVar('T') T_contra = TypeVar('T_contra', contravariant=True) @@ -93,6 +97,17 @@ def _decode(message: bytes) -> dict[str, Any]: return json.loads(message.decode('utf-8')) +class OrjsonRpcProcessor(JsonRpcProcessor): + + @staticmethod + def _encode(data: dict[str, Any]) -> bytes: + return orjson.dumps(data) + + @staticmethod + def _decode(message: bytes) -> dict[str, Any]: + return orjson.loads(message) + + class ProcessTransport(Transport[T]): def __init__(self, name: str, process: subprocess.Popen | None, socket: socket.socket | None, @@ -231,7 +246,10 @@ def _stderr_loop(self) -> None: # Can be a singleton since it doesn't hold any state. -json_rpc_processor = JsonRpcProcessor() +if orjson: + json_rpc_processor = OrjsonRpcProcessor() +else: + json_rpc_processor = JsonRpcProcessor() def create_transport(config: TransportConfig, cwd: str | None,