calamine icon indicating copy to clipboard operation
calamine copied to clipboard

Performance issue with the try! macro

Open yyzdtccjdtc opened this issue 3 years ago • 3 comments

https://github.com/tafia/calamine/blob/c80df57b4b0a088ebcc3a4f4ff11bcf61832649a/src/xls.rs#L316

According to the output file of objdump, this ? operator (which has been used to repladced try! macro since 1.39.0) will introduce some unnecessary memory operations. If we change it into let r = record.unwrap();, these memory operations will be eliminated.

Same with the ? in these lines: https://github.com/tafia/calamine/blob/c80df57b4b0a088ebcc3a4f4ff11bcf61832649a/src/xls.rs#L320-L328

With these modifications, according to my test with search_errors example, the average execution time decreased from 3.3s to 3.1s, which is a 1.06x speedup.

Hope this information helps!

yyzdtccjdtc avatar Jun 19 '22 23:06 yyzdtccjdtc

In my opinion, a library should never panic. If there's something wrong with the file, I would rather it propagates all the way to the user even if it means more branches.

hbina avatar Jun 22 '22 01:06 hbina

@yyzdtccjdtc are you sure you did compile with --release ? Also I don't think we want to panic/hard crash on invalid input..

0xpr03 avatar Jul 18 '22 11:07 0xpr03

@yyzdtccjdtc are you sure you did compile with --release ? Also I don't think we want to panic/hard crash on invalid input..

Yes, I compile it with --release and set opt-level to 3. According to my test, unwrap() and try() all will panic if the input is None, which means their output should be the same.

yyzdtccjdtc avatar Jul 18 '22 16:07 yyzdtccjdtc

While I am surprised with the drop in performance using ? (thanks for reporting it), I agree with the others that I'd rather lose some performance but ensure I'm not panicking. That being said there may be some way to ensure that some of these ? could actually never fail.

tafia avatar Oct 19 '22 16:10 tafia