From 6b30c86eca20f01ac0b33787cd3caa04f08dd136 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 24 Apr 2024 13:07:05 -0400 Subject: [PATCH] Try out `msgspec` encode-buffer optimization As per the reco: https://jcristharif.com/msgspec/perf-tips.html#reusing-an-output-buffe BUT, seems to cause this error in `pikerd`.. `BufferError: Existing exports of data: object cannot be re-sized` Soo no idea? Maybe there's a tweak needed that we can glean from tests/examples in the `msgspec` repo? Disabling for now. --- tractor/msg/__init__.py | 5 +++++ tractor/msg/_codec.py | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tractor/msg/__init__.py b/tractor/msg/__init__.py index d968f6c..13739cd 100644 --- a/tractor/msg/__init__.py +++ b/tractor/msg/__init__.py @@ -37,6 +37,11 @@ from ._codec import ( MsgDec as MsgDec, current_codec as current_codec, ) +# currently can't bc circular with `._context` +# from ._ops import ( +# PldRx as PldRx, +# _drain_to_final_msg as _drain_to_final_msg, +# ) from .types import ( Msg as Msg, diff --git a/tractor/msg/_codec.py b/tractor/msg/_codec.py index 104f7d9..e3540c3 100644 --- a/tractor/msg/_codec.py +++ b/tractor/msg/_codec.py @@ -280,17 +280,32 @@ class MsgCodec(Struct): def enc(self) -> msgpack.Encoder: return self._enc + # TODO: reusing encode buffer for perf? + # https://jcristharif.com/msgspec/perf-tips.html#reusing-an-output-buffer + _buf: bytearray = bytearray() + def encode( self, py_obj: Any, + use_buf: bool = False, + # ^-XXX-^ uhh why am i getting this? + # |_BufferError: Existing exports of data: object cannot be re-sized + ) -> bytes: ''' Encode input python objects to `msgpack` bytes for transfer on a tranport protocol connection. + When `use_buf == True` use the output buffer optimization: + https://jcristharif.com/msgspec/perf-tips.html#reusing-an-output-buffer + ''' - return self._enc.encode(py_obj) + if use_buf: + self._enc.encode_into(py_obj, self._buf) + return self._buf + else: + return self._enc.encode(py_obj) @property def dec(self) -> msgpack.Decoder: