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.
runtime_to_msgspec
Tyler Goodlet 2024-04-24 13:07:05 -04:00
parent 6aa52417ef
commit 6b30c86eca
2 changed files with 21 additions and 1 deletions

View File

@ -37,6 +37,11 @@ from ._codec import (
MsgDec as MsgDec, MsgDec as MsgDec,
current_codec as current_codec, 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 ( from .types import (
Msg as Msg, Msg as Msg,

View File

@ -280,17 +280,32 @@ class MsgCodec(Struct):
def enc(self) -> msgpack.Encoder: def enc(self) -> msgpack.Encoder:
return self._enc 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( def encode(
self, self,
py_obj: Any, 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: ) -> bytes:
''' '''
Encode input python objects to `msgpack` bytes for Encode input python objects to `msgpack` bytes for
transfer on a tranport protocol connection. 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 @property
def dec(self) -> msgpack.Decoder: def dec(self) -> msgpack.Decoder: