Skip to content

Commit

Permalink
Use orjson to de-/serialize json-rpc messages
Browse files Browse the repository at this point in the history
  • Loading branch information
deathaxe committed Sep 13, 2024
1 parent f4223d6 commit 0a74822
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
">=4096": [
"bracex",
"mdpopups",
"orjson",
"typing_extensions",
"wcmatch"
]
Expand Down
22 changes: 20 additions & 2 deletions plugin/core/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0a74822

Please sign in to comment.