rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
94.74k stars 12.21k forks source link

Inefficient code generated for state machine #73811

Open Plecra opened 4 years ago

Plecra commented 4 years ago

While trying to implement a parser using a state machine, I found that it isn't optimised very well. I've made a comparison on godbolt: https://godbolt.org/z/NJ9HM5

I'd expect the match statement to be replaced with jumps since the state should be statically known at the end of each branch

(Side note: I'd hope that the Normal loop got auto-vectorized in the fast version since it's performing the same check over and over)

leonardo-m commented 4 years ago

There are tons of "slow" bugs in the bugtracker, but I think most of them are not worked on.

nbdd0121 commented 4 years ago

It seems clang cannot optimise that as well. so I guess LLVM doesn't optimise state machines. https://godbolt.org/z/xaW-by

Plecra commented 4 years ago

Yup, they've even got an issue for it. https://bugs.llvm.org/show_bug.cgi?id=39043