-
Notifications
You must be signed in to change notification settings - Fork 2.7k
refactor: JSON message with less allocations #16130
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
Open
weihanglo
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
weihanglo:serialize
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was found during experimenting `-Zbuild-analysis` with ndjson. From me tracing the code with `cargo expand`, basically there shouldn't have any significant performance difference between `serde(flatten)` and inlining all the fields. Here the differences between them * flatten one calls `Serialize::serialize_map` without fields size hint so cannot pre-allocate Vec with `Vec::with_capacity`, whereas inline case calls `Serialize::serialize_struct` with a known length of fields. * flatten would end up calling `Serializer::serialize_map` and line calls `Serializer::serialize_struct`. And in serde_json serializer `serialize_struct` actually call `serailze_map`. So no difference on serializer side. * There might be some function calls not inlined I like `FlatMapSerializer` but I doubt it is costly than allocation. Here is the `cargo-expand`'d result: ```rust #[derive(Serialize)] pub struct Foo<D: Serialize> { id: u8, #[serde(flatten)] data: D, } #[derive(Serialize)] struct Bar { a: bool, } // Expand to extern crate serde as _serde; impl<D: Serialize> _serde::Serialize for Foo<D> where D: _serde::Serialize, { fn serialize<__S>( &self, __serializer: __S, ) -> _serde::__private228::Result<__S::Ok, __S::Error> where __S: _serde::Serializer, { let mut __serde_state = _serde::Serializer::serialize_map( __serializer, _serde::__private228::None, )?; _serde::ser::SerializeMap::serialize_entry( &mut __serde_state, "id", &self.id, )?; _serde::Serialize::serialize( &&self.data, _serde::__private228::ser::FlatMapSerializer(&mut __serde_state), )?; _serde::ser::SerializeMap::end(__serde_state) } } impl _serde::Serialize for Bar { fn serialize<__S>( &self, __serializer: __S, ) -> _serde::__private228::Result<__S::Ok, __S::Error> where __S: _serde::Serializer, { let mut __serde_state = _serde::Serializer::serialize_struct( __serializer, "Bar", false as usize + 1, )?; _serde::ser::SerializeStruct::serialize_field( &mut __serde_state, "a", &self.a, )?; _serde::ser::SerializeStruct::end(__serde_state) } } ``` ```rust #[derive(Serialize)] pub struct Foo<D: Serialize> { id: u8, a: bool, } // Expand to impl<D: Serialize> _serde::Serialize for Foo<D> { fn serialize<__S>( &self, __serializer: __S, ) -> _serde::__private228::Result<__S::Ok, __S::Error> where __S: _serde::Serializer, { let mut __serde_state = _serde::Serializer::serialize_struct( __serializer, "Foo", false as usize + 1 + 1, )?; _serde::ser::SerializeStruct::serialize_field( &mut __serde_state, "id", &self.id, )?; _serde::ser::SerializeStruct::serialize_field( &mut __serde_state, "a", &self.a, )?; _serde::ser::SerializeStruct::end(__serde_state) } } ```
Just used Claude Haiku 4.5 to generate a criterioin benchamrk script for me. The benchmark was run under Here is the result:
BTW, it is interesting that I can directly pull in local path dependencies in
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-console-output
Area: Terminal output, colors, progress bar, etc.
S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR try to resolve?
This was found during experimenting
-Zbuild-analysis
with ndjson.From me tracing the code with
cargo expand
, basically there shouldn't have any significant performance difference betweenserde(flatten)
and inlining all the fields. Here the differences between themSerialize::serialize_map
without fields size hint so cannot pre-allocate Vec withVec::with_capacity
, whereas inline case callsSerialize::serialize_struct
with a known length of fields.Serializer::serialize_map
and line callsSerializer::serialize_struct
. And in serde_json serializerserialize_struct
actually callserailze_map
. So no difference on serializer side.FlatMapSerializer
but I doubt it is costly than allocation.Here is the
cargo-expand
'd result:How to test and review this PR?
CI passing.
One change is that
reason
will no longer be the first field. We could activate thepreserve_order
feature inserde_json
if we want, though that may get more perf loess than the gain.See the benchmark result in #16130 (comment)