#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum Enum1 {
Item1,
Item2,
Item3,
// 20,000 more
}
The cargo expand
shows that the Debug and Clone traits is expanded to large match with 20000 arms
// Debug
match (&*self,) {
(&Enum1::Item1,) => {
let debug_trait_builder =
&mut ::core::fmt::Formatter::debug_tuple(f, "Item1");
::core::fmt::DebugTuple::finish(debug_trait_builder)
}
// 20,000 more
}
// Clone
match (&*self,) {
(&Enum1::Item1,) => Enum1::Item1,
// 20,000 more
}
The cargo rustc self profile shows the bottleneck:
type_check_crate > item_bodies_checking > typeck_item_bodies > typeck
18.2% of the time
match_checking > misc_checking_2 > check_match
29.5% of the time each * 2 times
MIR_borrow_checking > mir_borrowck
17% of the time
I want to improve the speed of compilation.
For the Debug trait, I'm planning to use a lazy_static hashmap to map the variants to string. Hopefully it's not a big problem.
For the Clone trait, since all the variants hold no data inside, I want to use a bitwise Copy
, but Copy
requires Clone
to be implemented. Is there some way I can do a simple copy here?
Ways that I haven't tried yet:
num_traits::FromPrimitives & ToPrimitives
#[repr(u16)]
Any suggestion appreciates!