pub struct SerBuffer;Expand description
Thread-local reusable serialization buffer.
Avoids the per-call Vec<u8> allocation from serde_json::to_vec().
The buffer is cleared (but not deallocated) between uses, so repeated
serializations reuse the same heap allocation.
§Performance impact
- Small payloads (<256B): Eliminates the 2.3× allocation overhead.
serde_json::to_vecallocates a newVec<u8>with ~80 bytes of initial overhead per call. WithSerBuffer, this overhead is paid once. - Large payloads (>1KB): Minimal benefit — the buffer grows to match the payload and the fixed overhead is negligible.
§Thread safety
Each thread gets its own buffer via thread_local!. There is no
cross-thread contention. The buffer is never shared.
Implementations§
Source§impl SerBuffer
impl SerBuffer
Sourcepub fn serialize<T>(value: &T) -> Result<Vec<u8>, Error>where
T: Serialize,
pub fn serialize<T>(value: &T) -> Result<Vec<u8>, Error>where
T: Serialize,
Serializes value into a reusable thread-local buffer and returns
the bytes as a new Vec<u8>.
The thread-local buffer is reused across calls — only one allocation occurs per thread (on first use), then the buffer grows as needed but is never deallocated between calls.
§Errors
Returns a serde_json::Error if serialization fails.
Auto Trait Implementations§
impl Freeze for SerBuffer
impl RefUnwindSafe for SerBuffer
impl Send for SerBuffer
impl Sync for SerBuffer
impl Unpin for SerBuffer
impl UnsafeUnpin for SerBuffer
impl UnwindSafe for SerBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request