Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: too many temporary allocation when serializing data #389

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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