Skip to content

Commit

Permalink
fix: temporary allocation for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Totodore committed Oct 27, 2024
1 parent 11a827a commit c86f0e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions crates/parser-common/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ pub fn from_value<'de, T: Deserialize<'de>>(

/// Serialize any serializable data and an event to a generic [`SocketIoValue`] data.
pub fn to_value<T: ?Sized + Serialize>(data: &T, event: Option<&str>) -> serde_json::Result<Value> {
let (writer, binary) = if is_ser_tuple(data) {
let (buff, binary) = if is_ser_tuple(data) {
ser::into_str(data, event)?
} else {
ser::into_str(&(data,), event)?
};
let data = unsafe { Str::from_bytes_unchecked(Bytes::from(writer)) };
let data = unsafe { Str::from_bytes_unchecked(buff) };
Ok(Value::Str(data, (!binary.is_empty()).then_some(binary)))
}

Expand Down
7 changes: 4 additions & 3 deletions crates/parser-common/src/value/ser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module contains a specialized JSON serializer wrapper that can serialize binary payloads as placeholders.
use std::{cell::UnsafeCell, collections::VecDeque};

use bytes::Bytes;
use bytes::{BufMut, Bytes, BytesMut};

Check warning

Code scanning / clippy

unused imports: BufMut and BytesMut Warning

unused imports: BufMut and BytesMut

Check warning

Code scanning / clippy

unused imports: BufMut and BytesMut Warning

unused imports: BufMut and BytesMut

Check warning

Code scanning / clippy

unused imports: BufMut and BytesMut Warning

unused imports: BufMut and BytesMut

Check warning

Code scanning / clippy

unused imports: BufMut and BytesMut Warning

unused imports: BufMut and BytesMut
use serde::ser::{
self, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
SerializeTupleStruct, SerializeTupleVariant,
Expand All @@ -16,7 +16,7 @@ use serde::ser::{
pub fn into_str<T: ?Sized + ser::Serialize>(
data: &T,
event: Option<&str>,
) -> Result<(Vec<u8>, VecDeque<Bytes>), serde_json::Error> {
) -> Result<(Bytes, VecDeque<Bytes>), serde_json::Error> {
let mut writer = Vec::new();
let binary_payloads = UnsafeCell::new(VecDeque::new());
let ser = &mut serde_json::Serializer::new(&mut writer);
Expand All @@ -27,7 +27,8 @@ pub fn into_str<T: ?Sized + ser::Serialize>(
is_root: true,
};
data.serialize(ser)?;
Ok((writer, binary_payloads.into_inner()))
writer.shrink_to_fit();
Ok((Bytes::from(writer), binary_payloads.into_inner()))
}

struct Serializer<'a, S> {
Expand Down

0 comments on commit c86f0e6

Please sign in to comment.