about summary refs log tree commit diff
path: root/src/libsyntax_ext/deriving/cmp
diff options
context:
space:
mode:
authorBjörn Steinbrink <bsteinbr@gmail.com>2016-05-12 17:54:05 +0200
committerBjörn Steinbrink <bsteinbr@gmail.com>2016-05-12 21:05:13 +0200
commit0eeb14eaba04025aa8a4612e1935f04f2ca3fb6b (patch)
tree410400c6c9e7f7dd6fb098f1fd0d53dfbbe5dd04 /src/libsyntax_ext/deriving/cmp
parentc0495417416c8e0687bc6a997507c403627f6568 (diff)
downloadrust-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/cmp')
-rw-r--r--src/libsyntax_ext/deriving/cmp/eq.rs1
-rw-r--r--src/libsyntax_ext/deriving/cmp/ord.rs1
-rw-r--r--src/libsyntax_ext/deriving/cmp/partial_eq.rs1
-rw-r--r--src/libsyntax_ext/deriving/cmp/partial_ord.rs2
4 files changed, 5 insertions, 0 deletions
diff --git a/src/libsyntax_ext/deriving/cmp/eq.rs b/src/libsyntax_ext/deriving/cmp/eq.rs
index 1b855c56a48..8bd12c39337 100644
--- a/src/libsyntax_ext/deriving/cmp/eq.rs
+++ b/src/libsyntax_ext/deriving/cmp/eq.rs
@@ -62,6 +62,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
                 ret_ty: nil_ty(),
                 attributes: attrs,
                 is_unsafe: false,
+                unify_fieldless_variants: true,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     cs_total_eq_assert(a, b, c)
                 }))
diff --git a/src/libsyntax_ext/deriving/cmp/ord.rs b/src/libsyntax_ext/deriving/cmp/ord.rs
index 74706c47087..6133adb8fc5 100644
--- a/src/libsyntax_ext/deriving/cmp/ord.rs
+++ b/src/libsyntax_ext/deriving/cmp/ord.rs
@@ -42,6 +42,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
                 ret_ty: Literal(path_std!(cx, core::cmp::Ordering)),
                 attributes: attrs,
                 is_unsafe: false,
+                unify_fieldless_variants: true,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     cs_cmp(a, b, c)
                 })),
diff --git a/src/libsyntax_ext/deriving/cmp/partial_eq.rs b/src/libsyntax_ext/deriving/cmp/partial_eq.rs
index 6406ee59a5e..e5890d7213b 100644
--- a/src/libsyntax_ext/deriving/cmp/partial_eq.rs
+++ b/src/libsyntax_ext/deriving/cmp/partial_eq.rs
@@ -73,6 +73,7 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
                 ret_ty: Literal(path_local!(bool)),
                 attributes: attrs,
                 is_unsafe: false,
+                unify_fieldless_variants: true,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     $f(a, b, c)
                 }))
diff --git a/src/libsyntax_ext/deriving/cmp/partial_ord.rs b/src/libsyntax_ext/deriving/cmp/partial_ord.rs
index e49c77285ab..cfc6dbe5cd0 100644
--- a/src/libsyntax_ext/deriving/cmp/partial_ord.rs
+++ b/src/libsyntax_ext/deriving/cmp/partial_ord.rs
@@ -38,6 +38,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
                 ret_ty: Literal(path_local!(bool)),
                 attributes: attrs,
                 is_unsafe: false,
+                unify_fieldless_variants: true,
                 combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
                     cs_op($op, $equal, cx, span, substr)
                 }))
@@ -62,6 +63,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
         ret_ty: ret_ty,
         attributes: attrs,
         is_unsafe: false,
+        unify_fieldless_variants: true,
         combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
             cs_partial_cmp(cx, span, substr)
         }))