-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
When panicking on enum variants, the cost of encoding the variant in order to log it becomes extremely high, to the level that we can hardly talk about "zero or negligible" cost, on the contrary. E.g., the following example will end up in 472 bytes in bytecode size, most of it being in the encoding of SomeError::A.
script;
#[error_type]
enum SomeError {
#[error(m = "An error has occurred.")]
A: (),
}
fn main() {
panic SomeError::A;
}The equivalent panicking using a str error will produce a zero overhead and the bytcode size of the example will shrink to 112 bytes, mostly spent in the __entry function.
script;
fn main() {
panic "An error has occurred.";
}The generated IR is given below in a comment, for reference.
We expect this will improve after we improve encoding/decoding in general. But regardless of that, in many cases a general overhead of having a match will always be there.
What can be done in addition is an optimization on the compiler side in the cases like the one above which will be very often. If the error enum instance is known at compile time, compiler can generate the slice at compile time and emit only the log with that slice, instead if encoding it at runtime.