-
Notifications
You must be signed in to change notification settings - Fork 38
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
Reduce monomorphization in Deserializer implementation #126
Open
DaniPopes
wants to merge
1
commit into
SergioBenitez:master
Choose a base branch
from
DaniPopes:less-mono
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.
Conversation
This file contains 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
The `F` generic in `MapDe` and `SeqDe` makes it so that every constructed instance has its own type, which creates unnecessary monomorphizations of the underlying `Deserialize` methods. This is especially noticeable when the type being deserialized is a struct with 100+ fields that automatically derives the `Deserialize` implementation, as the multiple copies of the `visit_map` function end up being up to hundreds of KiB, bloating code size and compilation time. Here's an example output of [`cargo-bloat`] on such a configuration: ```text ... 0.0% 0.1% 64.2KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::magic::Tagged<()> as figment::value::magic::Magic>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#1}>> 0.0% 0.1% 64.2KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::magic::Tagged<()> as figment::value::magic::Magic>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#2}>> 0.0% 0.1% 64.2KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::magic::RelativePathBuf as figment::value::magic::Magic>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#2}>> 0.0% 0.1% 64.2KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::magic::RelativePathBuf as figment::value::magic::Magic>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#0}>> 0.0% 0.1% 64.2KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::value::Value>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#0}>> 0.0% 0.1% 64.0KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::magic::Tagged<()> as figment::value::magic::Magic>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#1}>> 0.0% 0.1% 64.0KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::magic::Tagged<()> as figment::value::magic::Magic>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#2}>> 0.0% 0.1% 64.0KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::magic::RelativePathBuf as figment::value::magic::Magic>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#2}>> 0.0% 0.1% 64.0KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::magic::RelativePathBuf as figment::value::magic::Magic>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#0}>> 0.0% 0.1% 64.0KiB foundry_config <<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<figment::value::de::MapDe<figment::value::de::ConfiguredValueDe, <figment::value::value::Value>::deserialize_from<<foundry_config::Config as serde::de::Deserialize>::deserialize::__Visitor, figment::value::de::DefaultInterpreter>::{closure#0}>> ``` Essentially, each of `Tagged<()>`, `RelativePathBuf`, and `Value` have their own instantiation of `MapDe`, which duplicates the massive automatically-derived `serde` `visit_map` method. [`cargo-bloat`]: https://github.com/RazrFalcon/cargo-bloat This PR removes this `F` generic by introducing a separate trait, implemented on the deserializer `D`, that is just a simple constructor for `D`. Another approach would be simply converting the generic `F: Fn...` to `&dyn Fn`, but this introduces some runtime overhead, so I've opted for the slightly more verbose approach.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
F
generic inMapDe
andSeqDe
makes it so that every constructed instance has its own type, which creates unnecessary monomorphizations of the underlyingDeserialize
methods. This is especially noticeable when the type being deserialized is a struct with 100+ fields that automatically derives theDeserialize
implementation, as the multiple copies of thevisit_map
function end up being up to hundreds of KiB, bloating code size and compilation time.Here's an example output of
cargo-bloat
on such a configuration:Essentially, each of
Tagged<()>
,RelativePathBuf
, andValue
have their own instantiation ofMapDe
, which duplicates the massive automatically-derivedserde
visit_map
method.This PR removes this
F
generic by introducing a separate trait, implemented on the deserializerD
, that is just a simple constructor forD
.Another approach would be simply converting the generic
F: Fn...
to&dyn Fn
, but this introduces some runtime overhead, so I've opted for the slightly more verbose approach.