diff options
| author | Björn Steinbrink <bsteinbr@gmail.com> | 2016-05-12 17:54:05 +0200 |
|---|---|---|
| committer | Björn Steinbrink <bsteinbr@gmail.com> | 2016-05-12 21:05:13 +0200 |
| commit | 0eeb14eaba04025aa8a4612e1935f04f2ca3fb6b (patch) | |
| tree | 410400c6c9e7f7dd6fb098f1fd0d53dfbbe5dd04 /src/libsyntax_ext/deriving/clone.rs | |
| parent | c0495417416c8e0687bc6a997507c403627f6568 (diff) | |
| download | rust-0eeb14eaba04025aa8a4612e1935f04f2ca3fb6b.tar.gz rust-0eeb14eaba04025aa8a4612e1935f04f2ca3fb6b.zip | |
Improve derived implementations for enums with lots of fieldless variants
A number of trait methods like PartialEq::eq or Hash::hash don't actually need a distinct arm for each variant, because the code within the arm only depends on the number and types of the fields in the variants. We can easily exploit this fact to create less and better code for enums with multiple variants that have no fields at all, the extreme case being C-like enums. For nickel.rs and its by now infamous 800 variant enum, this reduces optimized compile times by 25% and non-optimized compile times by 40%. Also peak memory usage is down by almost 40% (310MB down to 190MB). To be fair, most other crates don't benefit nearly as much, because they don't have as huge enums. The crates in the Rust distribution that I measured saw basically no change in compile times (I only tried optimized builds) and only 1-2% reduction in peak memory usage.
Diffstat (limited to 'src/libsyntax_ext/deriving/clone.rs')
| -rw-r--r-- | src/libsyntax_ext/deriving/clone.rs | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/src/libsyntax_ext/deriving/clone.rs b/src/libsyntax_ext/deriving/clone.rs index 2c21fd2cd5e..30fe0f2db8a 100644 --- a/src/libsyntax_ext/deriving/clone.rs +++ b/src/libsyntax_ext/deriving/clone.rs @@ -39,6 +39,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, // Clone + Copy, and then there'd be no Clone impl at all if the user fills in something // that is Clone but not Copy. and until specialization we can't write both impls. let bounds; + let unify_fieldless_variants; let substructure; match *item { Annotatable::Item(ref annitem) => { @@ -49,6 +50,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, && attr::contains_name(&annitem.attrs, "derive_Copy") => { bounds = vec![Literal(path_std!(cx, core::marker::Copy))]; + unify_fieldless_variants = true; substructure = combine_substructure(Box::new(|c, s, sub| { cs_clone("Clone", c, s, sub, Mode::Shallow) })); @@ -56,6 +58,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, _ => { bounds = vec![]; + unify_fieldless_variants = false; substructure = combine_substructure(Box::new(|c, s, sub| { cs_clone("Clone", c, s, sub, Mode::Deep) })); @@ -84,6 +87,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, ret_ty: Self_, attributes: attrs, is_unsafe: false, + unify_fieldless_variants: unify_fieldless_variants, combine_substructure: substructure, } ), |
