Why '?' operator is so slow?

Hi all,

I am the maintainer of a plotting library, plotters. And I was discussing some performance issue on Github with others. And I realized one thing that is quite shocking to me. It seems two "?" operator makes the crate 50% slower.

Here's the benchmark

Local repo:

test sine_640_480                                  ... bench:   4,188,667 ns/iter (+/- 338,945)
test sine_640_480_2_horizontal_subgraphs_draw_both ... bench:   4,609,418 ns/iter (+/- 407,366)
test sine_640_480_2_horizontal_subgraphs_draw_one  ... bench:   2,298,113 ns/iter (+/- 461,527)
test sine_640_480_2_horizontal_subgraphs_fake_blit ... bench:     270,355 ns/iter (+/- 53,794)

0.2.8:

test sine_640_480                                  ... bench:   7,277,665 ns/iter (+/- 3,636,985)
test sine_640_480_2_horizontal_subgraphs_draw_both ... bench:   8,129,140 ns/iter (+/- 937,515)
test sine_640_480_2_horizontal_subgraphs_draw_one  ... bench:   4,303,821 ns/iter (+/- 255,423)
test sine_640_480_2_horizontal_subgraphs_fake_blit ... bench:     288,119 ns/iter (+/- 102,912)

The code I changed to make the difference is https://github.com/38/plotters/commit/05716072909fda7769854f5a3caae85771d71c50

It seems the into_result uses 50% of the CPU time in total.

Is anybody know what is going on?


The link to the original issue in github: https://github.com/38/plotters/issues/58
The CPU Profiler result: https://user-images.githubusercontent.com/3023614/66969933-c4b4b700-f048-11e9-9456-780b91bb7f8b.png


And I checked the Try::into_result impl for Result<T,E>, I don't know why this function so expensive at all. https://doc.rust-lang.org/src/core/result.rs.html#1351-1369

It is currently just a bit more complicated than the optimizer can handle. Work on improving it is tracked here: https://github.com/rust-lang/rust/issues/37939

6 Likes