From 22f96a6f640762a35ffb10754b99f8347453839f Mon Sep 17 00:00:00 2001 From: Yury Date: Mon, 22 Jul 2024 00:53:26 +0300 Subject: [PATCH] Switch to msgspec as faster alternative --- server/requirements-common.txt | 2 +- server/restapi/MMVC_Rest_VoiceChanger.py | 10 +++++----- server/sio/MMVC_SocketIOServer.py | 4 ++-- server/sio/serializers/msgspec.py | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 server/sio/serializers/msgspec.py diff --git a/server/requirements-common.txt b/server/requirements-common.txt index ab65301ee..7a68f0c70 100644 --- a/server/requirements-common.txt +++ b/server/requirements-common.txt @@ -19,4 +19,4 @@ pydantic-settings xxhash aiohttp certifi -msgpack +msgspec diff --git a/server/restapi/MMVC_Rest_VoiceChanger.py b/server/restapi/MMVC_Rest_VoiceChanger.py index 0479d62a9..ce74b1eb4 100644 --- a/server/restapi/MMVC_Rest_VoiceChanger.py +++ b/server/restapi/MMVC_Rest_VoiceChanger.py @@ -1,5 +1,5 @@ import numpy as np -import msgpack +from msgspec import msgpack from fastapi import APIRouter, Request from fastapi.responses import Response, PlainTextResponse @@ -30,7 +30,7 @@ def version(self): async def test(self, req: Request): try: data = await req.body() - timestamp, voice = msgpack.unpackb(data) + timestamp, voice = msgpack.decode(data) unpackedData = np.frombuffer(voice, dtype=np.int16).astype(np.float32) / 32768 @@ -40,7 +40,7 @@ async def test(self, req: Request): if err is not None: error_code, error_message = err return Response( - content=msgpack.packb({ + content=msgpack.encode({ "error": True, "timestamp": timestamp, "details": { @@ -52,7 +52,7 @@ async def test(self, req: Request): ) return Response( - content=msgpack.packb({ + content=msgpack.encode({ "error": False, "timestamp": timestamp, "audio": out_audio, @@ -64,7 +64,7 @@ async def test(self, req: Request): except Exception as e: logger.exception(e) return Response( - content=msgpack.packb({ + content=msgpack.encode({ "error": True, "timestamp": 0, "details": { diff --git a/server/sio/MMVC_SocketIOServer.py b/server/sio/MMVC_SocketIOServer.py index bc8f2cb6b..9b03e353b 100644 --- a/server/sio/MMVC_SocketIOServer.py +++ b/server/sio/MMVC_SocketIOServer.py @@ -3,7 +3,7 @@ from typing import Literal from sio.MMVC_Namespace import MMVC_Namespace from voice_changer.VoiceChangerManager import VoiceChangerManager - +from .serializers.msgspec import MsgPackPacket class MMVC_SocketIOServer: _instance: socketio.AsyncServer | None = None @@ -15,7 +15,7 @@ def get_instance( allowedOrigins: list[str] | Literal['*'], ): if cls._instance is None: - sio = socketio.AsyncServer(async_mode="asgi", serializer='msgpack', cors_allowed_origins=allowedOrigins) + sio = socketio.AsyncServer(async_mode="asgi", serializer=MsgPackPacket, cors_allowed_origins=allowedOrigins) namespace = MMVC_Namespace.get_instance(voiceChangerManager) sio.register_namespace(namespace) cls._instance = sio diff --git a/server/sio/serializers/msgspec.py b/server/sio/serializers/msgspec.py new file mode 100644 index 000000000..c03518af4 --- /dev/null +++ b/server/sio/serializers/msgspec.py @@ -0,0 +1,18 @@ +import msgspec +from socketio import packet + + +class MsgPackPacket(packet.Packet): + uses_binary_events = False + + def encode(self): + """Encode the packet for transmission.""" + return msgspec.msgpack.encode(self._to_dict()) + + def decode(self, encoded_packet): + """Decode a transmitted package.""" + decoded = msgspec.msgpack.decode(encoded_packet) + self.packet_type = decoded['type'] + self.data = decoded.get('data') + self.id = decoded.get('id') + self.namespace = decoded['nsp']