Use external package betterproto-rust-codec for better (de-)serialization performance (#545)

* optionally use betterproto-rust-codec
* monkey patch `parse` and `__bytes__`
This commit is contained in:
Erik Friese
2023-12-07 01:21:29 +01:00
committed by GitHub
parent bd7de203e1
commit d34b16993d
4 changed files with 62 additions and 33 deletions

View File

@@ -1868,6 +1868,23 @@ class Message(ABC):
Message.__annotations__ = {} # HACK to avoid typing.get_type_hints breaking :)
# monkey patch (de-)serialization functions of class `Message`
# with functions from `betterproto-rust-codec` if available
try:
import betterproto_rust_codec
def __parse_patch(self: T, data: bytes) -> T:
betterproto_rust_codec.deserialize(self, data)
return self
def __bytes_patch(self) -> bytes:
return betterproto_rust_codec.serialize(self)
Message.parse = __parse_patch
Message.__bytes__ = __bytes_patch
except ModuleNotFoundError:
pass
def serialized_on_wire(message: Message) -> bool:
"""