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 Change the line rasterizer not using "?" to handle error. result 50% … · plotters-rs/plotters@0571607 · GitHub

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: [BUG] Investigate the performance overhead of result types · Issue #58 · plotters-rs/plotters · GitHub
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. result.rs - source

It is currently just a bit more complicated than the optimizer can handle. Work on improving it is tracked here: Reduced performance when using question mark operator instead of `try!` · Issue #37939 · rust-lang/rust · GitHub

6 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.