Skip to content

Optimize panicking on enum variants #7493

@ironcev

Description

@ironcev

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.

Metadata

Metadata

Assignees

Labels

compilerGeneral compiler. Should eventually become more specific as the issue is triagedcompiler: frontendEverything to do with type checking, control flow analysis, and everything between parsing and IRgencompiler: irIRgen and sway-ir including optimization passesteam:compilerCompiler Team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions