Skip to content

Commit

Permalink
Decouple serde from its derive crate (bytecodealliance#6917)
Browse files Browse the repository at this point in the history
By not activating the `derive` feature on `serde`, the compilation speed
can be improved by a lot. This is because `serde` can then compile in
parallel to `serde_derive`, allowing it to finish compilation possibly
even before `serde_derive`, unblocking all the crates waiting for
`serde` to start compiling much sooner.

As it turns out the main deciding factor for how long the compile time of a
project is, is primarly determined by the depth of dependencies rather
than the width. In other words, a crate's compile times aren't affected
by how many crates it depends on, but rather by the longest chain of
dependencies that it needs to wait on. In many cases `serde` is part of
that long chain, as it is part of a long chain if the `derive` feature
is active:

`proc-macro2` compile build script > `proc-macro2` run build script >
`proc-macro2` > `quote` > `syn` > `serde_derive` > `serde` >
`serde_json` (or any crate that depends on serde)

By decoupling it from `serde_derive`, the chain is shortened and compile
times get much better.

Check this issue for a deeper elaboration:
serde-rs/serde#2584

For `wasmtime` I'm seeing a reduction from 24.75s to 22.45s when
compiling in `release` mode. This is because wasmtime through `gimli`
has a dependency on `indexmap` which can only start compiling when
`serde` is finished, which you want to happen as early as possible so
some of wasmtime's dependencies can start compiling.

To measure the full effect, the dependencies can't by themselves
activate the `derive` feature. I've upstreamed a patch for
`fxprof-processed-profile` which was the only dependency that activated
it for `wasmtime` (not yet published to crates.io). `wasmtime-cli` and
co. may need patches for their dependencies to see a similar
improvement.
  • Loading branch information
CryZe authored and eduardomourar committed Sep 6, 2023
1 parent 03aa21d commit 7b8294f
Show file tree
Hide file tree
Showing 78 changed files with 186 additions and 97 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion cranelift/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ cranelift-control = { workspace = true }
hashbrown = { workspace = true, features = ["raw"] }
target-lexicon = { workspace = true }
log = { workspace = true }
serde = { version = "1.0.94", features = ["derive"], optional = true }
serde = { version = "1.0.188", optional = true }
serde_derive = { version = "1.0.188", optional = true }
bincode = { version = "1.2.1", optional = true }
gimli = { workspace = true, features = ["write"], optional = true }
smallvec = { workspace = true }
Expand Down Expand Up @@ -87,6 +88,7 @@ all-arch = [
# For dependent crates that want to serialize some parts of cranelift
enable-serde = [
"serde",
"serde_derive",
"cranelift-entity/enable-serde",
"regalloc2/enable-serde",
"smallvec/serde"
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/meta/src/gen_inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ fn gen_opcodes(all_inst: &AllInstructions, fmt: &mut Formatter) {
fmt.line(
r#"#[cfg_attr(
feature = "enable-serde",
derive(serde::Serialize, serde::Deserialize)
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]"#,
);

Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/binemit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod stack_map;
pub use self::stack_map::StackMap;
use core::fmt;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// Offset in bytes from the beginning of the function.
///
Expand Down
5 changes: 4 additions & 1 deletion cranelift/codegen/src/binemit/stack_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ const NUM_BITS: usize = core::mem::size_of::<Num>() * 8;
/// live GC references in these slots. We record the `IncomingArg`s in the
/// callee's stack map.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "enable-serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(
feature = "enable-serde",
derive(serde_derive::Deserialize, serde_derive::Serialize)
)]
pub struct StackMap {
bitmap: Vec<BitSet<Num>>,
mapped_words: u32,
Expand Down
5 changes: 4 additions & 1 deletion cranelift/codegen/src/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use core::ops::{Add, BitOr, Shl, Sub};

/// A small bitset built on a single primitive integer type
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "enable-serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
pub struct BitSet<T>(pub T);

impl<T> BitSet<T>
Expand Down
4 changes: 2 additions & 2 deletions cranelift/codegen/src/incremental_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl std::fmt::Display for CacheKeyHash {
}
}

#[derive(serde::Serialize, serde::Deserialize)]
#[derive(serde_derive::Serialize, serde_derive::Deserialize)]
struct CachedFunc {
// Note: The version marker must be first to ensure deserialization stops in case of a version
// mismatch before attempting to deserialize the actual compiled code.
Expand All @@ -139,7 +139,7 @@ struct CacheKey<'a> {
parameters: CompileParameters,
}

#[derive(Clone, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
#[derive(Clone, PartialEq, Hash, serde_derive::Serialize, serde_derive::Deserialize)]
struct CompileParameters {
isa: String,
triple: String,
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/atomic_rmw_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use core::fmt::{self, Display, Formatter};
use core::str::FromStr;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/condcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::fmt::{self, Display, Formatter};
use core::str::FromStr;

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// Common traits of condition codes.
pub trait CondCode: Copy {
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use core::str::{from_utf8, FromStr};
use cranelift_entity::EntityRef;

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// This type describes the actual constant data. Note that the bytes stored in this structure are
/// expected to be in little-endian order; this is due to ease-of-use when interacting with
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::u16;

use alloc::collections::BTreeMap;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
use smallvec::SmallVec;

/// Storage for instructions within the DFG.
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/dynamic_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ir::PrimaryMap;
use crate::ir::Type;

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// A dynamic type object which has a base vector type and a scaling factor.
#[derive(Clone, PartialEq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::entity::entity_impl;
use core::fmt;
use core::u32;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// An opaque reference to a [basic block](https://en.wikipedia.org/wiki/Basic_block) in a
/// [`Function`](super::function::Function).
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/extfunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use alloc::vec::Vec;
use core::fmt;
use core::str::FromStr;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

use super::function::FunctionParameters;

Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/extname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::str::FromStr;

use cranelift_entity::EntityRef as _;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

use super::entities::UserExternalNameRef;
use super::function::FunctionParameters;
Expand Down
10 changes: 8 additions & 2 deletions cranelift/codegen/src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ impl<'de> Deserialize<'de> for VersionMarker {
/// Function parameters used when creating this function, and that will become applied after
/// compilation to materialize the final `CompiledCode`.
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "enable-serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
pub struct FunctionParameters {
/// The first `SourceLoc` appearing in the function, serving as a base for every relative
/// source loc in the function.
Expand Down Expand Up @@ -146,7 +149,10 @@ impl FunctionParameters {
/// Additionally, these fields can be the same for two functions that would be compiled the same
/// way, and finalized by applying `FunctionParameters` onto their `CompiledCodeStencil`.
#[derive(Clone, PartialEq, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "enable-serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
pub struct FunctionStencil {
/// A version marker used to ensure that serialized clif ir is never deserialized with a
/// different version of Cranelift.
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/globalvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::isa::TargetIsa;
use core::fmt;

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// Information about a global value declaration.
#[derive(Clone, PartialEq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/immediates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Sub};
use core::str::FromStr;
use core::{i32, u32};
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// Convert a type into a vector of bytes; all implementors in this file must use little-endian
/// orderings of bytes to match WebAssembly's little-endianness.
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::ops::{Deref, DerefMut};
use core::str::FromStr;

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

use crate::bitset::BitSet;
use crate::entity;
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/jumptable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::fmt::{self, Display, Formatter};
use core::slice::{Iter, IterMut};

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// Contents of a jump table.
///
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/known_symbol.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use core::str::FromStr;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// A well-known symbol.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/libcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use core::fmt;
use core::str::FromStr;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// The name of a runtime library routine.
///
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/memflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use core::fmt;

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

enum FlagBit {
Notrap,
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod trapcode;
pub mod types;

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

pub use crate::ir::atomic_rmw_op::AtomicRmwOp;
pub use crate::ir::builder::{
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/sourceloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use core::fmt;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// A source location.
///
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/stackslot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::ir::{DynamicTypeData, GlobalValueData};
use crate::ir::types::*;

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// The size of an object on the stack, or the size of a stack frame.
///
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ir::{GlobalValue, Type};
use core::fmt;

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// Information about a table declaration.
#[derive(Clone, PartialEq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/trapcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use core::fmt::{self, Display, Formatter};
use core::str::FromStr;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// A trap code describing the reason for a trap.
///
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::default::Default;
use core::fmt::{self, Debug, Display, Formatter};
use cranelift_codegen_shared::constants;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
use target_lexicon::{PointerWidth, Triple};

/// The type of an SSA value.
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/isa/call_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::str;
use target_lexicon::{CallingConvention, Triple};

#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

/// Calling convention identifiers.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand Down
Loading

0 comments on commit 7b8294f

Please sign in to comment.