Optimize out nop-matches · Issue #66234 · rust-lang/rust (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

@oli-obk

Description

@oli-obk

The ? operator is not zero cost right now, because the matches generated by it don't get optimized as well as they could.

Looking at https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=627df52e7c476667ecf9a9831eecf829

I see

_3 = ((_1 as Ok).0: u32); _4 = _3; ((_0 as Ok).0: u32) = move _4; discriminant(_0) = 0;

and

_5 = ((_1 as Err).0: i32); _6 = _5; ((_0 as Err).0: i32) = move _6; discriminant(_0) = 1;

Which we could reasonably write a peephole optimization for getting transformed to

each

Then a second optimization could find switchInt terminators where all basic blocks are the same and eliminate the switchInt by replacing it to a goto to the first basic block being switched to.

This will even benefit matches where only one arm is a nop, because that arm will just become a memcopy